Re: simple eurphoria question
- Posted by Ricardo M. Forno <rmforno at tutopia.com> Dec 07, 2006
- 683 views
c.k.lester wrote: > > Larry Miller wrote: > > 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: > > > 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 > > Nice job, you guys. Thanks! :) Yes, this last solution is simpler, but a bit slower when dealing with many numbers. Please see my General functions library, where this and other similar problems are dealt with. Regards.