mod (dividend divisor -- remainder)
Inputs
dividend: Numeric object whose remainder is taken.divisor: Numeric object dividing the dividend to produce the remainder.
Outputs
remainder: Remainder object.
mod computes the remainder of numeric division. Both inputs are dereferenced, must be non-NIL, and must have the same numeric schema.
Behavior
- For Int, the operation uses signed remainder semantics. The remainder follows the dividend sign.
- For Nat, the operation uses unsigned remainder semantics.
- For Real, the operation uses floating remainder semantics. The remainder likewise follows the dividend sign.
- If the divisor is known to be
0, the builtin is invalid. - If the dividend is the known minimum value of a signed Whole schema and the divisor is known
-1, the builtin is invalid because the corresponding signed division quotient is not representable in that schema. - If both operands are known at compile time and the operation is valid, the remainder is computed at compile time.
- If either operand is unknown, the result is an object with an unknown value of the same numeric schema, except that a known divisor
1yields a known0for Whole schemas. An unknown divisor is allowed; only a known zero divisor is rejected. - For unknown operands the operation is performed at run time without a zero-divisor or overflow check.
Example
{} () {} [
"-- int32 --" printCompilerMessage
7 3 mod printStack _:;
-7 3 mod printStack _:;
7 -3 mod printStack _:;
"-- nat32 --" printCompilerMessage
7n32 3n32 mod printStack _:;
"-- real32 --" printCompilerMessage
7.5r32 2.0r32 mod printStack _:;
-7.5r32 2.0r32 mod printStack _:;
7.5r32 -2.0r32 mod printStack _:;
"-- unknown divisor --" printCompilerMessage
divisor: 3 dynamic;
7 divisor mod printStack _:;
] "main" exportFunction
Expected Output During Compilation
-- int32 --
1
-1
1
-- nat32 --
1n32
-- real32 --
1.5r32
-1.5r32
1.5r32
-- unknown divisor --
Int32