Skip to content
Tutorials Center
My learning

Topics

C Reference ยท ctype.h, math.h and others

C exp(), log(), log10() and log2() functions

Reference for C exp(), log(), log10(), log2(), expm1() and log1p() from math.h: natural and base-10 logarithms, domain errors and digit counting.

exp(x) computes e raised to the power x, where e is about 2.71828. log(x) is its inverse, the natural logarithm. log10() and log2() give logarithms to base 10 and base 2. Mind the names: in C, log means natural logarithm, not base 10 as on many calculators.

They are used for growth and decay models, for converting between scales (decibels, pH, bits), and for counting the digits of a number. expm1() and log1p() compute exp(x) - 1 and log(1 + x) accurately for tiny x.

C
#include <math.h>

double exp(double x);
double log(double x);
double log10(double x);
double log2(double x);

Try it

C Inverse functions and different bases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Digits, bits and compound growth

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
double exp(double x);    double exp2(double x);    double expm1(double x);
double log(double x);    double log10(double x);   double log2(double x);   double log1p(double x);
Name Description
x For exp functions, the exponent. For log functions, the number whose logarithm is taken.

Returns: exp(x) is e^x, exp2(x) is 2^x, expm1(x) is e^x - 1. log(x), log10(x) and log2(x) are the logarithms of x to base e, 10 and 2. log1p(x) is log(1 + x). Each has f and l variants.

Errors / undefined behavior: none is undefined, but the functions report errors:

  • log(x) with x < 0 returns NaN (domain error). log(0) returns -HUGE_VAL, which is negative infinity, with a pole error.
  • exp(x) with a large positive x (above about 709.78 for double) overflows to infinity with a range error. A large negative x underflows to 0.
  • log1p(x) requires x >= -1.

Usage notes

  • log is natural. For base 10 use log10; for any other base use log(x) / log(base).
  • Digit counting with floor(log10(n)) + 1 is neat but depends on floating-point accuracy for large values. For exact work, divide by 10 in a loop.
  • Bits and powers of two. log2 of an exact power of two is exact on common libraries. For integers, counting leading zeros (a compiler builtin, or stdc_bit_width in C23's <stdbit.h>) is exact and faster.
  • Precision near zero. exp(x) - 1 and log(1 + x) lose digits when x is tiny because 1 + x rounds. Use expm1 and log1p, as the example shows.
  • Overflow safety. Work with logarithms to multiply many probabilities or large factors: add the logs instead of multiplying, then exp at the end if needed.
  • Unusual identities. pow(x, y) equals exp(y * log(x)) for positive x in theory, but call pow instead; it handles special cases and has better accuracy.
  • Output. Results of transcendental functions can differ in the last digit between platforms; print a limited number of digits.
  • Complex versions: cexp and clog in complex.h.
  • Standard version. exp, log and log10 in C89; exp2, expm1, log1p and log2 in C99.