Coding challenge?

Dennis Brown see3d at writeme.com
Mon May 30 20:56:32 EDT 2005


On May 30, 2005, at 7:56 PM, Thomas McGrath III wrote:

> I would like to see something graphic or animation based but would  
> probably learn more from an SQL or XML challenge. You guys always  
> blow me away with MOD and TRUNC etc. Since math is not to my  
> advantage, I am rather visual in nature.
>
> TOm

Tom,

I am no math whiz either, but if you are you asking for an  
explaination of the way I use Mod and Trunc, it is very simple:

Trunc(number) is simply the integer function.  It hands you back the  
number with any decimal portions thrown away (no rounding).  10.1  
becomes 10 and 10.99999 becomes 10.

Mod is the remainder function from a division.  It performs a  
division and throws away the answer but hands you back the  
remainder.  10.99999 mod 10 is 0.99999.  10.99999/10 the answer is 1  
with a remainder of 0.99999.

It is handy in loops if you want to see if a counter is at every Nth  
count  --like every 11th time through the loop you want to do  
something different.  You could say if loopCounter mod 11=0 then  
doSomething.  The remainder will only be 0 if the loopCounter is an  
even multiple of 11.  I use it in this way to update a field or check  
for user aborts inputs in a long loop where I don't want to waste  
time doing the UI stuff every time through the loop.

It is also handy to do the opposite of the Trunc function --where you  
want to throw away the whole number and keep the decimal portion.  In  
this case anyNumber mod 1 will do the trick.  10.99999 mod 1 gives  
you 0.99999.

So:

get 10.99999
get trunc(it)&it mod 1 --take the number apart and put it back  
together again

it=10.99999

Hope this helps,
Dennis


More information about the use-livecode mailing list