4

I can't figure out the reason 100000000000000030/10 = 10000000000000004 in JavaScript, I haven't seen this problem in other questions.

  <script>
    alert(100000000000000030/10);
  </script>
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Ala Abid
  • 2,256
  • 23
  • 35
  • 1
    Just curious: in which context are you using this calculation? Because that's a _really_ big number. Can you show us the code around it please? – Virginia Dec 14 '18 at 19:44
  • 1
    "Computer math is to math what computer music is to music." Maybe write some code that demonstrates this question. Without a [mcve] this question isn't complete. –  Dec 14 '18 at 19:44
  • 3
    Javascript uses double-precision floating point for numbers. You're bound to encounter some anomalies like this. – Robert Harvey Dec 14 '18 at 19:44
  • There are JS math libraries out there which remove most of the inaccuracies and limits (e.g max number). I wouldn't recommend to use vanilla JS for math that requires absolute precision. – Solo Dec 14 '18 at 19:48
  • You also get the wrong result for `100000000000000030 + 1` or `100000000000000030 - 1`. Or try `100000000000000030 + 9` for more fun. – Herohtar Dec 14 '18 at 19:48
  • @jdv Fixed that for you. – Robert Harvey Dec 14 '18 at 19:49
  • @Virginia It's a problem that involves large numbers in a competitive programming site, i know JS isn't best for that, but I'm simply trying. – Ala Abid Dec 14 '18 at 19:49
  • Sounds interesting, keep going! Hopefully the answer in the other thread can help you out. – Virginia Dec 14 '18 at 19:51
  • This is the wrong duplicate (but it is somewhat related). Your are using a number that is larger than `Number.MAX_SAFE_INTEGER`. JavaScript "integers" only have 53 bits of precision. Closest question I found for now: https://stackoverflow.com/q/10636570/218196 – Felix Kling Dec 14 '18 at 19:52
  • @RobertHarvey heh. Well, we didn't _really_ know if the example provided was vanilla, or using some weirdo library. With package managers going viral you just never know what code someone has borrowed from the internet is at the root of a question without actually looking at it. –  Dec 14 '18 at 19:52
  • @Solo: *"I wouldn't recommend to use vanilla JS for math that requires absolute precision."* Really depends on the numbers you have to work with. – Felix Kling Dec 14 '18 at 19:55
  • Lots of related questions that would answer yours: https://stackoverflow.com/search?q=Number.MAX_SAFE_INTEGER – Felix Kling Dec 14 '18 at 19:58

1 Answers1

4

Before doing this calculation you may want to know that whether this number is a safe javascript number or not. you can use - Number.isSafeInteger(100000000000000030), to know that.

"The MAX_SAFE_INTEGER constant has a value of 9007199254740991 (9,007,199,254,740,991 or ~9 quadrillion). The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent numbers between -(253 - 1) and 253 - 1."

You may also want to read more here - Division and remainder of large numbers in JavaScript

Gaurav
  • 623
  • 5
  • 11