C# Math - Log2() Method
The C# 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 positive infinity, the method returns positive infinity.
- If the argument is zero, the method returns negative infinity.
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(0) = " + Math.Log2(0)); Console.WriteLine("Math.Log2(10) = " + Math.Log2(10)); Console.WriteLine("Math.Log2(50) = " + Math.Log2(50)); Console.WriteLine("Math.Log2(Double.NaN) = " + Math.Log2(Double.NaN)); Console.WriteLine("Math.Log2(Double.NegativeInfinity) = " + Math.Log2(Double.NegativeInfinity)); Console.WriteLine("Math.Log2(Double.PositiveInfinity) = " + Math.Log2(Double.PositiveInfinity)); } }
The output of the above code will be:
Math.Log2(0) = -Infinity Math.Log2(10) = 3.321928094887362 Math.Log2(50) = 5.643856189774724 Math.Log2(Double.NaN) = NaN Math.Log2(Double.NegativeInfinity) = NaN Math.Log2(Double.PositiveInfinity) = Infinity
❮ C# Math Methods