Skip to content

Home

Check if two numbers are approximately equal in JavaScript

We've all seen the classic floating point arithmetic problem in JavaScript, usually demonstrated as 0.1 + 0.2 === 0.3 returning false. This is due to the limited precision of floating point numbers, and is a common problem in many programming languages.

In many cases, not limited to precision issues, you might want to check if two numbers are approximately equal to each other. Generally speaking, the straightforward way to do this is to use Math.abs() to compare the absolute difference of the two numbers to a certain epsilon value.

const approximatelyEqual = (v1, v2, epsilon = 0.001) =>
  Math.abs(v1 - v2) < epsilon;

approximatelyEqual(0.1 + 0.2, 0.3); // true
approximatelyEqual(Math.PI / 2.0, 1.5708); // true

More like this

Start typing a keyphrase to see matching snippets.