admin管理员组文章数量:1277401
I'm trying to get the remainder of a large number, for example:
1551690021432628813 % 64
But I find that the it's a couple of digits too long for JavaScript. i.e. it's getting rounded to zero.
Is there a way around this other than using a 26kb library like BigInteger.js?
I'm trying to get the remainder of a large number, for example:
1551690021432628813 % 64
But I find that the it's a couple of digits too long for JavaScript. i.e. it's getting rounded to zero.
Is there a way around this other than using a 26kb library like BigInteger.js?
Share Improve this question asked Jul 6, 2017 at 11:58 Sean HSean H 1,0661 gold badge15 silver badges34 bronze badges 2-
How are you even representing
1551690021432628813
? As a string? – John Coleman Commented Jul 6, 2017 at 14:46 - @JohnColeman yes – Sean H Commented Jul 6, 2017 at 15:05
6 Answers
Reset to default 4thank you John Coleman
javascript version :
function calculateMod(str, mod) {
var n = str.length;
if (n <= 10) {
return parseInt(str) % mod;
}
else {
var first = str.substring(0, n - 10)
var second = str.substring(n - 10)
return (calculateMod(first, mod) * (Math.pow(10, 10) % mod) + parseInt(second) % mod) % mod;
}
}
You could break the number into chunks of 10 digits (from the right) and do the modular arithmetic on the chunks, bining the result at the end:
1551690021432628813 = 155169002 * 10**10 + 1432628813
Hence
1551690021432628813 % 64 = (155169002 % 64 * (10**10) % 64 + 1432628813 % 64) % 64
(Which equals 13
).
You could write a recursive function that implements this idea. The following is in Python (which I am more fluent in) but should be easily translated into JavaScript:
def remainder(s,m):
#putes int(s) % m, while just using small numbers
#s is a string and m is an integer
n = len(s)
if n <= 10:
return int(s) % m
else:
first = s[:n-10] #first n-10 digits in s
second = s[-10:] #last 10 digits
return (remainder(first,m) * ((10**10) % m) + int(second) % m) % m
For the special case that the modulus is 64
, there is an exceptionally easy approach: 64 divides 10**6
so, absolutely always
n % 64 == (last 6 digits of n) % 64
For example,
1551690021432628813 % 64 = 628813 % 64 = 13
Similar remarks hold whenever the modulus is a power of 2.
You'd have to use a library (like that one you found). JavaScript's numbers (IEEE-754 double-precision binary floating point) are just not accurate at that scale, and those are the only kind of number JavaScript has*. Once you get past Number.MAX_SAFE_INTEGER + 1
(9,007,199,254,740,992), JavaScript's numbers cannot represent every integer anymore (for instance, can't represent 9,007,199,254,740,993).
* Other than the element type of a typed array, but those don't help you with this for two reasons: 1. There's no typed array for Uint64
, the biggest integer is Uint32
, and 2. Once you're performing math operations on the entry, you're converting it to standard number.
// you can use the built-in BigInt object
console.log(Number(1551690021432628813n % 64n));
There are some workarounds, but generally it is easiest to use a library Read more
BigInt(1551690021432628813) % 64
本文标签: mathDivision and remainder of large numbers in JavaScriptStack Overflow
版权声明:本文标题:math - Division and remainder of large numbers in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741277109a2369792.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论