What is Math.Log() in C#?

C# has a built-in Math class that provides useful mathematical functions and operations. The class has the Log() function, which is used to compute the log of a specific number.

Natural log

Syntax

public static double Log (double value);

Parameter

  • value: It is of the Double type and represents the input value that is determined by finding Log(). Its range is all double numbers.

Return value

  • Double: It returns a Double number representing the natural log of the value.

  • NaN: It returns this for negative or NaN input in value.

  • PositiveInfinity: It returns this if value is PositiveInfinity.

  • NegativeInfinity: It returns this if value is 0.

The natural log is the log to the base e, which is a mathematical expression with value 2.71828.

Log to a base

Syntax

public static double Log (double value, double newbase);

Parameter

  • value: It is one of the Double type and represents the input value, which is determined by finding Log(). Its range is all double numbers.
  • newbase: It is of the Double type and represents the new base value for Log(). Its range is all double numbers.

Return value

  • Double: It returns a Double number representing the log to the base newbase of the value.

  • NaN: It returns this if:

    • value < 0; newbase = any
    • value = any; newbase < 0
    • value != 1; newbase = 0
    • value != 1; newbase = +∞
    • value = NaN; newbase = any
    • value = any; newbase = NaN
    • value = any; newbase = 1
  • PositiveInfinity: It returns this if:

    • value = 0; 0 < newbase < 1
    • value = + ∞; newbase > 1
  • NegativeInfinity: It returns this if:

    • value = 0; newbase > 1
    • value = +∞; 0 < newbase < 1

Example

using System;
class Educative
{
static void Main()
{
Double result = Math.Log(1.2);
System.Console.WriteLine("Log(1.2) = "+ result);
Double result1 = Math.Log(9.9, 0.1);
System.Console.WriteLine("Log(9.9, 0.1) = "+ result1);
}
}

Free Resources