C++ Math log2()

The function computes the base 2 logarithm of a given number.

Suppose a number is 'x':

Syntax

Note: The return_type can be float, double or long double.

Parameter

x: The value whose logarithm is to be calculated.

Return value

ParameterReturn value
x>1Positive
x=1Zero
1>x> 0Negative
x= 0-infinity
x<0Not a Number(nan)

Example 1

Let's see the simple example when the value of x is greater than one.

Output:

Value of x is : 2
log2(x) = 1

In this example, log2() function computes the logarithm value of base 2 when the value of x is greater than one

Example 2

Let's see the simple example when the value of x is equal to 1.

Output:

Value of x is : 1
log2(x) = 0

In this example, log2() function computes the logarithm value of base 2 when the value of x is equal to one.

Example 3

Let's see the simple example when the value of x lies between 0 and 1.

Output:

Value of x is : 0.2
log2(x) = -2.32193

In this example, log2() function computes the logarithm value of base 2 when the value of x is equal to 0.2.

Example 4

Let's see the simple example when the value of x is equal to zero.

Output:

Value of x is : 0
log2(x) = -inf

In this example, log2() function computes the logarithm value of base 2 when the value of x is equal to 0.

Example 5

Let's see the simple example when the value of x is less than zero.

Output:

Value of x is : -1.5
log2(x) = nan

In this example, log2() function computes the logarithm value of base 2 when the value of x is less than zero.






Latest Courses