lua-users home
lua-l archive

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


On Fri, Aug 7, 2015 at 9:53 PM, voidptr <voidptr69@gmail.com> wrote:
>
> from  Zerobrane IDE with lua 5.3 and 5.2
>
> a = 1.45555
> b = a * 10^4
> -- b = 14555.5
> b = b + 0.5
> -- b = 14556
> c = math.floor(b)
> -- c = 14555
> c = b // 1
> -- c = 14555
>
> it there a way to force a good  float to int conversion ???
>
> // voidptr
>

This is a standard floating-point issue.

You asked for truncation, you got truncation. b's value was never
14.556; it was 14.5559something -- because a's value is not 1.45555;
that can't be represented precisely in base 2. Its value is
1.455549something.

If you want rounding, then round. If you want lopsided rounding, then
add an epsilon and floor. (For example, you might add 0.01 before
flooring if you want to say "14555.99 is okay to call 14556, but
14555.98 should be truncated to 14555.")

/s/ Adam