0

The title says it all why does 40000000000000000 - 3 = 40000000000000000 in JavaScript? The correct answer should be 39999999999999997

pizzarob
  • 11,711
  • 6
  • 48
  • 69
  • 2
    It is probably using floating point numbers – Ed Heal Jan 20 '18 at 08:50
  • 1
    Is that approximation not good enough for your purposes? It's like 99.99999999999997% correct, you need more precision? :) – connexo Jan 20 '18 at 08:50
  • @connexo unit testing my ethereum contract! 40000000000000000 wei === 0.04 ether. 40000000000000000 - 3 should break one of my tests, but wasn't! The reason being its the same number after subtracting 3. TIL – pizzarob Jan 20 '18 at 08:55
  • @connexo, i have a calculator.It's best for you to learn how to work with it – Ehsan Jan 20 '18 at 09:07
  • The duplicated post mark by @trincot is incorrect, realseanp needs to know why it does not work, not how to perform the calculation. – e-info128 Jan 20 '18 at 09:27
  • 1
    @e-info128, I quote the first line of the accepted answer: *"Because numbers in JavaScript are floating-point. They have limited precision."*. – trincot Jan 20 '18 at 09:30

0 Answers0