you can use Number.EPSILON
The property Number.EPSILON
represents the difference between 1 and the smallest floating point number greater than 1.
and take the absolute delta of the value and the desired value and check if it is less Number.EPSILON
. If true
, then the value error is less than the possible floating point arithmetic error.
console.log([
[Math.sin(Math.PI), 0],
[Math.cos(Math.PI/2), 0],
[Math.sin(3.14), 0.0015926529164868282],
[Math.sin(3.141592), 6.535897930762419e-7]
].map(([a, b]) => Math.abs(a - b) < Number.EPSILON));
Run codeHide result, :