使用指数评估公式

ITM*_*Ltd 2 c# math formula

我试图将以下公式从Excel复制到C#应用程序,结果是不同的.

答案x=5应该是y=55.249875我刚刚使用Windows计算器完成并匹配Excel答案..但是当我在C#中尝试时却没有.

因为E我使用Math.Expx^y我使用Math.Pow().

有任何想法吗?

式:

y = -1E-06x^6 + 0.0001x^5 - 0.0025x^4 + 0.0179x^3 + 0.0924x^2 - 0.6204x + 55.07
Run Code Online (Sandbox Code Playgroud)

Ree*_*sey 9

这将是:

static double Compute(double x)
{
    return -1E-06 * Math.Pow(x, 6) 
        + 0.0001 * Math.Pow(x, 5) 
        - 0.0025 * Math.Pow(x, 4) 
        + 0.0179 * Math.Pow(x, 3) 
        + 0.0924 * Math.Pow(x, 2)
        - 0.6204 * x + 55.07;
}
Run Code Online (Sandbox Code Playgroud)

这是一个完整的测试程序,用于演示:

using System;
class Test
{
    static double Compute(double x)
    {
        return -1E-06 * Math.Pow(x, 6) 
            + 0.0001 * Math.Pow(x, 5) 
            - 0.0025 * Math.Pow(x, 4) 
            + 0.0179 * Math.Pow(x, 3) 
            + 0.0924 * Math.Pow(x, 2)
            - 0.6204 * x + 55.07;
    }

    static void Main()
    {
        Console.WriteLine("Value for x {0} == {1}", 5, Compute(5));
        Console.ReadKey();
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为令人困惑的是你假设需要-1E-06 Math.Exp,但事实并非如此.这只是科学记法中的一个简单数字.


Dav*_*nan 8

E是科学记数法,所以基数10. Math.Exp是自然幂数,即e^x.

而不是写-Math.Exp(-06)*Math.Pos(x, 6)你只是写-1E-06*Math.Pow(x, 6).