Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. log() / log2() / log10() / exp()

log() / log2() / log10() / exp()

These are logarithmic and exponential functions. They are defined in <math.h> and are used in a wide range of fields including information theory, signal processing, and scientific computing. You may need to pass the -lm linker option at compile time.

Syntax

// Returns the natural logarithm (base e) of x (requires x > 0).
double log(double x);

// Returns the base-2 logarithm of x (requires x > 0; C99 and later).
double log2(double x);

// Returns the common logarithm (base 10) of x (requires x > 0).
double log10(double x);

// Returns e raised to the power of x (where e ≈ 2.71828).
double exp(double x);

// Returns 2 raised to the power of x (C99 and later).
double exp2(double x);

// Computes e^x - 1 with high precision (useful when x is close to 0; C99 and later).
double expm1(double x);

Function List

FunctionDescriptionExample
log()Returns the natural logarithm (ln x).log(M_E) → 1.0
log2()Returns the base-2 logarithm.log2(8.0) → 3.0
log10()Returns the common logarithm (log₁₀ x).log10(1000.0) → 3.0
exp()Returns e raised to the power of x (the exponential function).exp(1.0) → 2.71828...
exp2()Returns 2 raised to the power of x.exp2(10.0) → 1024.0
expm1()Computes e^x - 1 with high precision.expm1(0.0001) ≈ 0.00010001

Sample Code

#include <stdio.h>
#include <math.h>

int main(void) {
    // Use log to compute the natural logarithm.
    printf("log(1.0)  = %.6f\n", log(1.0));   // Prints ln(1) = 0.
    printf("log(M_E)  = %.6f\n", log(M_E));   // Prints ln(e) = 1.
    printf("log(10.0) = %.6f\n", log(10.0));  // Prints 2.302585...

    // Use log2 to find how many times 2 must be multiplied to reach a value.
    printf("log2(8.0)    = %.1f\n", log2(8.0));    // Prints '3.0'.
    printf("log2(1024.0) = %.1f\n", log2(1024.0)); // Prints '10.0'.

    // Use log10 to find the number of digits (power of 10).
    printf("log10(100.0)  = %.1f\n", log10(100.0));  // Prints '2.0'.
    printf("log10(1000.0) = %.1f\n", log10(1000.0)); // Prints '3.0'.

    // To compute a logarithm with an arbitrary base b, use log(x) / log(b).
    double x = 27.0, base = 3.0;
    double log_base3 = log(x) / log(base);
    printf("log3(27) = %.6f\n", log_base3); // Prints '3.000000'.

    // Use exp to compute the exponential function (example: continuous compound interest).
    double r = 0.05; // Annual interest rate: 5%
    double t = 10.0; // 10 years
    double growth = exp(r * t); // Growth factor with continuous compounding
    printf("Growth factor after 10 years: %.4f\n", growth); // Prints approximately 1.6487.

    // exp and log are inverse functions of each other.
    double val = 3.0;
    printf("exp(log(3.0)) = %.6f\n", exp(log(val))); // Prints '3.000000'.

    return 0;
}

Notes

In C, log() computes the natural logarithm (base e). Note that in mathematics, "log" often refers to the common logarithm (base 10), so be careful about the distinction. To compute a logarithm with an arbitrary base b, use the change-of-base formula: log_b(x) = log(x) / log(b).

Passing a value of 0 or less to log(), log2(), or log10() returns -HUGE_VAL (equivalent to negative infinity) or NaN. Always verify that the argument is positive before calling these functions.

expm1() computes e^x - 1 more accurately than exp(x) - 1 when x is very close to zero. Use it in situations where precision matters for small values of x, such as financial calculations.

For exponentiation (pow) and square roots, see sqrt() / pow(). For trigonometric functions, see sin() / cos() / tan().

If you find any errors or copyright issues, please .