peterolson / BigInteger.js

An arbitrary length integer library for Javascript
The Unlicense
1.12k stars 187 forks source link

Lock up issues and no calculation OR RangeError: Maximum BigInt size exceeded #236

Closed ganeshkbhat closed 1 year ago

ganeshkbhat commented 1 year ago

I am trying this var n = bigInt("3").pow("300530164787").minus("3").isDivisibleBy("300530164787"); and var n = bigInt("3").pow("3005301647874556767787878788787").minus("3").isDivisibleBy("3005301647874556767787878788787");and the node console is all locked up.

I am expecting the above to check if isDivisibleBy(value) returns an absolute integer and not a float. 300530164787%1 === 0 and 300530164787.234%1 === 0.234 or value%1 === 0

It also throws an error :

return new NativeBigInt(this.value * parseValue(v).value);
                                           ^

RangeError: Maximum BigInt size exceeded
    at NativeBigInt.multiply (C:\Users\GB\Documents\projects\requireurl\bigint.js:398:44)
    at NativeBigInt.pow (C:\Users\GB\Documents\projects\requireurl\bigint.js:670:23)
    at Object.<anonymous> (C:\Users\GB\Documents\projects\requireurl\bigint.js:1455:21)
    at Module._compile (node:internal/modules/cjs/loader:1149:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1203:10)
    at Module.load (node:internal/modules/cjs/loader:1027:32)
    at Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.10.0
Yaffle commented 1 year ago

It could ve that the number us too large, try modPow

peterolson commented 1 year ago

You probably reached the memory limit. I think the V8 engine will error once you use more than 1 billion bits.

By the way bigInt("3").pow("3005301647874556767787878788787") is a very huge number, I'm not sure that I would expect the calculation to finish within a human lifetime.

As @Yaffle pointed out, if all you want to do is check divisibility, the modPow method should be a lot faster.