Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Numbers and Math

Math.Log() and Math.Exp() methods

Reference for Math.Log, Log10, Log2 and Exp in C#: natural and base-10 logarithms, arbitrary bases, domain errors, growth and decay examples.

A logarithm answers the question "to what power must the base be raised to get this number?". Math.Log(x) is the natural logarithm (base e), Math.Log10(x) uses base 10, Math.Log2(x) uses base 2, and Math.Log(x, newBase) accepts any base. Math.Exp(x) is the opposite of Math.Log: it computes e raised to the power x.

Logarithms turn multiplication into addition, which makes them useful for counting digits, finding how many times something halves, working with very large or very small probabilities, and modeling growth and decay.

C#
double digits = Math.Floor(Math.Log10(98765)) + 1;   // 5
double bits = Math.Log2(1024);                        // 10

Try it

C# Logarithms and exponentials

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 Console.ReadLine()), it gets the next line from this box.

C# Growth, decay and domain errors

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 Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static double Log(double d);
public static double Log(double a, double newBase);
public static double Log10(double d);
public static double Log2(double x);
public static double Exp(double d);

Name Description
d, a, x The number. For logarithms it must be positive.
newBase The base for the two-argument Log.

Returns: the logarithm or the exponential as a double.

Errors / undefined behavior: nothing is thrown. Log(0) is -Infinity. Log of a negative number is NaN. Exp overflows to Infinity above about 709.78 and underflows to 0 below about -745. Log(a, 1) and Log(a, 0) return NaN, because those bases make no sense.

Usage notes

  • Math.Log is the natural log. Unlike calculators or some languages where "log" means base 10, C# uses Log10 for that. Math.Log(100) is about 4.6, not 2.
  • Change of base. Math.Log(x, b) equals Math.Log(x) / Math.Log(b). The result for exact powers may be a hair off (Math.Log(1000, 10) can be 2.9999999999999996), so prefer Log10 or Log2 when the base is 10 or 2.
  • Counting digits. (int)Math.Floor(Math.Log10(n)) + 1 works for positive n; for exact results on huge long values, or for n == 0, use n.ToString().Length or a loop. For bits, BitOperations.Log2 is exact and integer-only.
  • Probabilities. When multiplying many numbers below 1, add their logarithms instead (as in the second example) to avoid underflow to zero.
  • Growth formulas. Continuous growth is P * Math.Exp(r * t). Compounding a fixed number of times per year uses Math.Pow(1 + r / n, n * t).
  • Decimal numbers. decimal has no logarithm. Convert to double, calculate, and convert back, accepting about 15 digits of precision.
  • BigInteger.Log. The BigInteger type has its own Log, Log10 and a two-argument Log, which work on numbers too big for double.