帽子^运算符与Math.Pow() [英] Hat ^ operator vs Math.Pow()
问题描述
仔细阅读了 ^(帽子)运算符和
Having perused the MSDN documentation for both the ^ (hat) operator and the Math.Pow() function, I see no explicit difference. Is there one?
显然有一个区别,一个是函数,而另一个被认为是运算符,例如这将不起作用:
There obviously is the difference that one is a function while the other is considered an operator, e.g. this will not work:
Public Const x As Double = 3
Public Const y As Double = Math.Pow(2, x) ' Fails because of const-ness
但这会:
Public Const x As Double = 3
Public Const y As Double = 2^x
但是它们产生最终结果的方式有所不同吗?例如,Math.Pow()
是否进行更多的安全检查?还是其中一个只是别的别名?
But is there a difference in how they produce the end result? Does Math.Pow()
do more safety checking for example? Or is one just some kind of alias for the other?
推荐答案
一种找出方法是检查IL.对于:
One way to find out is to inspect the IL. For:
Dim x As Double = 3
Dim y As Double = Math.Pow(2, x)
IL是:
IL_0000: nop
IL_0001: ldc.r8 00 00 00 00 00 00 08 40
IL_000A: stloc.0 // x
IL_000B: ldc.r8 00 00 00 00 00 00 00 40
IL_0014: ldloc.0 // x
IL_0015: call System.Math.Pow
IL_001A: stloc.1 // y
对于:
Dim x As Double = 3
Dim y As Double = 2 ^ x
IL 也是:
IL_0000: nop
IL_0001: ldc.r8 00 00 00 00 00 00 08 40
IL_000A: stloc.0 // x
IL_000B: ldc.r8 00 00 00 00 00 00 00 40
IL_0014: ldloc.0 // x
IL_0015: call System.Math.Pow
IL_001A: stloc.1 // y
IE编译器已将^
转换为对Math.Pow
的调用-它们在运行时相同.
IE the compiler has turned the ^
into a call to Math.Pow
- they're identical at runtime.
这篇关于帽子^运算符与Math.Pow()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!