简体   繁体   中英

Why the modulo/remainder operator calculated is wrong for large numbers?

I am trying to understand why I am not able to calculate the right modulo using JavaScript. The operation I have tried is:

Wrong answer

28493595674446332150349236018567871332790652257295571471311614363091332152517 % 6 = 4

The result should be 1.

28493595674446332150349236018567871332790652257295571471311614363091332152517 % 6 = 1

I have tried to convert this number to BN but unfortunately I always get the same answer. However if you use wolfram alpha or another math software it returns the right answer.

What's going on? What have I been doing wrong?

The integer number range in JavaScript is +/- 9007199254740991 (Number.MAX_SAFE_INTEGER). Your number is simply out of range for JS.

You can also use the BigInt notation to get the right answer. 28493595674446332150349236018567871332790652257295571471311614363091332152517n % 6n

See What is JavaScript's highest integer value that a number can go to without losing precision?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM