Why does `0.1 + 0.2` produce `0.30000000000000004`? Both decimal fractions repeat in binary, so storing each in a 52-bit significand first requires rounding. Addition aligns their exponents, then normalizes and rounds the result again. The discrepancy is a consequence of binary floating-point representation, not a JavaScript-specific bug. section 3.1.1
Table 3.3 Representing 0.1 and 0.2 as floating-point numbers
|
Value
|
Sign
|
Exponent
|
Significand
|
|
0.1
|
0
|
01111111011
|
1.1001100110011001100110011001100110011001100110011010
|
|
0.2
|
0
|
01111111100
|
1.1001100110011001100110011001100110011001100110011010
|
Compare the exponents and repeating significand patterns stored for 0.1 and 0.2. section 3.1.1
Rounding also appears explicitly in `Math.round(n)` (nearest integer), `Math.floor(n)` (down), and `Math.ceil(n)` (up). These operations differ from the rounding needed to fit a result into a floating-point significand. For approximate comparisons, `Number.EPSILON` provides a reference near 1; a comparison can scale it by the magnitudes being compared. section 3.1.1






