C frexp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, frexp() splits x into a fraction and a power of two. The call returns the fraction, usually from 0.5 up to but not including 1, and writes the exponent through a pointer. frexp(8, &exp) returns 0.5 and sets exp to 4, because 0.5 × 24 is 8.

You pass the double and a pointer to an int. Both are required. Include <math.h>.

Syntax

Pass the value and the address of an int. Start with these lines.

C
int exp;
double frac = frexp(8.0, &exp);
CallWhat it does
frexp(8.0, &exp)Returns 0.5 and stores 4 in exp.

Include <math.h>. On GCC, link with -lm. The pointer must point to an int you own.

Library declarations
C
double frexp(double x, int *exp);
float frexpf(float x, int *exp);
long double frexpl(long double x, int *exp);

Parameters

ParameterDescription
xRequired. The double to split.
expRequired. A pointer to an int. The function writes the power of two there.

Return Value

Return ValueDescription
FractionThe significand. For a normal number it is from 0.5 up to, but not including, 1, and it keeps the sign of x.

C Examples

Example 1 — Basic Split

Call frexp and print both the mantissa and the exponent.

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

int main(void) {
    int exp;
    double frac = frexp(8.0, &exp);

    printf("frac = %.1f\n", frac);
    printf("exp  = %d\n", exp);
    return 0;
}

How It Works

  1. Prepare exp. An int receives the binary exponent.
  2. Call frexp. Return value is the mantissa; &exp stores 4.
  3. Check the range. 0.5 sits at the low end of [0.5, 1).
  4. Meaning. 8 = 0.5 × 2⁴.

Example 2 — Rebuild with ldexp

ldexp is the inverse: multiply the mantissa by a power of two.

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

int main(void) {
    int exp;
    double x = 123.45;
    double frac = frexp(x, &exp);
    double back = ldexp(frac, exp);

    printf("frac = %.6f, exp = %d\n", frac, exp);
    printf("back = %.2f\n", back);
    return 0;
}

How It Works

  1. Split. frexp finds frac and exp = 7 (2⁷ = 128).
  2. Rebuild. ldexp(frac, 7) multiplies by 128.
  3. Match. The reconstructed value equals the original (within normal rounding).
  4. Pair them. Use frexp/ldexp together when scaling by powers of two.

Example 3 — Zero and Negatives

Zero is a special case. Negative inputs keep a negative mantissa with the same exponent magnitude rule.

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

int main(void) {
    int e0, e1;
    double f0 = frexp(0.0, &e0);
    double f1 = frexp(-4.0, &e1);

    printf("0  → frac=%.1f exp=%d\n", f0, e0);
    printf("-4 → frac=%.1f exp=%d\n", f1, e1);
    return 0;
}

How It Works

  1. Zero. Both parts are zero—outside the usual mantissa range.
  2. Negative. -4 = -0.5 × 2³, so the mantissa holds the sign.
  3. Same rule. Absolute mantissa still sits in [0.5, 1).
  4. Pointer required. Always pass a valid int * for the exponent.

Frequently Asked Questions

frexp() splits x into a fraction and a power of two. It returns the fraction and writes the exponent through a pointer.
Both are required. x is the double to split. exp is a pointer to an int that receives the power of two.
The significand as a double. For 8, the fraction is 0.5 and the stored exponent is 4.
Include <math.h>. On GCC, link with -lm.
No. Pass the address of an int, as in frexp(8.0, &exp).

Next: hypot()

Compute √(x² + y²) without intermediate overflow.

hypot() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful