I was making a calculator (something like excel in javascript) and I have found a strange behavior with ParseFloat.
parseFloat(999999999999999) //999999999999999
parseFloat(9999999999999999) //10000000000000000
parseFloat(9999999999999899) //9999999999999900
Is there a limit with parseFloat function in javascript? Following ECMA Standard there is no issue about this.
Float
is not an endless container. Consider this example:Or, another case:
...while
1e+35
is just1
with35
zeroes. Original number (9999...
) is so large and precise thatJS
starts cuttinglower digits
to store at least something - the source is too big to save infloat
.This actually happens because of internal
float
conversions made by JavaScript engine and the philosophy offloat
type is that higher digits are more important that lower.Your case is somewhat similar. This is because floating point type
accuracy
depends on its value length. So, If your value is too big or too small, you will lose precision for lower digits.Thus you should never trust
float
and never compare it with other values using '==' of '===' - it may be anything.