0
let x = -75;
let b = -7.105427357601002e-15;
x = x + b;
alert(x);

Alert prints -75. Why? What is the solution to avoid this error?

c3267738
  • 19
  • 5
  • 3
    You've basically said -75 + 0. That number is so small it's essentially 0. Do you actually need precision to 15 decimal places? – Carcigenicate Oct 30 '19 at 19:09
  • 1
    The number you are adding is too small, JavaScript can't calculate it with its vanilla libraries. Search for some BigDecimal library. – Arthur Lyrio de Oliveira Oct 30 '19 at 19:11
  • Possible duplicate of [Dealing with float precision in Javascript](https://stackoverflow.com/questions/11695618/dealing-with-float-precision-in-javascript) – devlin carnate Oct 30 '19 at 19:26

0 Answers0