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.
- Calculate the absolute difference between
aandb. - Return
trueif the difference is smaller than0.0001. - Return
falseotherwise.
01EXAMPLE 1
Input
a = 0.300000004, b = 0.3Output
trueExplanation: 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
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
a0.3000004
b0.3
Expected Output
true
Click RUN to test your solution