Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Math.pow( ) return Infinity?

i have problems with Math.pow in c# return infinity for example :

  double a=65;
  double b=331;
  Console.Write(Math.Pow(a,b));
  //but return infinty 

but calculator for my pc not return 65 ^ 331 infinity there are real number return this :1.1866456424809823888425970808655e+600

i use cast to (long) but the result not same windows calculator please i need the variable type is return same window calculator

like image 248
abddulrahman_ab Avatar asked Nov 22 '25 11:11

abddulrahman_ab


2 Answers

double has a limited range; it cannot store this number.

Use BigInteger.

like image 102
SLaks Avatar answered Nov 25 '25 05:11

SLaks


Math.pow has a limited range it can function with. See the MSDN docs for more details.

I'm not sure why you're trying to calculate 65^331. One work-around would be to use the BigInteger class:

BigInteger result = new BigInteger(Math.pow(65, 331))

If that doesn't work, there's always good ol' fashioned multiplication:

BigInteger product = 1;
BigInteger a = 65;

for (int i = 0; i < 331; i++) {
    product = BigInteger.Multiply(product, a);
}

This should return the value you want.

like image 21
ashes999 Avatar answered Nov 25 '25 03:11

ashes999



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!