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.
double digits = Math.Floor(Math.Log10(98765)) + 1; // 5 double bits = Math.Log2(1024); // 10
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 Console.ReadLine()), 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 Console.ReadLine()), it gets the next line from this box.
Syntax
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.Logis the natural log. Unlike calculators or some languages where "log" means base 10, C# usesLog10for that.Math.Log(100)is about 4.6, not 2.- Change of base.
Math.Log(x, b)equalsMath.Log(x) / Math.Log(b). The result for exact powers may be a hair off (Math.Log(1000, 10)can be2.9999999999999996), so preferLog10orLog2when the base is 10 or 2. - Counting digits.
(int)Math.Floor(Math.Log10(n)) + 1works for positiven; for exact results on hugelongvalues, or forn == 0, usen.ToString().Lengthor a loop. For bits,BitOperations.Log2is 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 usesMath.Pow(1 + r / n, n * t). - Decimal numbers.
decimalhas no logarithm. Convert todouble, calculate, and convert back, accepting about 15 digits of precision. BigInteger.Log. TheBigIntegertype has its ownLog,Log10and a two-argumentLog, which work on numbers too big fordouble.