ilogb() function in C++ STL


In this article, we will be discussing the working, syntax, and examples of ilogb() function in C++.

What is ilogb()?

ilogb() function is an inbuilt function in C++ STL, which is defined in <cmath< header file. ilogb() is used to find the integer part of the logarithm value. ilogb() means integer binary logarithm.

This function returns an integral part of the logarithm of |x| using the FLT_RADIX as the base for logarithm.

Syntax

int ilogb(double x);

Parameters

The function accepts the following parameter(s) −

  • x− This is the value whose logarithm we have to find.

Return value

This function returns the integral logarithm of |x|, using the value of FLT_RADIX as the base value. This function also throws an exception according to the parameter’s value.

If the parameter value is −

  • NaN − Then the function return FP_LOGBNAN.

  • Infinite − Then the function return INT_MAX.

  • 0 − Then the function returns FP_LOGB0

Input

ilogb(2);

Output 

1

Example

 Live Demo

#include <cfloat>
#include <cmath>
#include >iostream>
using namespace std;
int main(){
   int output, var = 2;
   output = ilogb(var);
   cout << "The value of ilogb(" << var << ") is: " << output << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

The value of ilogb(2) is: 1

Example

 Live Demo

#include <cfloat>
#include <cmath>
#include <iostream>
#include <iostream>
using namespace std;
int main(){
   int output, var = 10.23;
   output = ilogb(var);
   cout << "The value of ilogb(" << var << ") is: " << output<< endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

The value of ilogb(10) is: 3

Updated on: 22-Apr-2020

27 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements