MOD
MOD returns the modular equivalent of the remainder of one value divided by another.
MOD returns the smallest nonnegative value, R, such that (x
- R)/y = n.
In this example, the value for n is an integer value. That is, R is the smallest nonnegative value that must be subtracted from x to make it divisible by y.
- x
- Real expression.
- y
- Real expression. If y = 0, the ZERODIVIDE condition is raised.
The result, R, is real with the common base and scale of the arguments.
If the result is floating-point, the precision is the greater of those
of x and y. If
the result is fixed-point, the precision is given by the following:
(min(n,p2-q2+max(q1,q2)),max(q1,q2))In this example, (p1,q1) and (p2,q2) are the precisions of x and y, respectively, and n is N for FIXED DECIMAL or M for FIXED BINARY.
If x and y are fixed-point with different scaling factors, the argument with the smaller scaling factor is converted to the larger scaling factor before R is calculated. If the conversion fails, the result is unpredictable.
If the result has the attributes FIXED BIN and one or more of the
operands has the attributes UNSIGNED FIXED BIN, the result has the
SIGNED attribute unless both of the following conditions are true:
- All of the operands are UNSIGNED FIXED BIN.
- The RULES(ANS) compiler option is in effect.
- Raise the SIZE condition if SIZE is enabled.
- Produce a negative value if SIZE is not enabled.
Example
The following example contrasts the MOD and REM built-in functions.
rem( +10, +8 ) = 2
mod( +10, +8 ) = 2
rem( +10, -8 ) = 2
mod( +10, -8 ) = 2
rem( -10, +8 ) = -2
mod( -10, +8 ) = 6
rem( -10, -8 ) = -2
mod( -10, -8 ) = 6