Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The most efficient way of implementing pow() function in floating point

I am trying to implement my own version of pow() and sqrt() function as my custom library doesn't have pow()/sqrt() floating point support.

Can anyone help?

like image 400
Viks Avatar asked Apr 18 '10 23:04

Viks


People also ask

What is the use of POW () function explain with example?

Definition and Usage The pow() function returns the value of x to the power of y (xy). If a third parameter is present, it returns x to the power of y, modulus z.

How is pow function implemented in C?

pow() is function to get the power of a number, but we have to use #include<math. h> in c/c++ to use that pow() function. then two numbers are passed. Example – pow(4 , 2); Then we will get the result as 4^2, which is 16.


2 Answers

Yes, Sun can (Oracle now, I guess):

fdlibm, the "freely distributable math library", has sqrt and pow, along with many other math functions.

They're fairly high-tech implementations, though, and of course nothing is ever the "most efficient" implementation of something like this. Are you after source code to get it done, or are you really not so much looking for pow and sqrt, but actually looking for an education in floating-point algorithms programming?

like image 94
Steve Jessop Avatar answered Oct 08 '22 23:10

Steve Jessop


Sure - it's easy if you have exponential and natural log functions.

Since y = x^n, you can take the natural log of both sides:

ln(y) = n*ln(x)

Then taking the exponential of both sides gives you what you want:

y = exp(n*ln(x))

If you want something better, the best place I know to look is Abramowitz and Stegun.

like image 29
duffymo Avatar answered Oct 08 '22 23:10

duffymo