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
Doubletype and represents the input value for which we have to findLog2().valueis a base 10 number with all double numbers.
Return Value
-
Double: The function returns a positive
Doublenumber representing the base 2 log of thevalue. -
NaN: The function returns this for a negative or
NaNinput invalue. -
PositiveInfinity: The function returns this for
PositiveInfinityas input invalue. -
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 ) = -∞