.net 框架在 Math 类中提供了一种为 double 供电的方法。但是根据精度要求,我需要将小数提高到小数幂 [ Pow(decimal a, decimal b) ]。框架有这样的功能吗?有谁知道有这种功能的库吗?
最佳答案
为了解决我的问题,我找到了一些 expansion series ,我让它们实现来求解方程 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));
}
Pow() 和 Factorial() 函数很简单,因为幂始终是一个 int(在 de 幂级数内)。
关于c# - 将小数提高到小数的幂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/429165/