I’m under the impression that numeric values are stored as floating points. Comparing floats for equality in any other language is unreliable and not recommended. Is there some magic that goes on behind the scenes to make this work reliably for what would otherwise be integers? I can’t find any other reference to this.
I’m under the impression that numeric values are stored as floating points. Comparing floats
Share
You are correct about numbers in JS being floating point. Section 4.3.19 of the language specification says
Floating point comparison for integers works just fine. 64b IEEE-754 can accurately represent any integer with magnitude less than 2 to the 53rd power (see ULP). The problem comes in when you divide, or use
Mathfunctions that have to approximate results.If you do need to coerce a result from a floating point operation to the closest integer, use
Math.round."What Every Computer Scientist Should Know about Floating Point" has a good discussion of rounding error.