AMOS:Bug Mod

From Amiga Coding
Revision as of 22:09, 28 September 2007 by Spellcoder (talk | contribs) (Description/workaround for the bug in 'mod' when used with a negative value)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The help in AMOSPro says this about mod:

It works by returning the remainder after a division.

r=v Mod d

This is equivalent to:

R=v - Int(v/d)*d

However try this:
(V=Value, D=Divider, R=Remainder)

R1=V mod D

Print R1
Print R2

When the Value is negative, the Remainder should be negative too (or zero).
The formula in R2 works correct, but AMOSPro incorrectly returns the result (the remainder) as positive.


Probably the best workaround (least overhead/fastest and won't ever break) is:

R=V mod D
If V<0 And R>0 Then R=-R

What this does is check for the bug. If the Value is negative but the answer is positive, then it fixes the result by making it negative.

It's important to check if the result is indeed incorrect (positive), else if the bug were ever fixed your program will have a nasty bug.


  • bug description and workaround by Spellcoder (Mark de Jong) - 28 sep 2007