提高小数为十进制的力量?
在.NET Framework中的数学类提供了双电源的方法。但是,precision要求,我需要提出一个小数为十进制功率[战俘(十进制,十进制B)。该框架是否有这样的功能?有谁有这种功能知道图书馆的?
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?
要解决我的问题,我发现了一些的扩展系列,其中我已经实现他们解决方程x ^ N = E ^(N * LN X)。
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).
// Adjust this to modify the precision
public const int ITERATIONS = 27;
// power series
public static decimal DecimalExp(decimal power)
{
int iteration = ITERATIONS;
decimal result = 1;
while (iteration > 0)
{
fatorial = Factorial(iteration);
result += Pow(power, iteration) / fatorial;
iteration--;
}
return result;
}
// natural logarithm series
public static decimal LogN(decimal number)
{
decimal aux = (number - 1);
decimal result = 0;
int iteration = ITERATIONS;
while (iteration > 0)
{
result += Pow(aux, iteration) / iteration;
iteration--;
}
return result;
}
// example
void main(string[] args)
{
decimal baseValue = 1.75M;
decimal expValue = 1/252M;
decimal result = DecimalExp(expValue * LogN(baseValue));
}
战俘()和阶乘()函数是简单的,因为权力始终是一个int(德幂级数内)。
The Pow() and Factorial() functions are simple because the power is always an int (inside de power series).