简体   繁体   中英

Division and remainder of large numbers in JavaScript

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. ie it's getting rounded to zero.

Is there a way around this other than using a 26kb library like BigInteger.js ?

You could break the number into chunks of 10 digits (from the right) and do the modular arithmetic on the chunks, combining 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):
    #computes 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.

thank 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'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

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