How can I fix cannot convert number with >15 digits to a bigNumber while calculating pi?

389 Views Asked by At

The code is:

const c = mult(426880, sqrt(10005))
let calc = bn(0);

for (let k = 0; k < 20; k++) {
     
    const m = div(fac(mult(bn(6), k)), mult(fac(mult(bn(3), k)), pow(fac(k), bn(3))))
    const l = add(mult(bn(545140134), k), bn(13591409))
    const x = pow(-262537412640768000, k)
    
    calc += mult(c, pow(div(mult(m,l), x), bn(-1)))
   console.log("K is equal to: ", k)
}

pi = div(bn(1), calc)

//Printing the pi

console.log(MathJS.string(pi))

The line when I added the values to calc doesn’t work, and says:

“TypeError: Cannot implicitly convert a number with >15 significant digits to BigNumber. Use function bignumber(x) to convert to BigNumber

I think it's the X value, but I can’t figure out how to fix it. I’m using the library Math.Js.

0

There are 0 best solutions below