| Format | Assembly Format | Description
| 5D | rem
| Remainder when dividing one value by another.
…, value1, value2 → …, result
The rem
instruction divides value1 by value2 and pushes the remainder result on the stack.
The acceptable operand types and their corresponding result data type are encapsulated in Table 2: Binary Numeric Operations.
result = value1 rem
value2 satisfies the following conditions:
-
result = value1 - value2 × (value1
div
value2), and -
0 ≤ |result| < |value2|, and
-
sign(result) = sign(value1),
where div
is the division instruction, which truncates towards zero.
rem
is defined similarly as for integer operands, except that, if value2 is zero or value1 is infinity, result is NaN. If value2 is infinity, result is value1. This definition is different from the one for floating-point remainder in the IEC 60559:1989 Standard. That Standard specifies that value1 div
value2 is the nearest integer instead of truncating towards zero. System.Math.IEEERemainder
(see Partition IV) provides the IEC 60559:1989 behavior.
Integral operations throw System.DivideByZeroException
if value2 is zero.
Integral operations can throw System.ArithmeticException
if value1 is the smallest representable integer value and value2 is -1.
+10 rem +6 is 4 |
(+10 div +6 = 1) |
+10 rem -6 is 4 |
(+10 div -6 = -1) |
-10 rem +6 is -4 |
(-10 div +6 = -1) |
-10 rem -6 is -4 |
(-10 div -6 = 1) |
For the various floating-point values of 10.0 and 6.0, rem
gives the same values; System.Math.IEEERemainder
, however, gives the following values.
System.Math.IEEERemainder (+10.0, +6.0) is -2 |
(+10.0 div +6.0 = 1.666…7) |
System.Math.IEEERemainder (+10.0, -6.0) is -2 |
(+10.0 div -6.0 = -1.666…7) |
System.Math.IEEERemainder (-10.0, +6.0) is 2 |
(-10.0 div +6.0 = -1.666…7) |
System.Math.IEEERemainder (-10.0, -6.0) is 2 |
(-10.0 div -6.0 = 1.666…7) |