Computing exponentiation with BigInteger with float/double exponent in C#/.net

Viewed 16

C#/.NET has the BigInteger class that provides methods for exponentiation and logarithms on arbitrary precision integers.

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);

How does one compute BigInteger.Pow(x, y) where y is a float or double type? It looks like there is no built-in method in the class library. Is there an algorithm one can implement for this?

1 Answers

You can rewrite something like this :

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);
    }
}
Related