Question

Can anyone provide an explanation of the difference between using Math.Pow() and Math.Exp() in C# and .net ?

Is Exp()just taking a number to the Power using itself as the Exponent?

Was it helpful?

Solution

Math.Pow computes x y for some x and y.

Math.Exp computes e x for some x, where e is Euler's number.

Note that while Math.Pow(Math.E, d) produces the same result as Math.Exp(d), a quick benchmark comparison shows that Math.Exp actually executes about twice as fast as Math.Pow:

Trial Operations       Pow       Exp
    1       1000 0.0002037 0.0001344 (seconds)
    2     100000 0.0106623 0.0046347 
    3   10000000 1.0892492 0.4677785 

OTHER TIPS

Math.Pow(Math.E,n) = Math.Exp(n)  //of course this is not actual code, just a human equation.

More info: Math.Pow and Math.Exp

Math.Exp(x) is ex. (See http://en.wikipedia.org/wiki/E_(mathematical_constant).)

Math.Pow(a, b) is ab.

Math.Pow(Math.E, x) and Math.Exp(x) are the same, though the second one is the idiomatic one to use if you are using e as the base.

Just a quick extension to the Benchmark contribution from p.s.w.g -

I wanted to see one more comparison, for equivalent of 10^x ==> e^(x * ln(10)), or {double ln10 = Math.Log(10.0); y = Math.Exp(x * ln10);}

Here's what I've got:

Operation           Time
Math.Exp(x)         180 ns (nanoseconds)
Math.Pow(y, x)      440 ns
Math.Exp(x*ln10)    160 ns

Times are per 10x calls to Math functions.

What I don't understand is why the time for including a multiply in the loop, before entry to Exp(), consistently produces shorter times, unless there's a bug in this code, or the algorithm is value dependent?

The program follows.

namespace _10X {
    public partial class Form1 : Form {
        int nLoops = 1000000;
        int ix;

        // Values - Just to not always use the same number, and to confirm values.
        double[] x = { 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5 };

        public Form1() {
            InitializeComponent();
            Proc();
        }

        void Proc() {
            double y;
            long t0;
            double t1, t2, t3;

            t0 = DateTime.Now.Ticks;
            for (int i = 0; i < nLoops; i++) {
                for (ix = 0; ix < x.Length; ix++)
                    y = Math.Exp(x[ix]);
            }
            t1 = (double)(DateTime.Now.Ticks - t0) * 1e-7 / (double)nLoops;

            t0 = DateTime.Now.Ticks;
            for (int i = 0; i < nLoops; i++) {
                for (ix = 0; ix < x.Length; ix++)
                    y = Math.Pow(10.0, x[ix]);
            }
            t2 = (double)(DateTime.Now.Ticks - t0) * 1e-7 / (double)nLoops;

            double ln10 = Math.Log(10.0);
            t0 = DateTime.Now.Ticks;
            for (int i = 0; i < nLoops; i++) {
                for (ix = 0; ix < x.Length; ix++)
                    y = Math.Exp(x[ix] * ln10);
            }
            t3 = (double)(DateTime.Now.Ticks - t0) * 1e-7 / (double)nLoops;

            textBox1.Text = "t1 = " + t1.ToString("F8") + "\r\nt2 = " + t2.ToString("F8")
                        + "\r\nt3 = " + t3.ToString("F8");
        }

        private void btnGo_Click(object sender, EventArgs e) {
            textBox1.Clear();
            Proc();
        }
    }
}

So I think I'm going with Math.Exp(x * ln10) until someone finds the bug...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top