PDA

View Full Version : Help using GMP in C: How to take non-integer power?



launchcode
February 5th, 2009, 07:32 PM
Hi,

Can anyone tell me how to take a non-integer power using GMP in C?

For example:

y = x ^ 0.2,

where x is a GMP float.

Thanks.

Krupski
February 5th, 2009, 07:53 PM
Hi,

Can anyone tell me how to take a non-integer power using GMP in C?

For example:

y = x ^ 0.2,

where x is a GMP float.

Thanks.

Why do you need so much precision? Isn't an ordinary float good enough?

-- Roger

johnl
February 5th, 2009, 07:56 PM
Edit:

Whoops, wrong thread. Sorry.

PythonPower
February 5th, 2009, 09:41 PM
Well one method I can see involves converting an exponent in the form a^b into a function with exp(). Simply put, a^b = exp(b*ln(a)).

But that relies on the functions exp() and ln() which don't exist. You can implement these yourself in various ways and the computational complexity will be the worst complexity of either function.

Here are some references for starters:
http://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
http://en.wikipedia.org/wiki/Logarithm#More_efficient_series

Hopefully there is a less convoluted way but in absence of that, you have my thoughts. I'll check some other ideas out.

casevh
February 6th, 2009, 04:31 AM
If you need to do it in C, I would use mpfr. It uses GMP for the underlying calculations but provides a much broader set of functions.

You can also use mpmath. It is written in Python but can use GMP for some operations.

casevh

PandaGoat
February 6th, 2009, 07:26 AM
I believe the previous poster hinted to what I will suggest. Search the following tome for what you seek: http://gmplib.org/manual/Float-Arithmetic.html#Float-Arithmetic. I even turned the pages for you.

launchcode
February 6th, 2009, 01:59 PM
Thanks guys. Much appreciated.