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

Math Logb Function

BLUF: Mastering Math Logb 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 Logb Function

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

C++ Math logb

The function calculates the logarithm of a specified number, utilizing FLT_RADX as the base for the logarithm.

Typically, FLT_RADX equals 2. As a result, the function logb is the same as log2.

Syntax

Suppose a number is 'x'. Syntax would be:

Example

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

Parameter

x : The value for which the logarithm needs to be determined.

Return value

It returns base FLT_RADX logarithm of x.

If x equals zero, it could potentially lead to a domain or singularity error based on how the library is implemented.

Example 1

Let's consider a straightforward scenario where the variable x is of type integer.

Example

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

Output:

Output

Value of x is :6
logarithm value of x is:2

In this instance, x is set to 6. The logb method calculates the logarithm of x with a base of FLT_RADX.

Example 2

Let's examine a basic scenario where the variable x is of the float data type.

Example

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

Output:

Output

Value of x is :10.4
logarithm value of x is:3

In this instance, the value assigned to x is 10.4. The logb method calculates the logarithm of x with a base of FLT_RADX.

Example 3

Let's examine a straightforward 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<<"logarithm value of x is : "<<logb(x);
     return 0;
}

Output:

Output

Value of x is  :0
logarithm value of x is : -inf

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