Re: simple eurphoria question

new topic     » goto parent     » topic index » view thread      » older message » newer message

Mike Nelson wrote:
> 
> c.k.lester wrote:
> 
> > I wrote a function just for this purpose. It's called
> > round_this_number_toward_zero_remember_that_negative_numbers_are_screwy().
> > 
> > I sometimes wonder if it's worth typing all that out just for
> > 
> > if x < 0 then
> >    return floor(x+1)
> > else
> >    return floor(x)
> > end if
> 
> This is not quite correct. if x is say, -2, it will return -1 rather than the
> correct -2.
> 
> This will work:
> 
> }}}
<eucode>
> if integer(x) then
>      return x
> elsif x<0 then
>      return floor(x+1)
> else
>      return floor(x)
> end if

A simpler method that works is:
if x < 0 then
    return -floor(-x)
else
    return floor(x)
end if


new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu