pow function

Paul Zimmermann Paul.Zimmermann at loria.fr
Fri May 2 08:52:14 CEST 2008


       Chris,

> GMP has solved almost all of my (math-related) problems except for one: I
> need a power function that takes an mpf_t base, but takes a double exponent,
> instead of an integer. I thought of using MPFR but it's a huge library to
> link in for only one function call. Am I overlooking something obvious? Is
> there a shortcut using GMP functions that I haven't thought of?

a possible shortcut is the following: write your exponent in the form
m*2^e using the frexp() function, where m is an integer. First compute x^m
using mpf_pow_ui, and then square it e times if e >= 0 (mpf_mul) or take its
square-root |e| times if e < 0 (mpf_sqrt). However you might encounter
overflow or underflow problems in this process, and you might have to cut m
into pieces on a 32-bit processor. Finally, you will have no guarantee on the
rounding error.

Alternatively, you can use MPFR indeed.

Paul Zimmermann


More information about the gmp-discuss mailing list