peterolson / BigInteger.js

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

NativeBigInt #212

Closed username1565 closed 3 years ago

username1565 commented 3 years ago

Hello. I see you added the supporting for NativeBigInt. I want to say, you, this is supporting .toString() method,

console.log(new bigInt(100500n.toString())); //BigInt-object

and old bigInt object was working good.

To return a native BigInt, there is enough to make

console.log(eval(new bigInt('100500').toString()+'n')); //100500n

Maybe, you can minify bigint library, using this.

Also, I see a some bug, and maybe, you can fix it:

console.log(new bigInt(1005000000000000000000000).toString()); //---> "1004999999999999964348416"

Best regards.

peterolson commented 3 years ago

The problem is that you should put the number inside a string, like this:

console.log(new bigInt("1005000000000000000000000").toString()); //---> "1005000000000000000000000"

This is because 1005000000000000000000000 is too large to preserve precision as a native JavaScript number.

username1565 commented 3 years ago

@peterolson, Look at this:

new bigInt('1005010000000000000000').prev().toString('1005010000000000000000') //"<1.00501e+21>"

P.S.: function toBase(), contains .toJSNumber(), so large values is invalid and are exponential:

        //MAX_INT = 9007199254740992,
        //MAX_INT = Number.MAX_SAFE_INTEGER, //= 9007199254740991
        //...
        if (base.toJSNumber() > MAX_INT) {
            throw new Error("Base too high: (base.toJSNumber() > MAX_INT): "+(base.toJSNumber() > MAX_INT)+'. Maximum is: '+MAX_INT);
        }
peterolson commented 3 years ago

By design bases that are too large are not supported.

username1565 commented 3 years ago

Ok. Look at this https://github.com/peterolson/BigInteger.js/blob/2c922709216d64b499bee1eda3172c3bb1b8fca8/BigInteger.js#L1192

this always true, because:

var x = undefined;
console.log(!x); //true;

so

(ceseSencetive !== undefined && !caseSencetive)

or just (caseSencetive==!1) will works, there. or

(
    caseSensitive !== undefined                 //if defined, because (!undefined === true)
&&  !caseSensitive                          //and if not sencetive for the case
&&  (new Set(alphabet.toLowerCase()).size === alphabet.length)  //and if all lower-cased characters are unique
)

Also, you can look on my changes, in the file BigInteger.js, at your free time: https://github.com/peterolson/BigInteger.js/compare/master...username1565:master