In this C++ tutorial, you will learn how to find integral part of logarithm of |x|, using ilogb() function of cmath, with syntax and examples.

C ilogb

C++ ilogb() returns integral part of logarithm of |x|, where x is the argument to ilogb() function.

Syntax

The syntax of C++ ilogb() is

ilogb(x)

where

Parameter Description
x A 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 ilogb(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 ilogb() function is

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

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

Example

In this example, we read a value from user into variable x, and find the integral part of the calculation: logarithm value of |x| with FLT_RADIX as base; using ilogb() function.

C++ Program

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

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

Output

Enter a number : 19
ilogb(19) : 4
Program ended with exit code: 0
Enter a number : -18
ilogb(-18) : 4
Program ended with exit code: 0
Enter a number : 0
ilogb(0) : -2.14748e+09
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned the syntax of C++ ilogb(), and how to use this function, with the help of examples.