Short answer: There is no bug. IEEERemainder is working properly.
Long answer below:
Quote:
Originally Posted by aikmin
Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. The remainder value is mathematically equal to f1 - f2 × n, where n is the mathematical integer closest to the exact mathematical value of the quotient f1/f2, and if two mathematical integers are equally close to f1/f2, then n is the integer that is even. If the remainder is zero, its sign is the same as the sign of the first argument. Special cases:
If either argument is NaN, or the first argument is infinite, or the second argument is positive zero or negative zero, then the result is NaN.
If the first argument is finite and the second argument is infinite, then the result is the same as the first argument.
|
That is what the javadoc says. Let me try to clarify.
When you're taught a remainder in grade school, you're taught it's the bit leftover when you do division. 9 / 5 = 1 remainder 4, because 5 goes into 9 once, with 4 left over.
The IEEERemainder method works a little differently, and tries to make the absolute value of the remainder as small as possible. If you're doing 9/5, it finds the multiple of 5 closet to 9. In this case, 10 is closer to 9 than 5 is. But 10 is bigger than 9; it's 1 too big. So your remainder is
negative 1.
Mathematically, your two cases are:
9 = 1 * 5 + 4
9 = 2 * 5 + -1
I was going to write you a method to turn IEEERemainder into the % operator, but since Dick already has an entire website of tutorials and should know his stuff, I guess I'll leave it as an exercise to the reader.