[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: why tostring for number use LUA_NUMBER_FMT "%.14g", not "%.16g" ?
- From: Leo Razoumov <slonik.az@...>
- Date: Sat, 7 Sep 2013 16:51:50 -0400
On 9/7/13, pulleyzzz_gmail <firstname.lastname@example.org> wrote:
> for this code:
> print(1/3==tonumber(a)) -- false
> --if use .16g
> print(1/3==tonumber(a)) -- true
Changing format to "%.16g" would not solve your problem of comparing
floats with == operator.
1/3 is computed in Lua natively in binary representation.
a=string.format('%.16g',1/3) is a binary representation converted to a
base-10 number. Very often it cannot be done without loss of accuracy.
Next, you are converting it to binary again with tonumber(a) which
causes another loss of accuracy.
As a rule of thumb do not compare floats with == operator. It will
lead to subtle and
difficult to catch errors.