lua-users home
lua-l archive

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


you can trash this message


after few silly thought I respond to fast , I need to go back and do some reel floating point low level things :o)

On 2015-08-08 1:31 AM, voidptr wrote:


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.

???
I agree with binary representation this NOT exactly what I meant ...

a = 1.45555
b = a * 10^4
-- b = 14555.5
b = b + 0.5
-- b = 14556

well that seems ok   in fact lua in the interpreter show me 14556.0
AND ...

c = math.floor(b)
-- c = 14555   gave me NOT 14556  but 14555 !!!


so you are telling me that 14556.0  can be 14555.09 ???


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