Next: , Previous: Elementary Functions, Up: Mathematical Functions


4.4 Small integer powers

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.

— Function: double gsl_pow_int (double x, int n)
— Function: double gsl_pow_uint (double x, unsigned int n)

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.

— Function: double gsl_pow_2 (const double x)
— Function: double gsl_pow_3 (const double x)
— Function: double gsl_pow_4 (const double x)
— Function: double gsl_pow_5 (const double x)
— Function: double gsl_pow_6 (const double x)
— Function: double gsl_pow_7 (const double x)
— Function: double gsl_pow_8 (const double x)
— Function: double gsl_pow_9 (const double x)

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 */