开发者

Evaluating a formula with exponents

开发者 https://www.devze.com 2023-03-11 02:55 出处:网络
I am trying to replicate the fo开发者_StackOverflow社区llowing formula from Excel in to a C# app and the result is different.

I am trying to replicate the fo开发者_StackOverflow社区llowing formula from Excel in to a C# app and the result is different.

The answer where x=5 should be y=55.249875 which I have just done using Windows calculator and matches to the Excel answer.. but not when I try it in C#.

For E I use Math.Exp and for x^y I use Math.Pow().

Any ideas?

Formula:

y = -1E-06x^6 + 0.0001x^5 - 0.0025x^4 + 0.0179x^3 + 0.0924x^2 - 0.6204x + 55.07


This would be:

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;
}

Here is a fully working test program to demonstrate:

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();
    }
}

I think the confusion was that you were assuming that -1E-06 required Math.Exp, but it does not. This is just a simple number in Scientific Notation.


E is scientific notation and so base 10. Math.Exp is natural exponentiation, i.e. e^x.

Instead of writing -Math.Exp(-06)*Math.Pos(x, 6) you simply write -1E-06*Math.Pow(x, 6).


For E i use Math.Exp

There is your problem. -1E-06 is the numeric literal -1 * 10 ^ -6 (i.e -0.000001), it's not -1 * e ^ -6.


Try this:

Func<double, double> calc = x => -1E-06d*Math.Pow(x, 6)
                                    + 0.0001d*Math.Pow(x, 5)
                                    - 0.0025d*Math.Pow(x, 4)
                                    + 0.0179d*Math.Pow(x, 3)
                                    + 0.0924d*Math.Pow(x, 2)
                                    - 0.6204d*x
                                    + 55.07;
var y = calc(5);
Console.Out.WriteLine("y = {0}", y);


In both Excel and C#, I get 61.4538750 for X=5.

Here's the C# code:

class Program
    {
        static void Main(string[] args)
        {
            // -1E-06x^6 + 0.0001x^5 - 0.0025x^4 + 0.0179x^3 + 0.0924x^2 - 0.6204x + 55.07
            var done = false;
            while(!done)
            {
                double x;
                if (!Prompt("X> ", out x, out done))
                {
                    if (done) break;
                }
                var sum = Calculate(x);
                Console.WriteLine("Result = {0}", sum);
            }



#if DEBUG
            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
#endif
        }

        private static double Calculate(double x)
        {
            Console.WriteLine("Calculating -1E-06x^6 + 0.0001x^5 - 0.0025x^4 + 0.0179x^3 + 0.0924x^2 - 0.6204x + 55.07");
            var coefficients = new double[] { -1e-6, +1e-4,-2.5e-3,+1.79e-2,9.24e-2,6.204e-1, 5.507e1 };
            var powers = new double[] { 6,5,4,3,2,1,0 };

            var sum = 0.0;
            for(var i=0;i<coefficients.Length;i++)
            {
                var termValue = coefficients[i] * Math.Pow(x, powers[i]);
                sum += termValue;
                Console.WriteLine("Sum [{0}x^{1}] = {2}", coefficients[i],powers[i],termValue);
            }

            return sum;

            //var result = -1E-6*Math.Pow(x,6) + 1E-4*Math.Pow(x,5) - 2.5E-4*Math.Pow(x,4) + 1.79E-2*Math.Pow(x,3)
        }

        static bool Prompt(string prompt, out double value, out bool done)
        {
            done=false;
            var validInput = false;

            Console.Write("X> ");
            var xString = string.Empty;
            if(!(validInput = double.TryParse(xString = Console.ReadLine(),out value)))
            {
                done = xString.ToLower()=="quit";
            }

            return validInput;

        }
0

精彩评论

暂无评论...
验证码 换一张
取 消