MOD function does not work! So be warned!

Michael D Mays michael at GreppyDreppies.com
Fri Mar 29 08:02:01 EST 2002


[I sent this message the first time from the wrong account. If it comes
through twice, sorry--mdm]

The MOD function works. The ROUND function might not.

Decimal 0.1 when converted to binary is actually slightly greater than
decimal 0.1. Set the numberFormat to something very precise and you will see
that .1 is really
 0.10000000000000000555111512312578

When you keep adding increment eventually you reach a point where the sum of
all those 5.5e-19ths (if I counted right) become significant to the MOD
function. 

I think David Vaughan has said all this already but it is a very important
point. 

But the round is NOT the way to go
on mouseUp
  put 1 +.1 into aa
  put aa into line 1 of fld 1
  set the numberFormat to \
  "0.######################################################################"
  put aa into line 2 of fld 1
  put round(aa,1) into line 3 of fld 1
end mouseUp

fld 1 is
1.1
1.1000000000000000888178419700125
1.0999999999999998667732370449812

I don't see it in MetaCard 2.2.3 (line 2 and 3 are the same in 2.2.3) but
the results are the same as above in MC2.4.1. I think this might be a BUG.

If you want to be testing as is in this example increments of tenths, I
something like set the numberFormat to "0.#" and add the line
 put loop&"" into loop
to your repeat clause.

michael
 

Ian Summerfield of iansummerfield at btconnect.com wrote the following on
3/28/02 9:53 PM

> The code I give here might look inefficient, the code comes from a large
> project, so there are reasons for doing it this way!
> 
> It's just taken me several hours to figure out why my code wasn't working,
> and it turns out to be a bug in Revolution.
> 
> Revolution 1.1.1b2 (OS X 10.1.3) seems to think 5 mod 5 = 5 whereas it is
> zero.
> 
> 
> Try this code:
> 
> on mouseup
>  put 235 into startPoint
>  put 255 into endPoint
>  put 0.1 into increment
>  put empty into msg
>  put startPoint into loop
>  repeat while loop<=endPoint
>   put loop && loop mod 5 & return after msg
>   add increment to loop
>  end repeat
> end mouseup
> 
> It will output to the message box a list of numbers.  The loop counter is on
> each line followed by a space and the loop mod 5.
> 
> Here's my output:
> 
> 235 0   <<<<---- correct
> 235.1 0.1
> 235.2 0.2
> 235.3 0.3
> <<SNIP>>
> 240 5   <<<<---- wrong and impossible
> 240.1 0.1




More information about the use-livecode mailing list