Open In App

ilogb() Function in C

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The ilogb() function in C is a part of the standard math library <math.h>. It is used to compute the binary exponent of a floating-point number, which is the exponent of the value when represented as a normalized floating-point number. This function is particularly useful for numerical analysis and floating-point arithmetic.

Syntax of ilogb() in C

int ilogb(double x);

Parameters of ilogb() in C

The ilogb() function in C takes the following parameter:

  • x: The floating-point number whose binary exponent is to be computed.

Return Value of ilogb() in C

The ilogb() function returns the exponent part of the logarithm (base 2) of the floating-point number x.

  • For normalized numbers, it returns the unbiased exponent.
  • For subnormal numbers, it returns the exponent of the normalized equivalent.
  • For zero, it returns FP_ILOGB0.
  • For infinity, it returns INT_MAX.
  • For NaN, it returns FP_ILOGBNAN.

Examples of ilogb() Function in C Language

The below examples demonstrate how we can use the ilogb() function in C language.

Input: 
double x = 1.5

Output:
The binary exponent of 16.00 is: 4

Example 1

The following program demonstrates how to use the ilogb() function in C.

C
// C Program to demonstrate the ilogb() function

#include <math.h>
#include <stdio.h>

int main()
{
    // initialize the double value
    double x = 16.0;
    // call the ilogb() function
    int result = ilogb(x);

    // Print the result
    printf("The binary exponent of %.2lf is: %d\n", x,
           result);

    return 0;
}

Output
The binary exponent of 16.00 is: 4

Time Complexity: O(1)
Auxiliary Space: O(1)

Example 2

The following program demonstrates how to handle special values using ilogb() function in C.

C
// C Program demonstrating ilogb() for special values
#include <math.h>
#include <stdio.h>

int main()
{

    // initialize all the special values
    double x1 = 0.0;
    double x2 = INFINITY;
    double x3 = NAN;
    double x4 = 0.25;

    // ilogb function for various values
    int result1 = ilogb(x1);
    int result2 = ilogb(x2);
    int result3 = ilogb(x3);
    int result4 = ilogb(x4);

    // Print the results
    printf("The exponent part of log2(0.0) is: %d "
           "(FP_ILOGB0)\n",
           result1);
    printf("The exponent part of log2(INFINITY) is: %d "
           "(INT_MAX)\n",
           result2);
    printf("The exponent part of log2(NAN) is: %d "
           "(FP_ILOGBNAN)\n",
           result3);
    printf("The exponent part of log2(0.25) is: %d\n",
           result4);

    return 0;
}


Output

The exponent part of log2(0.0) is: -2147483648 (FP_ILOGB0)
The exponent part of log2(INFINITY) is: 2147483647 (INT_MAX)
The exponent part of log2(NAN) is: -2147483648 (FP_ILOGBNAN)
The exponent part of log2(0.25) is: -2

Time Complexity: O(1)
Auxiliary Space: O(1)

Example 3

The following program demonstrates how to find the binary exponent for different data types in C.

C
// C program to demonstrate the ilogb() function for
// different data types

#include <math.h>
#include <stdio.h>

int main()
{

    // initialize values of different data types
    double x1 = 16.0;
    float x2 = 25.0f;
    long double x3 = 36.0l;

    // ilogb function for double
    int result1 = ilogb(x1);

    // ilogbf function for float
    int result2 = ilogbf(x2);

    // ilogbl function for long double
    int result3 = ilogbl(x3);

    // Print the results
    printf("The binary exponent of %.2lf is: %d\n", x1,
           result1);
    printf("The binary exponent of %.2f is: %d\n", x2,
           result2);
    printf("The binary exponent of %.2Lf is: %d\n", x3,
           result3);

    return 0;
}

Output
The binary exponent of 16.00 is: 4
The binary exponent of 25.00 is: 4
The binary exponent of 36.00 is: 5

Time Complexity: O(1)
Auxiliary Space: O(1)


Next Article

Similar Reads