Math.Pow() 与 Math.Exp() C# .Net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18367240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Math.Pow() vs Math.Exp() C# .Net
提问by CraigJSte
Can anyone provide an explanation of the difference between using Math.Pow()
and Math.Exp()
in C# and .net ?
任何人都可以解释在 C# 和 .net 中使用Math.Pow()
和之间的区别Math.Exp()
吗?
Is Exp()
just taking a number to the Power using itself as the Exponent?
难道Exp()
只是采取了一系列利用自身作为指数的权力?
采纳答案by p.s.w.g
Math.Pow
computes xyfor some xand y.
Math.Pow
单位计算X Ÿ一些X和ÿ。
Math.Exp
computes exfor some x, where eis 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
:
请注意,虽然Math.Pow(Math.E, d)
产生与 相同的结果Math.Exp(d)
,但快速基准比较表明,Math.Exp
实际执行速度大约是 的两倍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
回答by King King
回答by Timothy Shields
Math.Exp(x)
is ex. (See http://en.wikipedia.org/wiki/E_(mathematical_constant).)
Math.Exp(x)
是 e x。(参见http://en.wikipedia.org/wiki/E_(mathematical_constant)。)
Math.Pow(a, b)
is ab.
Math.Pow(a, b)
是一个b。
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.
Math.Pow(Math.E, x)
并且Math.Exp(x)
是相同的,尽管如果您使用 e 作为基础,则第二个是惯用的。
回答by Truckee Tinker
Just a quick extension to the Benchmark contribution from p.s.w.g -
只是对来自 pswg 的 Benchmark 贡献的快速扩展 -
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);}
我想再看一次比较,相当于 10^x ==> e^(x * ln(10)),或者 {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?
我不明白的是为什么在循环中包含乘法的时间,在进入 之前Exp()
,始终会产生更短的时间,除非此代码中存在错误,或者算法依赖于值?
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...
所以我想我会Math.Exp(x * ln10)
一直坚持,直到有人发现这个错误......