C#Math.pow()返回Infinity?

abd*_*_ab 2 c# double casting return floating-point-precision

我在c#return infinity中遇到Math.pow问题,例如:

  double a=65;
  double b=331;
  Console.Write(Math.Pow(a,b));
  //but return infinty 
Run Code Online (Sandbox Code Playgroud)

但我的电脑计算器不返回65 ^ 331无穷大有实数返回此:1.1866456424809823888425970808655e + 600

我使用强制转换为(长)但结果不同于Windows计算器请我需要变量类型返回相同的窗口计算器

SLa*_*aks 6

double具有有限的范围 ; 它无法存储此号码.

使用BigInteger.


ash*_*999 5

Math.pow它的功能范围有限.有关更多详细信息,请参阅MSDN文档.

我不确定你为什么要计算65^331.一种解决方法是使用BigInteger类:

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

如果这不起作用,总会有很好的增长:

BigInteger product = 1;
BigInteger a = 65;

for (int i = 0; i < 331; i++) {
    product = BigInteger.Multiply(product, a);
}
Run Code Online (Sandbox Code Playgroud)

这应该返回您想要的值.