[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Rounding a number to integer
- From: Roberto Ierusalimschy <roberto@...>
- Date: Fri, 5 Aug 2016 12:35:52 -0300
> On Sun, Jul 31, 2016 at 9:33 PM, Roberto Ierusalimschy <
> roberto@inf.puc-rio.br> wrote:
>
> > What about this one?
> >
> > function round (x)
> > return math.floor((x % 2.0 == 0.5) and x or x + 0.5)
> > end
> >
>
> > (2^52+1)|0, round(2^52+1)
> 4503599627370497 4503599627370498
Another try :-)
-- unbiased rounding
function round (x)
if math.floor(x) == x then return x
else
return math.floor((x % 2.0 == 0.5) and x or x + 0.5)
end
end
-- Roberto