提高小数为十进制的力量? [英] Raising a decimal to a power of decimal?

查看:156
本文介绍了提高小数为十进制的力量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.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).

这篇关于提高小数为十进制的力量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆