Hat ^ operator versus Math.Pow ()

After reviewing the MSDN documentation for both ^ (hat) and Math.Pow () , I don't see an obvious difference. There is one?

There is obviously a difference that one function is a function and the other is considered an operator, for example. it won't work:

Public Const x As Double = 3
Public Const y As Double = Math.Pow(2, x) ' Fails because of const-ness

      

But it will:

Public Const x As Double = 3
Public Const y As Double = 2^x

      

But is there a difference in how they produce the end result? For example, Math.Pow()

does a large security check? Or is it just some kind of alias for someone else?

+3


source to share


1 answer


One way to find out is to check 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

      

And for:



Dim x As Double = 3
Dim y As Double = 2 ^ x

      

IL also:

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

      

The IE compiler turned ^

into a call Math.Pow

- they are identical at runtime.

+5


source







All Articles