lua-users home
lua-l archive

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




On 2015-08-08 1:09 AM, Coda Highland wrote:
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.

darn so silly I am   !!
I agree with binary representation this NOT exactly what I meant ...
b = b + 0.5
-- b = 14556.0
should give me something like this
14555.999999999998994582028899458236992359161376953125   +/- 0.01 (kindof)


I miss to include the .0 decimal
in fact the interpreter show me 14556.0
so I thought if the interpreter show me 14556.0 that .0 means in was securely rounded to that decimal.
and number have been correctly reinterpreted ...

so never believe your eyes !


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