Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Math.Pow() implemented in .NET Framework?

Tags:

c#

.net

pow

I was looking for an efficient approach for calculating ab (say a = 2 and b = 50). To start things up, I decided to take a look at the implementation of Math.Pow() function. But in .NET Reflector, all I found was this:

[MethodImpl(MethodImplOptions.InternalCall), SecuritySafeCritical] public static extern double Pow(double x, double y); 

What are some of the resources wherein I can see as what's going on inside when I call Math.Pow() function?

like image 608
Pawan Mishra Avatar asked Jan 15 '12 14:01

Pawan Mishra


People also ask

How does the Math POW method work?

The Math. pow() function returns the base to the exponent power, as in base exponent , the base and the exponent are in decimal numeral system. Because pow() is a static method of Math , use it as Math. pow() , rather than as a method of a Math object you created.

What does Math Pow do in C#?

The Math. Pow() method in C# is used to compute a number raised to the power of some other number.

How is built in function pow () function different from function Math pow () explain with an example?

The built-in pow(x,y [,z]) function has an optional third parameter as modulo to compute x raised to the power of y and optionally do a modulo (% z) on the result. It is same as the mathematical operation: (x ^ y) % z. Whereas, math. pow() function does not have modulo functionality.

Why do we use pow () function?

The function pow() is used to calculate the power raised to the base value. It takes two arguments. It returns the power raised to the base value. It is declared in “math.


2 Answers

MethodImplOptions.InternalCall

That means that the method is actually implemented in the CLR, written in C++. The just-in-time compiler consults a table with internally implemented methods and compiles the call to the C++ function directly.

Having a look at the code requires the source code for the CLR. You can get that from the SSCLI20 distribution. It was written around the .NET 2.0 time frame, I've found the low-level implementations, like Math.Pow() to be still largely accurate for later versions of the CLR.

The lookup table is located in clr/src/vm/ecall.cpp. The section that's relevant to Math.Pow() looks like this:

FCFuncStart(gMathFuncs)     FCIntrinsic("Sin", COMDouble::Sin, CORINFO_INTRINSIC_Sin)     FCIntrinsic("Cos", COMDouble::Cos, CORINFO_INTRINSIC_Cos)     FCIntrinsic("Sqrt", COMDouble::Sqrt, CORINFO_INTRINSIC_Sqrt)     FCIntrinsic("Round", COMDouble::Round, CORINFO_INTRINSIC_Round)     FCIntrinsicSig("Abs", &gsig_SM_Flt_RetFlt, COMDouble::AbsFlt, CORINFO_INTRINSIC_Abs)     FCIntrinsicSig("Abs", &gsig_SM_Dbl_RetDbl, COMDouble::AbsDbl, CORINFO_INTRINSIC_Abs)     FCFuncElement("Exp", COMDouble::Exp)     FCFuncElement("Pow", COMDouble::Pow)     // etc.. FCFuncEnd() 

Searching for "COMDouble" takes you to clr/src/classlibnative/float/comfloat.cpp. I'll spare you the code, just have a look for yourself. It basically checks for corner cases, then calls the CRT's version of pow().

The only other implementation detail that's interesting is the FCIntrinsic macro in the table. That's a hint that the jitter may implement the function as an intrinsic. In other words, substitute the function call with a floating point machine code instruction. Which is not the case for Pow(), there is no FPU instruction for it. But certainly for the other simple operations. Notable is that this can make floating point math in C# substantially faster than the same code in C++, check this answer for the reason why.

By the way, the source code for the CRT is also available if you have the full version of Visual Studio vc/crt/src directory. You'll hit the wall on pow() though, Microsoft purchased that code from Intel. Doing a better job than the Intel engineers is unlikely. Although my high-school book's identity was twice as fast when I tried it:

public static double FasterPow(double x, double y) {     return Math.Exp(y * Math.Log(x)); } 

But not a true substitute because it accumulates error from 3 floating point operations and doesn't deal with the weirdo domain problems that Pow() has. Like 0^0 and -Infinity raised to any power.

like image 136
Hans Passant Avatar answered Sep 22 '22 17:09

Hans Passant


Hans Passant's answer is great, but I would like to add that if b is an integer, then a^b can be computed very efficiently with binary decomposition. Here's a modified version from Henry Warren's Hacker's Delight:

public static int iexp(int a, uint b) {     int y = 1;      while(true) {         if ((b & 1) != 0) y = a*y;         b = b >> 1;         if (b == 0) return y;         a *= a;     }     } 

He notes that this operation is optimal (does the minimum number of arithmetic or logical operations) for all b < 15. Also there is no known solution to the general problem of finding an optimal sequence of factors to compute a^b for any b other than an extensive search. It's an NP-Hard problem. So basically that means that the binary decomposition is as good as it gets.

like image 34
Michael Graczyk Avatar answered Sep 21 '22 17:09

Michael Graczyk