rounding up with < .5 ?

Dar Scott dsc at swcp.com
Thu Mar 14 18:19:07 EST 2002


On Thursday, March 14, 2002, at 03:26 PM, Subscriber wrote:
> if trunc(x)=x then          --check the number is an integer
>     return x
> else
>     return (trunc(x)+1)     -- if not then return the next integer 
> above
> end if

I like this.

However, one has to add a comment line mentioning that it works 
funny with negative numbers.  Otherwise, it will jump up and bite 
you months or years down the road.

Standing on Gary's shoulders, I suggest something like this to 
handle negative numbers, too:

function ceiling n
   if trunc(n)=n then
     return n
   else
     if n>0 then
       return trunc(n)+1
     else
       return trunc(n)
     end if
   end if
end ceiling

Once you get the bugs out of it (I typed it in off the top of my 
head), this function will last you a lifetime.  The ceiling 
function is found in many calculation methods and has an 
agreed-upon meaning.

Dar Scott





More information about the use-livecode mailing list