lua-users home
lua-l archive

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


Hello, everyone.

There's an issue that bothers me with regards to float-to-string formatting in Lua.

It appears to be imprecise, unless you're doing hexadecimal formatting.

I'm getting these results in Lua 5.4.2

> tostring(0x1.a8e64000018dp-1)
0.82988166809153
> 0x1.a8e64000018dp-1 == tonumber(tostring(0x1.a8e64000018dp-1))
false
> 0x1.a8e64000018dp-1 == tonumber(string.format("%f", 0x1.a8e64000018dp-1))
false
> 0x1.a8e64000018dp-1 == tonumber(string.format("%e", 0x1.a8e64000018dp-1))
false
> 0x1.a8e64000018dp-1 == tonumber(string.format("%a", 0x1.a8e64000018dp-1))
true

There seems to be some loss of precision in non-hex formatting.
Is this something fixable?
Is there some way for me (in Lua) to format floats to decimal strings and not lose precision?