Alice Community Bug in IEEERemainder function

 Bug in IEEERemainder function
 DickBaldwin Guest   Status: Posts: n/a Bug in IEEERemainder function - 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 Last edited by DickBaldwin; 11-05-2007 at 04:44 PM. Reason: Clarification
 dragnet Member   Status: Offline Posts: 38 Join Date: Jan 2010 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: modulo.jpg
 IEEERemainder
 aikmin Senior Member     Status: Offline Posts: 85 Join Date: Mar 2008 IEEERemainder - 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
Guest

Status:
Posts: n/a
04-24-2010, 09:11 AM

Short answer: There is no bug. IEEERemainder is working properly.

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.

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.

Last edited by flats; 04-24-2010 at 09:13 AM.

 So
 arty-fishL Senior Member     Status: Offline Posts: 1,878 Join Date: Mar 2008 Location: In the corner of your eye So - 04-26-2010, 02:30 PM Is there a way to actually find the plain remainder of a number? █░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ █░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█ █░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█ █░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█ █░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█ █░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█ █░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█ █░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ I have mostly moved on from Alice, but may still respond to messages if important [¬º-°]¬
arty-fishL
Senior Member

Status: Offline
Posts: 1,878
Join Date: Mar 2008
Location: In the corner of your eye
04-26-2010, 03:04 PM

Quote:
 Originally Posted by arty-fishL Is there a way to actually find the plain remainder of a number?
Actually, I've found a way (this is a custom function):
Attached Images
 remainder finder.jpg (56.9 KB, 39 views)

█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
█░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█
█░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█
█░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█
█░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█
█░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█
█░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█

I have mostly moved on from Alice, but may still respond to messages if important [¬º-°]¬

flats
Guest

Status:
Posts: n/a
04-27-2010, 02:48 PM

Quote:
 Originally Posted by arty-fishL Is there a way to actually find the plain remainder of a number?
Code:
```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:

Code:
```If (remainder(a,b) > b/2)
Return remainder(a,b) - b
Else
Return remainder(a,b)```

Last edited by flats; 04-27-2010 at 02:50 PM. Reason: I Triple E

 ;]
arty-fishL
Senior Member

Status: Offline
Posts: 1,878
Join Date: Mar 2008
Location: In the corner of your eye
;] - 04-27-2010, 03:05 PM

Quote:
 Originally Posted by flats Code: ```If (IEEERemainder (a,b) < 0) Return IEEERemainder (a,b) + b Else Return IEEERemainder (a,b)``` edit: Your method will work as well. ...

█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
█░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█
█░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█
█░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█
█░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█
█░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█
█░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█

I have mostly moved on from Alice, but may still respond to messages if important [¬º-°]¬

Last edited by arty-fishL; 04-27-2010 at 03:06 PM. Reason: quote was too long

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements     Community News and Announcements Alice 3     How Do I?     Works In Progress     Share Your World     Share Custom Classes     Bugs and Trouble Shooting     Suggestion Box Alice 2     How do I...?     Works-In-Progress     Share Worlds     Share Objects     Bugs and Troubleshooting     Suggestion Box Educators     Teaching with Alice     Teaching Using Alice 3     Teaching Using Alice 2     CS Principles and Alice     AP CSA and Intro to Java and Alice     Workshops General Discussion     Questions and Comments     The Lounge