How to check two numbers are approximately equal in JavaScript?

In JavaScript, determining if two floating-point numbers are "approximately equal" is essential because of precision issues with decimal calculations. We compare the absolute difference between two numbers against a tolerance value called epsilon.

Why Use Approximate Equality?

Floating-point arithmetic can produce unexpected results:

console.log(0.1 + 0.2);           // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3);   // false
0.30000000000000004
false

The Algorithm

We calculate the absolute difference between two numbers and compare it with epsilon (tolerance). If the difference is less than epsilon, the numbers are approximately equal.

const difference = Math.abs(val1 - val2);
if (difference 

Basic Implementation

function isApproximatelyEqual(val1, val2, epsilon) {
    const difference = Math.abs(val1 - val2);
    return difference 

true
false
true

Interactive Example

<!DOCTYPE html>
<html>
<body>
    <h2>Approximate Equality Checker</h2>
    <input type="number" id="val1" placeholder="Enter first number" step="any"><br><br>
    <input type="number" id="val2" placeholder="Enter second number" step="any"><br><br>
    <input type="number" id="epsilon" placeholder="Enter epsilon (tolerance)" step="any"><br><br>
    <button onclick="checkApproximateEquality()">Check Equality</button>
    <div id="result"></div>
    
    <script>
        function checkApproximateEquality() {
            const val1 = parseFloat(document.getElementById("val1").value);
            const val2 = parseFloat(document.getElementById("val2").value);
            const epsilon = parseFloat(document.getElementById("epsilon").value) || 0.001;
            
            if (isNaN(val1) || isNaN(val2)) {
                document.getElementById("result").innerHTML = "Please enter valid numbers";
                return;
            }
            
            const difference = Math.abs(val1 - val2);
            const isEqual = difference < epsilon;
            
            document.getElementById("result").innerHTML = `
                <h3>Results:</h3>
                <p>Difference: ${difference.toFixed(6)}</p>
                <p>Epsilon: ${epsilon}</p>
                <p>Result: ${isEqual ? "Approximately Equal" : "Not Approximately Equal"}</p>
            `;
        }
    </script>
</body>
</html>

Enhanced Function with Default Epsilon

function isApproximatelyEqual(val1, val2, epsilon = 0.001) {
    // Handle edge cases
    if (val1 === val2) return true;
    if (!isFinite(val1) || !isFinite(val2)) return false;
    
    const difference = Math.abs(val1 - val2);
    return difference 

true
false
true
false

Common Use Cases

Scenario Suggested Epsilon Example
Currency calculations 0.01 $1.99 vs $2.00
Floating-point math 0.0001 0.1 + 0.2 vs 0.3
Scientific calculations 1e-9 High precision comparisons

Conclusion

Use approximate equality when comparing floating-point numbers to handle precision issues. Choose epsilon based on your precision requirements?smaller values for stricter comparison, larger values for more tolerance.

Updated on: 2026-03-15T23:19:00+05:30

705 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements