MOD function does not work! So be warned!

Rob Cozens rcozens at pon.net
Wed Apr 3 11:02:16 EST 2002


>The situation is that when you convert .1 to binary and add it to 255 it is
>actually adding a binary number whose value is a little less than the value
>of decimal .10. .1 has no exact binary representation

Hi Michael (Mays),

One of us is off base here, and since you didn't pick up on my 
question to Dar, I don't know who.

>I think there's more to it than that:
>
>In
>
><<
>on testMod
>   put 249 into var1
>   put var1 into var2
>   add 1.0 to var1
>   repeat 10
>     add 0.1 to var2
>   end repeat
>   put (var1=var2)&&((var1 mod 5)=(var2 mod 5))
>end testMod
><<
>
>The first test always resolves to true...only the second test yields 
>a false result.

I say the issue may be related to inexact decimal representation; but 
the mod function is where the error is manifested.  If it were simply 
a matter of x being 255 plus/minus .0001 instead of 255, x mod 5 
should not return 5.  There is no value v for which v mod 5 is ever 5.

A final note:  Someone else mentioned the mod operator is supposed to 
be used with integers, so anyRealValue mod anyBase is non-sequitur. 
I didn't see anything in the documentation restricting the use of mod 
to integers; OTOH, I can't recall having a practical need for the mod 
of a non-integer in 25+ years of programming.


-- 

Rob Cozens
CCW, Serendipity Software Company
http://www.oenolog.com/who.htm

"And I, which was two fooles, do so grow three;
Who are a little wise, the best fooles bee."

from "The Triple Foole" by John Donne (1572-1631)



More information about the use-livecode mailing list