You-dont-know-js: Float Subtraction: Unexpected result

Created on 1 Mar 2018  路  2Comments  路  Source: getify/You-Dont-Know-JS

1.00000100-0.00000100 results in 0.9999999999999999.

Why does this happen? How to get correct result?

Most helpful comment

It happens because of IEEE-754, nothing specific to JS. There's no way to get the math exact, but there is a way (covered in the Types & Grammar book) to use what's called the machine epsilon to check that the number you're comparing to is "close enough" to the number that it's considered within this margin of floating point error.

All 2 comments

It happens because of IEEE-754, nothing specific to JS. There's no way to get the math exact, but there is a way (covered in the Types & Grammar book) to use what's called the machine epsilon to check that the number you're comparing to is "close enough" to the number that it's considered within this margin of floating point error.

@getify Thanks for your response.

Was this page helpful?
0 / 5 - 0 ratings