[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: math.floor and rounding
- From: J.Jørgen von Bargen <jjvb.primus@...>
- Date: Tue, 21 Sep 2010 05:01:13 +0000 (UTC)
James Graves <ansible <at> xnet.com> writes:
> If you want to _round_ off a floating point number, instead of
> flooring it, here is the rounding function I have in my standard
> library:
>
> function round(num) return math.floor(num+.5) end
:::
> Are you handling negative numbers? math.floor may not be what you want
> in that case:
This is my way to get symmetric behavior
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
Regards Jørgen