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.
#include <math.h> double exp(double x); double log(double x); double log10(double x); double log2(double x);
Try it
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.
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
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)withx < 0returns NaN (domain error).log(0)returns-HUGE_VAL, which is negative infinity, with a pole error.exp(x)with a large positivex(above about 709.78 fordouble) overflows to infinity with a range error. A large negativexunderflows to 0.log1p(x)requiresx >= -1.
Usage notes
logis natural. For base 10 uselog10; for any other base uselog(x) / log(base).- Digit counting with
floor(log10(n)) + 1is neat but depends on floating-point accuracy for large values. For exact work, divide by 10 in a loop. - Bits and powers of two.
log2of an exact power of two is exact on common libraries. For integers, counting leading zeros (a compiler builtin, orstdc_bit_widthin C23's<stdbit.h>) is exact and faster. - Precision near zero.
exp(x) - 1andlog(1 + x)lose digits whenxis tiny because1 + xrounds. Useexpm1andlog1p, as the example shows. - Overflow safety. Work with logarithms to multiply many probabilities or large factors: add the logs instead of multiplying, then
expat the end if needed. - Unusual identities.
pow(x, y)equalsexp(y * log(x))for positivexin theory, but callpowinstead; 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:
cexpandclogincomplex.h. - Standard version.
exp,logandlog10in C89;exp2,expm1,log1pandlog2in C99.