Math.Pow () vs Math.Exp () C # .Net

Can someone explain the difference between using Math.Pow() and Math.Exp() in C # and .net?

Is Exp() just taking a number in Power, using itself as an exponent?

+4
source share
4 answers

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

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

Note that while Math.Pow(Math.E, d) gives the same result as Math.Exp(d) , a quick comparative comparison shows that Math.Exp actually performs about two times faster than 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 
+35
source
 Math.Pow(Math.E,n) = Math.Exp(n) //of course this is not actual code, just a human equation. 

Additional Information: Math.Pow and Math.Exp

+4
source

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

Math.Pow(a, b) is b .

Math.Pow(Math.E, x) and Math.Exp(x) same, although the second is idiomatic if you use e as the base.

+4
source

Just a swift expansion of pswg's Benchmark contribution -

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

Here is what I have:

 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. 

I don’t understand why the time to turn on multiplication in the loop before entering Exp() sequentially leads to a reduction in time if there is no error in this code or the algorithm depends on the value?

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 go with Math.Exp(x * ln10) until someone finds an error ...

0
source

Source: https://habr.com/ru/post/1498269/


All Articles