MOD

The MOD function divides the first argument by the second argument and returns the remainder.

Read syntax diagram
>>-MOD(numeric-expression-1,numeric-expression-2)--------------><

The schema is SYSIBM.

The formula used to calculate the remainder is: Start of change
   MOD(x,y) = x - FLOOR(x/y) * y
End of change Where x/y is the truncated integer result of the division. The result is negative only if the first argument is negative.

Each argument must be an expression that returns a value of any built-in numeric data type.

Start of changeThe arguments can also be a character string or graphic string data type. The string input is implicitly cast to a numeric value of DECFLOAT(34).End of change

The result can be null; if any argument is null, the result is the null value.

The attributes of the result are based on the arguments as follows:

  • If both arguments are large or small integers, the data type of the result is large integer.
  • If both arguments are integers and at least one argument is a big integer, the data type of the result is big integer.
  • If one argument is an integer and the other is a decimal, the data type of the result is decimal with the same precision and scale as the decimal argument.
  • If both arguments are decimal, the data type of the result is decimal. The precision of the result is min(p-s,p'-s') + max(s,s'), and the scale of the result is max(s,s'), where the symbols p and s denote the precision and scale of the first argument, and the symbols p' and s' denote the precision and scale of the second argument.
  • If one argument is a floating-point number, and the other is not a DECFLOAT, or both argument is a floating-point number, the data type of the result is double precision floating-point.

    The operation is performed in floating-point. If necessary, the operands are first converted to double precision floating-point numbers. For example, an operation that involves a floating-point number and either an integer or a decimal number is performed with a temporary copy of the integer or decimal number that has been converted to double precision floating-point. The result of a floating-point operation must be within the range of floating-point numbers.

  • If either argument is a DECFLOAT, the data type of the result is DECFLOAT(34).

    If either argument is a special decimal floating point value, the general rules for arithmetic operations apply. See General Arithmetic Operation Rules for DECFLOAT for more information.

    If one argument is a DECFLOAT and the second argument is zero, the result is NaN and an invalid operation condition is returned.

Example: Assume that M1 and M2 are two host variables. Find the remainder of dividing M1 by M2.
   SELECT MOD(:M1,:M2)
     FROM SYSIBM.SYSDUMMY1;
The following table shows the result for this function for various values of M1 and M2.
M1 data type M1 value M2 data type M2 value
Result of
MOD(:M1,:M2)
INTEGER 5 INTEGER 2 1
INTEGER 5 DECIMAL(3,1) 2.2 0.6
INTEGER 5 DECIMAL(3,2) 2.20 0.60
DECIMAL(4,2) 5.50 DECIMAL(4,1) 2.0 1.50
DECFLOAT 1 DECFLOAT -INFINITY 1
DECFLOAT -0 DECFLOAT INFINITY -0
DECFLOAT -0 DECFLOAT -INFINITY -0