View Full Version : Bug in IEEERemainder function
DickBaldwin
11-05-2007, 04:43 PM
If you call the IEEERemainder function to get the remainder of 7/2, the value returned is -1, which is incorrect.
If you call the IEEERemainder function to get the remainder of 8/3, the value returned is -1, which is incorrect.
If you call the IEEERemainder function to get the remainder of 5/2, the value returned is 1, which is correct.
Sometimes the function returns the correct answer and sometimes it returns the wrong answer. On the basis of a very limited amount of experimentation, it seems to return the correct answer for those cases where the correct answer is 0.
Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm
dragnet
03-23-2010, 12:07 AM
To those who care...and I do because I use modulo quite often. It seems that I had teh same problem as Dick. The IEEERemainder function does not seem to work the way I would expect with some combinations of numbers. If you are getting weird results back from IEEERemainder try creating this function:
2688
aikmin
04-23-2010, 04:36 PM
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.
flats
04-24-2010, 09:11 AM
Short answer: There is no bug. IEEERemainder is working properly.
Long answer below:
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.
arty-fishL
04-26-2010, 02:30 PM
Is there a way to actually find the plain remainder of a number?
arty-fishL
04-26-2010, 03:04 PM
Is there a way to actually find the plain remainder of a number?
Actually, I've found a way (this is a custom function):
flats
04-27-2010, 02:48 PM
Is there a way to actually find the plain remainder of a number?
If (IEEERemainder (a,b) < 0)
Return IEEERemainder (a,b) + b
Else
Return IEEERemainder (a,b)
edit: Your method will work as well.
If you have a positive remainder function, to turn it to IEEERemainder:
If (remainder(a,b) > b/2)
Return remainder(a,b) - b
Else
Return remainder(a,b)
arty-fishL
04-27-2010, 03:05 PM
If (IEEERemainder (a,b) < 0)
Return IEEERemainder (a,b) + b
Else
Return IEEERemainder (a,b) edit: Your method will work as well.
...
Your smart.