You can use ieeereminder(r) in an OCL expression when you need the IEEE floating-point remainder of a number divided by r.
Syntax
self.ieeereminder(r)
| Part | Meaning |
|---|---|
self
|
The dividend: the number to divide. |
r
|
The divisor. |
| Result | The IEEE remainder calculated from the dividend and divisor. |
How the result is calculated
The operator calculates the result as:
dividend - (divisor * Q)
Q is the quotient dividend / divisor rounded to the nearest integer. When the quotient is exactly halfway between two integers, Q is the even integer. See Number::round() for the related rounding operation.
For example, 3.ieeereminder(2) has a quotient of 1.5. The nearest integer is 2, so the result is 3 - (2 * 2), which is -1.
Examples
| Expression | Calculation of Q | Result |
|---|---|---|
3.ieeereminder(2)
|
3 / 2 = 1.5; halfway rounds to the even integer 2
|
-1
|
4.ieeereminder(2)
|
4 / 2 = 2
|
0
|
10.ieeereminder(3)
|
10 / 3 rounds to 3
|
1
|
11.ieeereminder(3)
|
11 / 3 rounds to 4
|
-1
|
27.ieeereminder(4)
|
27 / 4 = 6.75; rounds to 7
|
-1
|
17.8.ieeereminder(4)
|
17.8 / 4 = 4.45; rounds to 4
|
1.8
|
Difference from integer remainder
Do not use ieeereminder when you need the ordinary integer remainder. IEEE remainder uses the nearest quotient, so its result can be negative even when both operands are positive.
For example:
| Operation | Expression | Result | Reason |
|---|---|---|---|
| IEEE remainder | 11.ieeereminder(3)
|
-1
|
The quotient rounds from about 3.67 to 4.
|
| Integer remainder | 11 mod 3
|
2
|
Integer remainder uses the integer division remainder. |
Use Integer::mod for integer remainder and Integer::div for integer quotient. Number::floor() is different again: it returns the greatest integer not greater than a number.
Edge cases
- If the divisor is zero, the result is
NaN. - If the computed remainder is zero, the result is positive zero when the dividend is positive and negative zero when the dividend is negative.
- Floating-point calculations can expose binary precision. For example, a result mathematically close to
0.1may be represented with additional decimal digits.
