lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



Unfortunately, "simplest solution" you gave is not a solution at all.
It gives wrong results even for integers stored as float:

> x=13510798882111490.0
> x|0
13510798882111490
> (x + 0x1p+52 - 0x1p+52)|0
13510798882111488

I totally missed that 0x1.8p+53 + 0x1p+52 would shift and truncate, well spotted.

you have to filter the values, specifically for NaN and any values larger than 0x1p+52 or less than -0x1p+52 (including +/-Inf) I would return verbatim.

Yes, this filter MUST be applied.

Yes it must.  A fixed version then:

```lua
function rint(x)
    if x ~= x or x < -0x1p+52 or x > 0x1p+52 then
        -- Already an Integer, or NaN, or Inf
    elseif x < 0 then
        x = ((x - 0x1p+52) + 0x1p+52)
    else
        x = ((x + 0x1p+52) - 0x1p+52)
    end
    return math.floor(x)
end
```