C++ logb()

C++ logb() returns logarithm of absolute value of given argument. FLT_RADIX is used as base for the logarithm calculation.

FLT_RADIX is 2 or greater, but generally 2. Please check this value before using logb() function.

Syntax

The syntax of C++ logb() is

logb(x)

where

ParameterDescription
xA double, float, long double, or any integral type value.

Returns

The return value depends on the type of value passed for parameter x.

The return value of logb(x) is

  • double if x is double or integral type.
  • float if x is float.
  • long double if x is long double.

The synopsis of logb() function is

double logb(double x);
float logb(float x);
long double logb(long double x);
double logb(T x); // for integral type argument values

logb() is a function of cmath library. Include cmath library at the start of program, if using logb() function.

Example

In this example, we read a value from user into variable x, and find the logarithm value of |x| using logb() function.

C++ Program

#include <iostream>
#include<cmath>
using namespace std;

int main() {
    double x;
    cout << "Enter a number : ";
    cin >> x;
    
    double result = logb(x);
    cout << "logb(" << x << ") : " << result << endl;
}

Output

Enter a number : 16
logb(16) : 4
Program ended with exit code: 0
Enter a number : -16
logb(-16) : 4
Program ended with exit code: 0
Enter a number : 0
logb(0) : -inf
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned the syntax of C++ logb(), and how to use this function to find the logarithm of absolute value of given number, with base value of FLT_RADIX, with the help of examples.