Math.Log() Method in C#

The Math.Log() method in C# is used to return the logarithm of a specified number. This method provides two overloads: one for calculating the natural logarithm (base e) and another for calculating logarithm with a specified base.

Syntax

Following are the two overloads of the Math.Log() method −

public static double Log(double num)
public static double Log(double num, double newBase)

Parameters

  • num − The number whose logarithm is to be calculated (must be positive for real results).

  • newBase − The base of the logarithm (must be positive and not equal to 1).

Return Value

The method returns a double representing the logarithm of the specified number. Special cases include:

  • NaN if the argument is negative or NaN

  • PositiveInfinity if the argument is PositiveInfinity

  • NegativeInfinity if the argument is 0

Using Math.Log() for Natural Logarithm

The single-parameter overload calculates the natural logarithm (base e) −

using System;

public class Demo {
    public static void Main() {
        double val1 = 2.15;
        double val2 = -2.15;
        double val3 = Math.E;
        
        Console.WriteLine("Log(" + val1 + ") = " + Math.Log(val1));
        Console.WriteLine("Log(" + val2 + ") = " + Math.Log(val2));
        Console.WriteLine("Log(e) = " + Math.Log(val3));
    }
}

The output of the above code is −

Log(2.15) = 0.765467842139571
Log(-2.15) = NaN
Log(e) = 1

Using Math.Log() with Custom Base

The two-parameter overload allows you to specify a custom base −

using System;

public class Demo {
    public static void Main() {
        double number = 8;
        double base2 = 2;
        double base10 = 10;
        
        Console.WriteLine("Log base 2 of " + number + " = " + Math.Log(number, base2));
        Console.WriteLine("Log base 10 of " + number + " = " + Math.Log(number, base10));
        Console.WriteLine("Log base 10 of 1000 = " + Math.Log(1000, base10));
    }
}

The output of the above code is −

Log base 2 of 8 = 3
Log base 10 of 8 = 0.9030899869919435
Log base 10 of 1000 = 3

Handling Special Values

The Math.Log() method handles special floating-point values according to IEEE standards −

using System;

public class Demo {
    public static void Main() {
        Console.WriteLine("Log(0) = " + Math.Log(0));
        Console.WriteLine("Log(1) = " + Math.Log(1));
        Console.WriteLine("Log(PositiveInfinity) = " + Math.Log(Double.PositiveInfinity));
        Console.WriteLine("Log(NegativeInfinity) = " + Math.Log(Double.NegativeInfinity));
        Console.WriteLine("Log(NaN) = " + Math.Log(Double.NaN));
    }
}

The output of the above code is −

Log(0) = -Infinity
Log(1) = 0
Log(PositiveInfinity) = Infinity
Log(NegativeInfinity) = NaN
Log(NaN) = NaN

Common Use Cases

The Math.Log() method is commonly used in scientific calculations, data analysis, and algorithms that require logarithmic transformations. It's particularly useful for converting exponential growth to linear scale and in mathematical formulas involving logarithms.

Conclusion

The Math.Log() method in C# provides flexible logarithm calculation with support for both natural logarithm and custom bases. It handles special values gracefully and returns appropriate results for edge cases like negative numbers, zero, and infinity values.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements