Math Log2 Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Log2 Function

Math Log2 Function

BLUF: Mastering Math Log2 Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Math Log2 Function

C++ is renowned for its efficiency. Learn how Math Log2 Function enables low-level control and high-performance computing in the tutorial below.

C++ Math log2

The function calculates the binary logarithm of a specified number.

Suppose a number is 'x':

Example

log2(x) = log2x;

Syntax

Example

float log2(float x);
double log2(double x);
long double log2(long double x);
double log2(integral x);

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

Parameter

x : The value for which the logarithm is to be computed.

Return value

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

Example 1

Let's consider a basic scenario where the value of x exceeds one.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x=2;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log2(x) = "<<log2(x);
     return 0;}

Output:

Output

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

In this instance, the log2 function calculates the logarithmic value with a base of 2 for x values exceeding one.

Example 2

Let's consider a straightforward scenario where the value assigned to variable x is 1.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x=1;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log2(x) = "<<log2(x);
     return 0;
}.

Output:

Output

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

In this instance, the log2 function calculates the logarithm with a base of 2 given the input x equals one.

Example 3

Let's consider a straightforward scenario where the value of x falls within the range of 0 to 1.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     float x=0.2;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log2(x) = "<<log2(x);
     return 0;
}

Output:

Output

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

In this instance, the log2 function calculates the logarithmic value with a base of 2 for x = 0.2.

Example 4

Let's examine a basic scenario where the value of x equals zero.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x=0;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log2(x) = "<<log2(x);
     return 0;
}

Output:

Output

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

In this instance, the log2 function calculates the logarithmic value with a base of 2 for x when x equals 0.

Example 5

Let's consider a basic scenario where the value of x is negative.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     float x= -1.50;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log2(x) = "<<log2(x);
     return 0;
}

Output:

Output

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

In this instance, the log2 function calculates the logarithm value with a base of 2 for x values below zero.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience