Lesson 30 of 31

Logarithms & Exponentials

Logarithms and Exponentials in C

<math.h> provides logarithm and exponential functions — essential for scientific computation, signal processing, and algorithm analysis.

The Functions

FunctionDescriptionExample
log(x)Natural log (base e)log(M_E) → 1.0
log2(x)Log base 2log2(8.0) → 3.0
log10(x)Log base 10log10(100.0) → 2.0
exp(x)e raised to xexp(1.0) → 2.71828...

Key Identities

// log and exp are inverses
exp(log(5.0)) == 5.0

// Change of base formula
// log_b(x) = log(x) / log(b)
double log3_of_81 = log(81.0) / log(3.0);  // 4.0

// log2 is handy for computer science
// How many bits to represent n values?
int bits = (int)ceil(log2(256.0));  // 8

Decibels (dB)

Logarithms are used in audio to express power ratios:

// dB = 10 * log10(power_ratio)
double ratio = 100.0;
double db = 10.0 * log10(ratio);  // 20.0 dB

Complexity Analysis

log2 appears constantly in algorithm analysis (binary search, divide-and-conquer):

// Steps for binary search on n elements ≈ log2(n)
printf("%.0f\n", log2(1024.0));  // 10 steps for 1024 elements

Your Task

Using <math.h>, compute and print with %.4f:

  1. log(M_E) — natural log of e
  2. log2(16.0) — log base 2 of 16
  3. log10(1000.0) — log base 10 of 1000
  4. exp(1.0) — e to the power of 1
TCC compiler loading...
Loading...
Click "Run" to execute your code.