Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell calculating incorrect value for large bigint numbers

[bigint][math]::pow(10,22) 

Gives the correct result: 10000000000000000000000

[bigint][math]::pow(10,23) 

Gives an incorrect result: 100000000000000008388608

[bigint]10e23

Gives a different but also incorrect result: 999999999999999983222784

I thought that System.Numerics.biginteger worked for arbitrarily large integers?

Is there something special that needs to be done to correctly calculate arbitrarily large numbers in Powershell, or a different method that must be used instead?

like image 456
Heltrix Avatar asked Feb 23 '26 22:02

Heltrix


1 Answers

The pow function takes double type arguments, and returns a double type result. Your int inputs are implicitly cast to double, and the double result is cast to bigint as best as able.

Math.Pow Method documentation

Also see this response to similar thing reported as bug. In that bug report, they suggest a function definition (including code) that will compute pow for big integer values.

like image 135
hatchet - done with SOverflow Avatar answered Feb 26 '26 11:02

hatchet - done with SOverflow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!