snippetjavascriptTip
Check if two numbers are approximately equal in JavaScript
Viewed 0 times
javascriptchecktwoequalapproximatelynumbersare
Problem
We've all seen the classic floating point arithmetic problem in JavaScript, usually demonstrated as
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
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.Solution
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); // trueCode Snippets
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); // trueContext
From 30-seconds-of-code: approximately-equal
Revisions (0)
No revisions yet.