The .net framework provides in the Math class a method for powering double. But by precision requirement I need to raise a decimal to a decimal power [ Pow(decimal a, decimal b) ]. Does the framework have such a function? Does anyone know of a library with this kind of function?
Are you sure you actually want to do this? A decimal
multiply is about 40 times slower than double
's, so I'd expect a decimal Math.Pow()
to be practically unusable.
If you expect only integer powers, though, I suggest you use the integer-based power algorithm that was already discussed here on SO.
I think it depends a lot on the number you plan on plugging in. If 'a' and 'b' are not 'nice' number then you'll likely get a value which is non-terminating that is impossible to store and if C# BigDecimal behaves at all like Java BigDecimal it probably throws an exception in such a case.
log(c^d) = d * log(c)
further...
c^d = antilogarithm(d * log(c))
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("169^(1/2) = " + RaiseToPower((169), (.5)));
Console.ReadLine();
}
public static double RaiseToPower(double a, double b)
{
return Math.Exp(b * Math.Log(a));
}
}
}
To solve my problem I found some expansion series, and them I had them implemented to solve the equation X^n = e^(n * ln x).
// power series
int iteration = 27; // Adjust this to modify the precision
decimal result = 1;
while (iteration > 0)
{
fatorial = Factorial(iteration);
result += (Pow(power, iteration) / fatorial);
iteration--;
}
// natural logarithm series
decimal aux = (value - 1);
decimal result = 0;
while (iteration > 0)
{
result += Pow(aux, iteration) / iteration;
iteration--;
}
The Pow() and Factorial() functions are simple because the power is always an int (inside de power series).