Bigint Issue Of Javascript
I want to find a sum of all integers of a large number for example power(99,95) I applied bigInt to get the power of large number and sum it together by below code. BigInt(Math.pow
Solution 1:
The expression Math.pow(99, 95), when it resolves, has already lost precision - casting it to a BigInt after the fact does not recover the lost precision.
Use BigInts from the beginning instead, and use ** instead of Math.pow so that the exponentiation works:
console.log(
(99n ** 95n)
.toString()
.split('')
.reduce((a, b) => a + Number(b), 0)
);Solution 2:
BigInt(Math.pow(99,95))
This runs math pow on 2 floats, then converts it to bigint.
You want BigInt(99) ** BigInt(95) instead
Post a Comment for "Bigint Issue Of Javascript"