Reputation: 2531
What is the reason behind this type of conversion and how should I convert string lua floating point string to number using tonumber?
tonumber('22.31')
--22.310000000000002
tonumber('22.32')
--22.32
tonumber('22.33');
--22.330000000000002
tonumber('22.34');
--22.34
tonumber(22.31)
--same result for number as well
--22.310000000000002
Upvotes: 1
Views: 9275
Reputation: 122383
Most of the floating point numbers can NOT be represented precisely in binary.
Some floating point numbers are precise in binary, like 22.5
, 22.25
, 22.125
, etc. However, the numbers in your examples are not one of these.
So why did some of these like 22.32
or 22.24
are shown without the trailing part while others do? It's only because of the display precision.
string.format('%.20g', tonumber('22.32'))
--> 22.320000000000000284
Upvotes: 4
Reputation: 1156
I just want to understand why 22.31 is converted to bit long number and why not 22.32 or 22.34
There are infinitely many numbers between 0 and 1. If you have a number 0.1
you could always add 1 to the end of it infinite amount of times thus generating infinite amount of numbers.
Not all values between 0 and 1 can be represented by a float since float can not hold infinite amount of data. Float is more of an approximation than an exact value.
You may find this SO answer helpful: https://stackoverflow.com/a/1089026/3586583
Upvotes: 0