What is Math.Log2() in C#?

C# has a built-in Math class which provides useful mathematical functions and operations. The class has the Log2() function, which is used to compute the base 2 logarithm of a specified number.

Syntax

public static double Log2 (double value);

Parameters

  • value: This is of the Double type and represents the input value for which we have to find Log2(). value is a base 10 number with all double numbers.

Return Value

  • Double: The function returns a positive Double number representing the base 2 log of the value.

  • NaN: The function returns this for a negative or NaN input in value.

  • PositiveInfinity: The function returns this for PositiveInfinity as input in value.

  • NegativeInfinity: The function returns this for 0 as input in value.

Example

using System;
class Educative
{
static void Main()
{
double result = Math.Log2(8);
System.Console.WriteLine("Log2(8) = "+ result);
double result1 = Math.Log2(Double.PositiveInfinity));
System.Console.WriteLine("Log2(∞) = "+ result1);
double result2 = Math.Log2(0);
System.Console.WriteLine("Log2(0) = "+ result2);
}
}

Output

  • Log2 ( 8 ) = 3
  • Log2 ( ∞ ) = ∞
  • Log2 ( 0 ) = -∞