C++ Standard Library C++ STL Library

C++ <cmath> - ilogb() Function



The C++ <cmath> ilogb() function returns the integer part of the logarithm of |x| (mod of argument), using FLT_RADIX as base for the logarithm. On most platforms, FLT_RADIX is 2.

In special cases, the following two specific macros are returned by the function:

MacrosDescription
FP_ILOGB0 Returned when x is zero.
FP_ILOGBNAN Returned when x is NaN.

Syntax

int ilogb (double x);
int ilogb (float x);
int ilogb (long double x);
int ilogb (T x);                   

Parameters

x Specify the value to calculate ilogb.

Return Value

Returns the integer part of the logarithm of |x|, using FLT_RADIX as base for the logarithm. In special cases, it returns the following:

  • FP_ILOGB0, if i is zero.
  • INT_MAX, if x is infinity.
  • FP_ILOGBNAN, if x is a NaN.
  • If the result is too large to be represented by a value of return type, the function returns an unspecified value, and an overflow range error occurs.

Example:

The example below shows the usage of ilogb() function.

#include <iostream>
#include <cmath>

using namespace std;
 
int main (){
  cout<<"ilogb(10) = "<<ilogb(10)<<"\n";
  cout<<"ilogb(-10) = "<<ilogb(-10)<<"\n";
  return 0;
}

The output of the above code will be:

ilogb(10) = 3
ilogb(-10) = 3

❮ C++ <cmath> Library