11

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
CraigJSte
  • 912
  • 6
  • 17
  • 33
  • 4
    What part of the documentation was unclear? I am happy to pass on your critique of the documentation to the documentation manager. – Eric Lippert Aug 21 '13 at 20:58
  • In case you didn't know, this is the documentation being referred to: http://msdn.microsoft.com/en-us/. A simple search for "Math.Exp" takes you right to the page where all of this is explained. – Timothy Shields Aug 21 '13 at 21:01
  • 9
    @EricLippert The only possible point of confusion I can see in the documentation is that it simply says "*Returns **e** raised to the specified power*", but it may not be clear what *e* represents to someone who has not encountered it before. Perhaps "*Returns the exponential of the specified number*". Of course, that doesn't do much if the person isn't doesn't know what the term '*exponential of*' means, but it may prompt further research. – p.s.w.g Aug 21 '13 at 22:32
  • Perhaps a resource for such research: http://study.com/academy/lesson/what-is-an-exponential-function.html – Rusty Nail Apr 07 '17 at 21:25

4 Answers4

60

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 
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
5
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

King King
  • 61,710
  • 16
  • 105
  • 130
  • 3
    Though your identity is a correct identity, it's not the *interesting* version of the identity. The interesting version is that z to the w = e to the (w ln z). This allows us to define arbitrary real (or complex!) exponentiation *in terms of* the exp function and its inverse, which have easy definitions. – Eric Lippert Aug 21 '13 at 23:20
  • @EricLippert this is just for `programming`, to understand it deeply the OP may want to search more. I even know the formula to calculate its value, even now they still find more and more the last digits of the `e` number, it requires a `supercomputer` to find, not easy task for us with a normal computer, understanding its nature is fine but not a must-learn thing for some programmers (because they don't use `Math` much in their code, just use `library`:) – King King Aug 22 '13 at 03:22
  • 4
    Finding digits of e is trivial; the formulas for e converge extremely quickly. Moreover, remember that in *factoradic* base, e is by definition 1 + 1.11111111111111111..., so if you want the nth factoradic digit of e to the right of the decimal place, I can tell you right now what it is: it's 1, for any n you care to name! That didn't take a supercomputer. – Eric Lippert Aug 22 '13 at 20:22
4

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.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
  • Thanks Timothy. I guess the definition of the constant was eluding me I still am a little unclear on it. – CraigJSte Aug 21 '13 at 22:03
  • 2
    @CraigJSte: Take a step back and define exponentiation from first principles. We define the function exp(x) as the sum of the infinite series of x to the n divided by n factorial for n from zero to infinity. This only requires real exponentiation with *natural* powers, which is well-defined. The function log(x) is defined as the inverse. pow(x,y) is defined as exp(y log(x)), and hey presto, we have defined real (and complex!) exponentiation using only integer powers and infinite sums. The constant e is simply the name given to exp(1) for convenience. – Eric Lippert Aug 21 '13 at 23:27
  • I am actually getting closer to understand it now, thank you for your efforts. – CraigJSte Aug 21 '13 at 23:33
1

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

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
  • And I apologize in advance for making that whole block . But I spent nearly 1 hour trying to post this with recurring result that "Your post appears to contain code that is not properly formatted" error message. Every time I tried something the line pointed to by the error box changed to somewhere else. Sometimes in the middle of a paragraph, sometimes to different points in the code block. It just got painful. Sorry. – Truckee Tinker Jun 12 '14 at 22:00
  • 1
    This should probably be a new question. – T.C. Jun 12 '14 at 22:31
  • @TruckeeTinker if you have problems with the formatting, take a look at https://stackoverflow.com/editing-help – AliciaBytes Jun 12 '14 at 22:33
  • I'd argue that it does contribute to the answer via the extension to the contribution by p.s.w.g. My goal was only to share the experience that the form Math.Exp(x*ln10) yielded a performance benefit over Math.Pow. – Truckee Tinker Jun 16 '14 at 17:15