Coding challenge?

Sarah Reichelt sarahr at genesearch.com.au
Mon May 30 23:05:46 EDT 2005


> 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.
>
>

One more useful trick:

div gives you the integer result of a division.
e.g.
104 / 10 = 10.4
104 div 10 = 10

For rounding:
104 div 10 * 10 = 100

This can be used in conjunction with mod to break a number up into  
it's parts:
104 mod 10 = 4

Cheers,
Sarah



More information about the use-livecode mailing list