c# - 将小数提高到小数的幂?

标签 c# bigdecimal decimal

.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/

相关文章:

c# - 从 Excel 复制到 DataGridView C#

c# - 无法使用 WebResponse c# 访问此页面

java - 将 BigDecimal 舍入到最接近的整数值

javascript - 如何在 JavaScript 中使用货币和相对于语言环境来格式化一个大数字(由字符串表示)?

java - 如何格式化货币金额中的小数点后两位?

c# - 线程和委托(delegate)——我不完全理解它们的关系

c# - 需要帮助序列化/反序列化这个 JSON

java - 删除前导 0 直至小数点

ruby - 十进制与任何其他 n 进制数字系统之间的相互转换

c++ - 加密数字 C++