C# Math.Log() – Examples

In this tutorial, we will learn about the C# Math.Log() method, and learn how to use this method to the natural logarithm of a number or logarithm of a number in specified base, with the help of examples.

Log(Double d)

Math.Log(d) returns the natural (base e) logarithm of a specified number d.

Syntax

The syntax of Log() method is

Math.Log(Double d)

where

ParameterDescription
dThe double value whose natural logarithm is to be found.

Return Value

The method returns a value of type double.

Example 1 – Log(d)

In this example, we will find the natural logarithm of the following values.

  • Positive Value
  • Negative Value
  • Positive Infinity
  • Negative Infinity
  • Zero

C# Program

using System;

class Example {
    static void Main(string[] args) {
        Double d, result;

        d = 8;
        result = Math.Log(d);
        Console.WriteLine($"Log({d}) = {result}");

        d = -7;
        result = Math.Log(d);
        Console.WriteLine($"Log({d}) = {result}");

        d = Double.PositiveInfinity;
        result = Math.Log(d);
        Console.WriteLine($"Log({d}) = {result}");

        d = Double.NegativeInfinity;
        result = Math.Log(d);
        Console.WriteLine($"Log({d}) = {result}");

        d = 0;
        result = Math.Log(d);
        Console.WriteLine($"Log({d}) = {result}");
    }
}

Output

Log(8) = 2.07944154167984
Log(-7) = NaN
Log(∞) = ∞
Log(-∞) = NaN
Log(0) = -∞

Log(Double d, Double newBase)

Math.Log(d, newBase) returns the logarithm of a specified number d in a specified base newBase.

Syntax

The syntax of Log() method is

Math.Log(Double a, Double newBase)

where

ParameterDescription
aThe double value whose logarithm is to be found.
newBaseThe base of the logarithm.

Return Value

The method returns a value of type double.

Example 2 – Log(d, newBase)

In this example, we will find the logarithm of the following values.

  • Positive Value to Base 3
  • Negative Value to Base 3
  • 3 to the Base 3
  • Negative Infinity to Base 3
  • Zero to Base 3

C# Program

using System;

class Example {
    static void Main(string[] args) {
        Double d, newBase, result;

        d = 9;
        newBase = 3;
        result = Math.Log(d, newBase);
        Console.WriteLine($"Log({d}) to the base {newBase} = {result}");

        d = -7;
        newBase = 3;
        result = Math.Log(d, newBase);
        Console.WriteLine($"Log({d}) to the base {newBase} = {result}");

        d = 3;
        newBase = 3;
        result = Math.Log(d, newBase);
        Console.WriteLine($"Log({d}) to the base {newBase} = {result}");

        d = Double.NegativeInfinity;
        newBase = 3;
        result = Math.Log(d, newBase);
        Console.WriteLine($"Log({d}) to the base {newBase} = {result}");

        d = 0;
        newBase = 3;
        result = Math.Log(d, newBase);
        Console.WriteLine($"Log({d}) to the base {newBase} = {result}");
    }
}

Output

Log(9) to the base 3 = 2
Log(-7) to the base 3 = NaN
Log(3) to the base 3 = 1
Log(-∞) to the base 3 = NaN
Log(0) to the base 3 = -∞

Conclusion

In this C# Tutorial, we have learnt the syntax of C# Math.Log() method, and also learnt how to use this method with the help of examples.