Facebook Page Twitter Page LinkedIn Page
× C# Tutorial C# Advanced C# Resources


The C# Math Log() method returns the natural logarithm or logarithm to the specified base of a given number. In special cases it returns the following:

  • If a is NaN or less than zero, the method returns NaN.
  • If Base is NaN or 1 or less than zero, the method returns NaN.
  • If a is PositiveInfinity and 0 < Base < 1, the method returns NegativeInfinity.
  • If a is PositiveInfinity and Base > 1, the method returns PositiveInfinity.
  • If a is zero and 0 < Base < 1, the method returns PositiveInfinity.
  • If a is zero and Base > 1, the method returns NegativeInfinity.
  • If a is 1 and Base is 0 or PositiveInfinity, the method returns 0.
  • If a is not equal to 1 and Base is 0 or PositiveInfinity, the method returns NaN.

Syntax

public static double Log (double a);
public static double Log (double a, double Base);

Parameters

a Specify the number whose logarithm is to be found.
Base Specify the base of the logarithm.

Return Value

Returns the natural logarithm or logarithm to the specified base of a given number.

Example:

In the example below, Log() method is used to calculate the natural logarithm of a given number.

using System;

class MyProgram {
  static void Main(string[] args) {
	 Console.WriteLine(Math.Log(10)); 
	 Console.WriteLine(Math.Log(50)); 
	 Console.WriteLine(Math.Log(100));  
  }
}

The output of the above code will be:

2.302585092994046
3.912023005428146
4.605170185988092

Example:

In the example below, Log() method is used to calculate the specified base logarithm of a given number.

using System;

class MyProgram {
  static void Main(string[] args) {
	 Console.WriteLine(Math.Log(10, 10)); 
	 Console.WriteLine(Math.Log(32, 2)); 
	 Console.WriteLine(Math.Log(9, 3));  
  }
}

The output of the above code will be:

1
5
2

❮ C# Math Methods

5