Exponentiation and Logarithms

double function>exp/function> (double x) float function>expf/function> (float x) long double function>expl/function> (long double x) These functions compute e (the base of natural logarithms) raised to the power x.

If the magnitude of the result is too large to be representable, exp signals overflow.

double function>exp2/function> (double x) float function>exp2f/function> (float x) long double function>exp2l/function> (long double x) These functions compute 2 raised to the power x. Mathematically, exp2 (x) is the same as exp (x * log (2)).

double function>exp10/function> (double x) float function>exp10f/function> (float x) long double function>exp10l/function> (long double x) double function>pow10/function> (double x) float function>pow10f/function> (float x) long double function>pow10l/function> (long double x) These functions compute 10 raised to the power x. Mathematically, exp10 (x) is the same as exp (x * log (10)).

These functions are GNU extensions. The name exp10 is preferred, since it is analogous to exp and exp2.

double function>log/function> (double x) float function>logf/function> (float x) long double function>logl/function> (long double x) These functions compute the natural logarithm of x. exp (log (x)) equals x, exactly in mathematics and approximately in C.

If x is negative, log signals a domain error. If x is zero, it returns negative infinity; if x is too close to zero, it may signal overflow.

double function>log10/function> (double x) float function>log10f/function> (float x) long double function>log10l/function> (long double x) These functions return the base-10 logarithm of x. log10 (x) equals log (x) / log (10).

double function>log2/function> (double x) float function>log2f/function> (float x) long double function>log2l/function> (long double x) These functions return the base-2 logarithm of x. log2 (x) equals log (x) / log (2).

double function>logb/function> (double x) float function>logbf/function> (float x) long double function>logbl/function> (long double x) These functions extract the exponent of x and return it as a floating-point value. If FLT_RADIX is two, logb is equal to floor (log2 (x)), except it's probably faster.

If x is de-normalized, logb returns the exponent x would have if it were normalized. If x is infinity (positive or negative), logb returns oo. If x is zero, logb returns oo. It does not signal.

int function>ilogb/function> (double x) int function>ilogbf/function> (float x) int function>ilogbl/function> (long double x) These functions are equivalent to the corresponding logb functions except that they return signed integer values.

Since integers cannot represent infinity and NaN, ilogb instead returns an integer that can't be the exponent of a normal floating-point number. math.h defines constants so you can check for this.

int function>FP_ILOGB0/function> ilogb returns this value if its argument is 0. The numeric value is either INT_MIN or -INT_MAX.

This macro is defined in ISO C99.

int function>FP_ILOGBNAN/function> ilogb returns this value if its argument is NaN. The numeric value is either INT_MIN or INT_MAX.

This macro is defined in ISO C99.

These values are system specific. They might even be the same. The proper way to test the result of ilogb is as follows:

i = ilogb (f);
if (i == FP_ILOGB0 || i == FP_ILOGBNAN)
  {
    if (isnan (f))
      {
        /* Handle NaN.  */
      }
    else if (f  == 0.0)
      {
        /* Handle 0.0.  */
      }
    else
      {
        /* Some other value with large exponent,
           perhaps +Inf.  */
      }
  }

double function>pow/function> (double base, double power) float function>powf/function> (float base, float power) long double function>powl/function> (long double base, long double power) These are general exponentiation functions, returning base raised to power.

Mathematically, pow would return a complex number when base is negative and power is not an integral value. pow can't do that, so instead it signals a domain error. pow may also underflow or overflow the destination type.

double function>sqrt/function> (double x) float function>sqrtf/function> (float x) long double function>sqrtl/function> (long double x) These functions return the nonnegative square root of x.

If x is negative, sqrt signals a domain error. Mathematically, it should return a complex number.

double function>cbrt/function> (double x) float function>cbrtf/function> (float x) long double function>cbrtl/function> (long double x) These functions return the cube root of x. They cannot fail; every representable real value has a representable real cube root.

double function>hypot/function> (double x, double y) float function>hypotf/function> (float x, float y) long double function>hypotl/function> (long double x, long double y) These functions return sqrt (x*x + y*y). This is the length of the hypotenuse of a right triangle with sides of length x and y, or the distance of the point (x, y) from the origin. Using this function instead of the direct formula is wise, since the error is much smaller. See also the function cabs in the section called “Absolute Value”.

double function>expm1/function> (double x) float function>expm1f/function> (float x) long double function>expm1l/function> (long double x) These functions return a value equivalent to exp (x) - 1. They are computed in a way that is accurate even if x is near zero--a case where exp (x) - 1 would be inaccurate owing to subtraction of two numbers that are nearly equal.

double function>log1p/function> (double x) float function>log1pf/function> (float x) long double function>log1pl/function> (long double x) These functions returns a value equivalent to log (1 + x). They are computed in a way that is accurate even if x is near zero.

ISO C99 defines complex variants of some of the exponentiation and logarithm functions.

complex double function>cexp/function> (complex double z) complex float function>cexpf/function> (complex float z) complex long double function>cexpl/function> (complex long double z) These functions return e (the base of natural logarithms) raised to the power of z. Mathematically, this corresponds to the value

exp (z) = exp (creal (z)) * (cos (cimag (z)) + I * sin (cimag (z)))

complex double function>clog/function> (complex double z) complex float function>clogf/function> (complex float z) complex long double function>clogl/function> (complex long double z) These functions return the natural logarithm of z. Mathematically, this corresponds to the value

log (z) = log (cabs (z)) + I * carg (z)

clog has a pole at 0, and will signal overflow if z equals or is very close to 0. It is well-defined for all other values of z.

complex double function>clog10/function> (complex double z) complex float function>clog10f/function> (complex float z) complex long double function>clog10l/function> (complex long double z) These functions return the base 10 logarithm of the complex value z. Mathematically, this corresponds to the value

log (z) = log10 (cabs (z)) + I * carg (z)

These functions are GNU extensions.

complex double function>csqrt/function> (complex double z) complex float function>csqrtf/function> (complex float z) complex long double function>csqrtl/function> (complex long double z) These functions return the complex square root of the argument z. Unlike the real-valued functions, they are defined for all values of z.

complex double function>cpow/function> (complex double base, complex double power) complex float function>cpowf/function> (complex float base, complex float power) complex long double function>cpowl/function> (complex long double base, complex long double power) These functions return base raised to the power of power. This is equivalent to cexp (y * clog (x))