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


The C# Math Log2() method returns the base-2 logarithm of a given number. In special cases it returns the following:

  • If the argument is NaN or less than zero, the method returns NaN.
  • If the argument is PositiveInfinity, the method returns PositiveInfinity.
  • If the argument is zero, the method returns NegativeInfinity.

Syntax

public static double Log2 (double arg);

Parameters

arg Specify the number whose logarithm is to be found.

Return Value

Returns the base-2 logarithm of a given number.

Example:

In the example below, Log2() method is used to calculate the base-2 logarithm of a given number.

using System;

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

The output of the above code will be:

1
3.321928094887362
5

❮ C# Math Methods

5