How does js judge whether the decimals are equal?
This article mainly introduces js how to judge whether the decimals are equal, the article is very detailed, has a certain reference value, interested friends must read it!
Judge whether the decimals are equal or not
Function epsEqu {return Math.abs (x-y) < Math.pow (2,-53);} / / example 0.1 + 0.2 = 0.3 / / falseepsEuq (0.1 + 0.2,0.3) / / true copy the code
The user enters decimal numbers and the computer saves binary numbers. So there will be some deviation in the calculation results, so we should not directly compare the non-integers, but take the upper limit and take the error into account. Such an upper limit is called machine epsilon, and the standard epsilon value for double precision is 2 ^-53 (Math.pow (2,-53)).
The above is all the contents of the article "how to judge whether decimals are equal by js". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!