Floating Point: Why Your Money Is Wrong
Every answer here is decidable by IEEE 754 and ECMA-262. No opinions, no engine trivia. Binary fractions, the lies toFixed tells, negative zero, the gaps above Number.MAX_SAFE_INTEGER, and why summing the same million numbers in a different order gives you a different total.
Questions
- Not answered. What does
0.1 + 0.2evaluate to in JavaScript? - Not answered. Why can a double hold
0.5exactly but not0.1? - Not answered. Which of these strict comparisons evaluates to
true? - Not answered. How far apart are
0.1 + 0.2and0.3on the double number line? - Not answered. Why does
String(0.1)print"0.1"whileString(0.1 + 0.2)prints all 17 digits? - Not answered. What does
(0.1).toFixed(20)return? - Not answered. What string does
(1.005).toFixed(2)return? - Not answered. Which observation proves
toFixedis not using banker's rounding when(1.005).toFixed(2)yields"1.00"? - Not answered. What do these two ways of rounding
1.005to two decimals print in the same runtime? - Not answered. What does
Math.round(1.005 * 100) / 100evaluate to? - Not answered. Which of these expressions evaluate to
true? - Not answered. Which of these expressions produces
-0? - Not answered. Why does this trend indicator report a falling trend for a metric that barely moved?
- Not answered. What do
(-0).toFixed(1)and(-0.04).toFixed(1)return? - Not answered. What is the value of
Number.MAX_SAFE_INTEGER? - Not answered.
2 ** 53is stored exactly, so why isNumber.MAX_SAFE_INTEGERone less than it? - Not answered. Which statements about doubles above
Number.MAX_SAFE_INTEGERare true? - Not answered. What does
JSON.parse('{"id": 1234567890123456789}').idreturn? - Not answered. Summing the same array forwards and backwards gives two different totals. What are they?
- Not answered. Which summation algorithm carries a running compensation term to recover the low-order bits lost at each addition?
- Not answered. In Kahan summation, what does
chold after the linec = (t - sum) - y? - Not answered. Which of these are real reasons to store money in
NUMERIC/DECIMALrather thanDOUBLE PRECISION? - Not answered. What does
Number.EPSILONactually measure? - Not answered. Why does
Math.abs(a - b) < Number.EPSILONaccept the tenths case but reject the millions case? - Not answered. What is
Number.MIN_VALUE? - Not answered. In 32-bit floats, does
0.1 + 0.2equal0.3? - Not answered. Which rounding mode does IEEE 754 apply to arithmetic results by default?
- Not answered. Which of these
NaNexpressions evaluate totrue? - Not answered. How many iterations does
for (let x = 0; x !== 1; x += 0.1)run? - Not answered. What does
parseInt(0.0000005)return? - Not answered. Which of these approaches actually make money arithmetic correct in JavaScript?