Back to the catalog

Floating point in JavaScript: binary fractions, rounding, and precision limits

31 questions

Why 0.1 + 0.2 isn't 0.3, why toFixed sometimes rounds the wrong way, where negative zero comes from, what happens above Number.MAX_SAFE_INTEGER, and why adding the same numbers in a different order can change the total. Every answer follows from IEEE 754 and ECMA-262.

Questions

  1. Not answered. What does 0.1 + 0.2 evaluate to in JavaScript?
  2. Not answered. Why can a double hold 0.5 exactly but not 0.1?
  3. Not answered. Which of these strict comparisons evaluates to true?
  4. Not answered. How far apart are 0.1 + 0.2 and 0.3 on the double number line?
  5. Not answered. Why does String(0.1) print "0.1" while String(0.1 + 0.2) prints all 17 digits?
  6. Not answered. What does (0.1).toFixed(20) return?
  7. Not answered. What string does (1.005).toFixed(2) return?
  8. Not answered. Which result shows that toFixed doesn't use banker's rounding, given that (1.005).toFixed(2) returns "1.00"?
  9. Not answered. What do these two ways of rounding 1.005 to two decimals print in the same runtime?
  10. Not answered. What does Math.round(1.005 * 100) / 100 evaluate to?
  11. Not answered. Which of these expressions evaluate to true?
  12. Not answered. Which of these expressions produces -0?
  13. Not answered. Why does this trend indicator say "falling" for a metric that barely moved?
  14. Not answered. What do (-0).toFixed(1) and (-0.04).toFixed(1) return?
  15. Not answered. What is the value of Number.MAX_SAFE_INTEGER?
  16. Not answered. 2 ** 53 is stored exactly, so why is Number.MAX_SAFE_INTEGER one less than it?
  17. Not answered. Which statements about doubles above Number.MAX_SAFE_INTEGER are true?
  18. Not answered. What does JSON.parse('{"id": 1234567890123456789}').id return?
  19. Not answered. Summing the same array forwards and backwards gives two different totals. What are they?
  20. Not answered. Which summation algorithm carries a running compensation term to recover the low-order bits lost at each addition?
  21. Not answered. In Kahan summation, what does c hold after the line c = (t - sum) - y?
  22. Not answered. Which of these are real reasons to store money as NUMERIC/DECIMAL instead of DOUBLE PRECISION?
  23. Not answered. What does Number.EPSILON actually measure?
  24. Not answered. Why does Math.abs(a - b) < Number.EPSILON pass for the tenths but fail for the millions?
  25. Not answered. What is Number.MIN_VALUE?
  26. Not answered. In 32-bit floats, does 0.1 + 0.2 equal 0.3?
  27. Not answered. Which rounding mode does IEEE 754 apply to arithmetic results by default?
  28. Not answered. Which of these NaN expressions evaluate to true?
  29. Not answered. How many iterations does for (let x = 0; x !== 1; x += 0.1) run?
  30. Not answered. What does parseInt(0.0000005) return?
  31. Not answered. Which of these approaches make money arithmetic correct in JavaScript?