在C#/.net中使用带浮点/双指数的BigInteger计算幂

  • 本文关键字:指数 计算 BigInteger net c# .net
  • 更新时间 :
  • 英文 :


C#/.NET具有BigInteger类,该类提供了对任意精度整数进行幂运算和对数运算的方法。

public static double Log (System.Numerics.BigInteger value);
public static double Log (System.Numerics.BigInteger value);
public static System.Numerics.BigInteger Pow (System.Numerics.BigInteger value, int exponent);

如何计算BigInteger.Pow(x,y(,其中y是float或double类型?类库中似乎没有内置方法。有没有一种算法可以实现?

您可以重写如下内容:

public class something
{
public static void Test()
{
int @base = 10;
double exp = 12345.123;
int intExp = (int)Math.Floor(exp);
double fracExp = exp - intExp;
BigInteger temp = BigInteger.Pow(@base, intExp);
double temp2 = Math.Pow(@base, fracExp);
int fractionBitsForDouble = 52;
for (int i = 0; i < fractionBitsForDouble; i++)
{
temp = BigInteger.Divide(temp, 2);
temp2 *= 2;
}
BigInteger result = BigInteger.Multiply(temp, (BigInteger)temp2);
Console.WriteLine(result);
}
}

最新更新