frexp() Function

The frexp() function in C decomposes a floating-point number into its binary significand and an integral exponent, such that the original number can be expressed as the product of the significand and 2 raised to the power of the exponent. This function is useful for breaking down a floating-point number for further analysis or manipulation.


Syntax of frexp()

</>
Copy
double frexp(double x, int *exp);
float  frexpf(float x, int *exp);
long double frexpl(long double x, int *exp);

Parameters

ParameterDescription
xThe floating-point value to be decomposed.
expPointer to an integer where the exponent will be stored.

Some key points about the working of frexp():

  • If the input is zero, both the returned significand and the stored exponent will be zero.
  • If the input is negative, the returned significand will be negative.
  • The returned significand has an absolute value in the interval [0.5, 1.0) (except when the input is zero).

Examples for frexp()

Example 1: Decomposing a Positive Floating-Point Number

This example demonstrates how to decompose a positive number into its significand and exponent using frexp().

Program

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

int main() {
    double number = 8.0;
    int exponent;
    double significand = frexp(number, &exponent);

    printf("Number: %f\n", number);
    printf("Significand: %f\n", significand);
    printf("Exponent: %d\n", exponent);
    return 0;
}

Explanation:

  1. The variable number is initialized with the value 8.0.
  2. frexp() is called to decompose number into a significand and an exponent.
  3. The returned significand and the stored exponent are printed. For 8.0, the significand is 0.5 and the exponent is 4, since 8.0 = 0.5 * 2⁴.

Program Output:

Number: 8.000000
Significand: 0.500000
Exponent: 4

Example 2: Decomposing a Negative Floating-Point Number

This example shows how frexp() handles a negative number by returning a negative significand.

Program

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

int main() {
    double number = -10.0;
    int exponent;
    double significand = frexp(number, &exponent);

    printf("Number: %f\n", number);
    printf("Significand: %f\n", significand);
    printf("Exponent: %d\n", exponent);
    return 0;
}

Explanation:

  1. The variable number is set to -10.0.
  2. frexp() is used to split number into a significand and an exponent.
  3. The function returns a negative significand and stores the exponent such that -10.0 equals the significand multiplied by 2 raised to the stored exponent (in this case, significand -0.625 and exponent 4, since -0.625 * 2⁴ = -10.0).

Program Output:

Number: -10.000000
Significand: -0.625000
Exponent: 4

Example 3: Handling Zero as Input

This example illustrates the behavior of frexp() when the input is zero.

Program

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

int main() {
    double number = 0.0;
    int exponent;
    double significand = frexp(number, &exponent);

    printf("Number: %f\n", number);
    printf("Significand: %f\n", significand);
    printf("Exponent: %d\n", exponent);
    return 0;
}

Explanation:

  1. The variable number is set to 0.0.
  2. frexp() is called with 0.0, which results in both the significand and the exponent being zero.
  3. The output confirms that the function correctly handles zero input.

Program Output:

Number: 0.000000
Significand: 0.000000
Exponent: 0