[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: math.floor and rounding
- From: James Graves <ansible@...>
- Date: Tue, 21 Sep 2010 10:31:12 -0500
On Tue, Sep 21, 2010 at 05:01:13AM +0000, J.J?rgen von Bargen wrote:
> local function round(x)
> if x<0 then return -round(-x) end
> return math.floor(x+0.5)
> end
>
> Test with -1.5 : my round=-2 your round=-1
Yes, handling values that have a fraction part exactly equal to 0.5 is
a little tricky. Your method is "round half away from zero" in the
tie-breaking section here:
http://en.wikipedia.org/wiki/Rounding#Rounding_to_integer
I was actually surprised at the number of different methods that have
been invented just to handle that pesky 0.5 fraction.
If you are doing some serious math, you'll need to read up on all of
that.
James Graves