Question Bank
/

Floating Point and Precision

Floating Point and Precision

Hard drills on IEEE-754 representation, equality pitfalls, accumulation drift, and safe comparison patterns. Includes one Kahan-summation walk and a 0.1 + 0.2 trace.

Question Bank
Hard
JavaScript
js-number-precision
math
interview-prep
fundamentals

1,027 views

13

Predict the output and explain why IEEE-754 cannot represent 0.1 or 0.2 exactly. What is a safer equality check for two Number values?

Examples

Example 1:

Input: evaluate 0.1 + 0.2 === 0.3 and (0.1 + 0.2).toFixed(20)
Output: false; '0.30000000000000004441'
Explanation: Binary floats can only represent fractions whose denominator is a power of 2. 0.1 is a repeating binary fraction so it rounds to the nearest representable double. The two rounding errors do not cancel. Safer check: Math.abs(a - b) < eps.

5 more questions, with full solutions and explanations, are available for premium members.

Upgrade to Premium