0

When I run node in the terminal, and run the following, I get this weird bug:

> 48112959837082048697 % 2
0
> 4811295983708204869 % 2
0
> 4811295983708204869 / 2
2405647991854102500
> 2405647991854102500 + 2405647991854102500
4811295983708205000
> 

It seems to only effect some numbers. Am I doing something wrong or misunderstanding modular?

user2865156
  • 291
  • 8
  • 14
  • 2
    That's a result of floating point arithmetic, see http://www.ecma-international.org/ecma-262/5.1/#sec-11.5.3. – Zeta Feb 09 '14 at 21:35
  • Thanks. That was quite a read so I just google "javascript floating point arithmetic" which lead me to http://stackoverflow.com/questions/588004/is-javascripts-floating-point-math-broken so you put me on the right path. – user2865156 Feb 09 '14 at 21:41

1 Answers1

0

4811295983708204869 is too large and can't be represented as is. So when you write it, javascript rounds it up to 4811295983708205000, which is an even number.

If you don't want that to happen, you probably need to use one of the bignumber libraries.

alex
  • 11,935
  • 3
  • 30
  • 42