Next: Testing the Sign of Numbers, Previous: Elementary Functions, Up: Mathematical Functions [Index]
A common complaint about the standard C library is its lack of a function for calculating (small) integer powers. GSL provides some simple functions to fill this gap. For reasons of efficiency, these functions do not check for overflow or underflow conditions.
These routines computes the power x^n for integer n. The
power is computed efficiently—for example, x^8 is computed as
((x^2)^2)^2, requiring only 3 multiplications. A version of this
function which also computes the numerical error in the result is
available as gsl_sf_pow_int_e
.
These functions can be used to compute small integer powers x^2,
x^3, etc. efficiently. The functions will be inlined when
HAVE_INLINE
is defined, so that use of these functions
should be as efficient as explicitly writing the corresponding
product expression.
#include <gsl/gsl_math.h> double y = gsl_pow_4 (3.141) /* compute 3.141**4 */