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.
Floatis not an endless container. Consider this example:Or, another case:
...while
1e+35is just1with35zeroes. Original number (9999...) is so large and precise thatJSstarts cuttinglower digitsto store at least something - the source is too big to save infloat.This actually happens because of internal
floatconversions made by JavaScript engine and the philosophy offloattype is that higher digits are more important that lower.Your case is somewhat similar. This is because floating point type
accuracydepends 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
floatand never compare it with other values using '==' of '===' - it may be anything.