C# Math Log() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.Log(a) returns the natural logarithm of a. A logarithm answers the question "to which power must I raise the base to get this number?". The natural logarithm uses the base e (about 2.718). So Math.Log(1) is 0, because e to the power 0 is 1.

With two arguments, Math.Log(a, newBase) lets you pick the base. Math.Log(8, 2) asks "2 to which power gives 8?", and the answer is 3. Logarithms are the reverse of Exp() and Pow(). They turn very large ranges into small numbers, which is why they appear in sound levels, earthquake scales, and "how many digits?" questions. For base 10 and base 2, there are also Log10() and Log2().

This is a static member of the static class System.Math. You call it with the class name, such as Math.Log(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Log is in every .NET version. The one-argument form uses base e; the two-argument form lets you choose the base.

Syntax

Pass one number for base e, or two numbers for a custom base. Both forms return a double.

C#
double a = Math.Log(1);           // 0
double b = Math.Log(Math.E);      // 1 (very nearly)
double c = Math.Log(8, 2);        // 3 (very nearly)
double d = Math.Log(0);           // -Infinity
CallWhat it does
Math.Log(1)Returns 0.
Math.Log(8, 2)2 to which power is 8? About 3.
Math.Log(0)Returns -Infinity.
Math.Log(-1)Returns NaN. You cannot take the logarithm of a negative number.

The input must be greater than zero. Zero gives -Infinity and a negative number gives NaN, with no exception. A base of 1 gives NaN because "1 to any power" is always 1. Most answers are not whole numbers, so round them before printing or comparing, for example Math.Round(Math.Log(8, 2), 6).

Library declarations
C#
public static double Log(double d);
public static double Log(double a, double newBase);

Parameters

ParameterDescription
d / aRequired. The number whose logarithm you want. It must be greater than 0 for a normal answer.
newBaseOptional. The base of the logarithm. It must be greater than 0 and not equal to 1. If you leave it out, the base is e.

Return Value

Return ValueDescription
doubleThe logarithm. -Infinity if the number is 0, NaN if it is negative or NaN, Infinity if it is positive infinity.

C# Examples

Example 1 — Natural Logarithm of a Few Numbers

Call Log on 1, e, and 10, then try zero and a negative number.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        Console.WriteLine(Math.Log(1));
        Console.WriteLine(Math.Round(Math.Log(Math.E), 6));
        Console.WriteLine(Math.Round(Math.Log(10), 4).ToString(CultureInfo.InvariantCulture));
        Console.WriteLine(Math.Log(0));
        Console.WriteLine(Math.Log(-5));
    }
}

How It Works

  1. Log of 1. e to the power 0 is 1, so Math.Log(1) is exactly 0. This is true in every base.
  2. Log of e. The natural logarithm of e is 1. We round to 6 places so tiny floating-point noise cannot show.
  3. Log of 10. e must be raised to about 2.3026 to reach 10. The answer is not whole, so we round it to 4 places.
  4. Bad inputs. Zero would need an endless negative power, so the answer is -Infinity. A negative number has no real logarithm, so you get NaN.

Example 2 — Logarithm in Another Base

Use the two-argument form to ask "base to which power?" for base 2, 10, and 3.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        Console.WriteLine(Math.Round(Math.Log(8, 2), 6));
        Console.WriteLine(Math.Round(Math.Log(100, 10), 6));
        Console.WriteLine(Math.Round(Math.Log(81, 3), 6));
        Console.WriteLine(Math.Round(Math.Log(1000, 2), 4).ToString(CultureInfo.InvariantCulture));
    }
}

How It Works

  1. Base 2. 2 x 2 x 2 is 8, so the power is 3. This is also the number of bits needed to count up to 8 values.
  2. Base 10. 10 x 10 is 100, so Math.Log(100, 10) is 2. It is the number of zeros in a power of ten.
  3. Base 3. 3 x 3 x 3 x 3 is 81, so the answer is 4. Any base above 0 (except 1) is allowed.
  4. Not a whole power. 1000 is not a power of 2. The answer, about 9.9658, means "you can halve 1000 almost 10 times before you reach 1".

Example 3 — NaN, Infinity, and a Bad Base

See what the two-argument form does with a base of 1, a negative number, zero, and infinity.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.Log(8, 1));
        Console.WriteLine(Math.Log(-8, 2));
        Console.WriteLine(Math.Log(0, 2));
        Console.WriteLine(Math.Log(double.PositiveInfinity));
        Console.WriteLine(double.IsNaN(Math.Log(-1)));
    }
}

How It Works

  1. Base 1. 1 to any power is 1, so no power can give 8. The answer is NaN.
  2. Negative number. No power of 2 gives -8, so the result is NaN.
  3. Zero. The logarithm of 0 is minus infinity, because you would need an endless negative power of 2.
  4. Infinity and the check. The logarithm of positive infinity is Infinity. double.IsNaN is the safe way to detect a NaN result.

Frequently Asked Questions

With one argument it returns the natural logarithm (base e). With two arguments, Math.Log(a, newBase), it returns the logarithm in the base you choose. Math.Log(8, 2) is about 3.
Base e. Math.Log(100) is about 4.605. For base 10 use Math.Log10(100), which is 2.
It returns -Infinity. A negative number returns NaN. Neither case throws an exception.
Use (int)Math.Floor(Math.Log10(n)) + 1 for positive whole numbers. Math.Log(n) alone is base e and will not give digit counts.
No. Math is a static class, so you call Math.Log(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: Log10()

Return the base-10 logarithm: how many times you multiply 10 to reach a number.

Log10() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful