AMOS:Bug Mod

From Amiga Coding
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)

V=-6
D=4
R1=V mod D
R2=V-Int(V/D)*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.


Workaround

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.


Tested with

AMOSPro V2.00 uncompiled - Happens with integers and floating-point numbers
AMOSPro V2.00 compiled - not tested yet
AMOS V1.36 uncompiled - not tested yet
AMOS V1.36 compiled - not tested yet


Credits

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