Precision Threshold

HardAcc. 56.7%
+55 XP 20

The Floating Point Trap

In JavaScript, 0.1 + 0.2 is actually 0.30000000000000004. This means (0.1 + 0.2) === 0.3 is false!

To compare decimals safely, professionals check if the difference between two numbers is smaller than a tiny threshold, often called EPSILON.

The Assignment

Your function receives numbers a and b.

  1. Calculate the absolute difference between a and b.
  2. Return true if the difference is smaller than 0.0001.
  3. Return false otherwise.

01EXAMPLE 1

Inputa = 0.300000004, b = 0.3
Outputtrue

Explanation: The difference is smaller than the 0.0001 threshold.

Constraints

  • Use Math.abs() to find the difference.
  • Use a threshold of 0.0001.
MathFloating PointProfessional
JavaScript
Loading...
3 Hidden

Input Arguments

a0.3000004
b0.3

Expected Output

true

Click RUN to test your solution