C Math frexp() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

The frexp() function splits a floating-point number into a mantissa (normalized fraction) and a binary exponent, so that x = mantissa × 2exp. It mirrors how computers store floats internally and pairs with ldexp() to rebuild the value.

01

Split x

Mantissa.

02

math.h

Link -lm.

03

Base 2

2^exp.

04

[0.5, 1)

Mantissa.

05

ldexp

Inverse.

06

vs modf

Different.

Definition and Usage

Think of scientific notation in base 2: any nonzero number can be written as a fraction between 0.5 and 1 times a power of two. For 123.45, frexp returns mantissa ≈ 0.964 and exponent 7, because 0.964 × 27 ≈ 123.45.

The exponent is stored through a pointer parameter int *exp. The function returns the mantissa as a double. Reassemble with ldexp(mantissa, exponent).

💡
Beginner Tip

Do not confuse frexp with modf. modf(3.75, &i) gives decimal parts 0.75 and 3. frexp gives a binary mantissa and power-of-two exponent—closer to how IEEE-754 floats work.

📝 Syntax

Standard C declaration:

C
double frexp(double x, int *exp);

Related variants

C
float frexpf(float x, int *exp);           /* <math.h> */
long double frexpl(long double x, int *exp); /* <math.h> */

Parameters

  • x — the value to decompose.
  • exp — pointer to int where the binary exponent is stored.

Return Value

  • Normalized mantissa m such that x = m × 2*exp.
  • For nonzero x, |m| is in [0.5, 1).
  • frexp(0, &exp) returns 0.0 and sets *exp to 0.

Headers and linking

  • #include <math.h>
  • Compile: gcc frexp.c -std=c11 -o frexp -lm

⚡ Quick Reference

CallMantissa (approx.)Exponent
frexp(123.45, &e)0.9647
frexp(8.0, &e)0.54
frexp(0.0, &e)0.00
ldexp(m, e)Rebuilds m × 2e
modf(3.75, &i)Decimal split (not frexp)
Decompose
frexp(x, &e)

Split x

Recompose
ldexp(m, e)

m * 2^e

Decimal
modf(x, &i)

Int + frac

Formula
x = m * pow(2,e)

Identity

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. Mantissa/exponent decomposition appears in low-level numeric code and custom float parsers.

📚 Getting Started

Extract mantissa and exponent from a floating-point value.

Example 1 — Split 123.45 into Mantissa and Exponent

Classic frexp demonstration from the reference.

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

int main(void) {
    double x = 123.45;
    int exponent;
    double mantissa = frexp(x, &exponent);

    printf("x = %.2f\n", x);
    printf("mantissa = %.6f\n", mantissa);
    printf("exponent = %d\n", exponent);
    printf("check: %.2f * 2^%d = %.2f\n",
           mantissa, exponent, ldexp(mantissa, exponent));

    return 0;
}

How It Works

0.964453 × 128 (which is 27) reconstructs 123.45. The mantissa always sits in [0.5, 1) for nonzero values.

Example 2 — frexp(0) Special Case

Zero has mantissa 0 and exponent 0.

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

int main(void) {
    int exp = -999;
    double m = frexp(0.0, &exp);

    printf("mantissa = %.1f\n", m);
    printf("exponent = %d\n", exp);

    return 0;
}

How It Works

Zero is the exception: mantissa is not in [0.5, 1). Both parts are set to zero.

📈 Practical Patterns

Rebuild with ldexp, powers of two, and negative values.

Example 3 — Reconstruct with ldexp()

Prove frexp and ldexp are inverses.

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

int main(void) {
    double values[] = { 1.0, 42.0, 0.125, 1000.0 };
    size_t i;

    for (i = 0; i < 4; i++) {
        double x = values[i];
        int exp;
        double m = frexp(x, &exp);
        double rebuilt = ldexp(m, exp);

        printf("x=%8.3f  m=%.4f  e=%3d  -> %.3f\n",
               x, m, exp, rebuilt);
    }

    return 0;
}

How It Works

ldexp(mantissa, exponent) multiplies the mantissa by 2exp and always recovers the original x.

Example 4 — Exact Power of Two (8.0)

Clean binary values produce neat mantissa/exponent pairs.

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

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

    printf("x = %.1f\n", x);
    printf("mantissa = %.1f  (because %.1f * 2^%d = %.1f)\n",
           m, m, exp, ldexp(m, exp));

    return 0;
}

How It Works

8 = 0.5 × 16 = 0.5 × 24. Powers of two often yield mantissa 0.5 with an integer exponent.

Example 5 — Negative Number Decomposition

The mantissa carries the sign; exponent stays an integer.

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

int main(void) {
    double x = -12.0;
    int exp;
    double m = frexp(x, &exp);

    printf("x = %.1f\n", x);
    printf("mantissa = %.4f\n", m);
    printf("exponent = %d\n", exp);
    printf("rebuilt = %.1f\n", ldexp(m, exp));

    return 0;
}

How It Works

−0.75 × 24 = −12. The mantissa is negative; |m| is still in [0.5, 1).

🚀 Common Use Cases

  • IEEE-754 education — understand mantissa and exponent fields.
  • Custom serialization — store floats in compact mantissa+exp form.
  • Range analysis — inspect magnitude via exponent before overflow.
  • Pair with ldexp() — scale values by powers of two efficiently.
  • Numerical algorithms — normalize operands before precision-sensitive steps.

🧠 How frexp() Works

1

Receive x

Any finite double; handle zero as special case.

Input
2

Normalize to [0.5, 1)

Shift binary point; count shifts as exponent.

Compute
3

Store exp via pointer

Write binary exponent to *exp.

Output
=

Mantissa returned

Rebuild: ldexp(mantissa, exponent).

📝 Notes

  • Identity: x = frexp(x,&e) × 2e (equivalently ldexp(frexp(...), e)).
  • Nonzero mantissa magnitude is in [0.5, 1).
  • exp must be a valid int *—not NULL.
  • frexp(NaN, &e) returns NaN; frexp(±INF, &e) returns NaN.
  • Not the same as modf() (decimal split) or logb() (exponent only).
  • Link with -lm on GCC/Clang Unix-like builds.

⚡ Optimization

frexp() is a thin wrapper over bit-level float representation on most platforms. Use it when you need true IEEE decomposition; do not hand-roll mantissa extraction unless you fully understand float internals and edge cases.

Conclusion

frexp() decomposes a double into a binary mantissa and exponent so that x = m × 2e. Pair it with ldexp() to rebuild values, and distinguish it from modf()’s decimal split.

Continue with hypot() for distance calculations, or explore ldexp() as the inverse operation.

💡 Best Practices

✅ Do

  • Pass a valid int * for the exponent
  • Rebuild with ldexp(m, e) to verify
  • Handle x == 0 as a special case in your logic
  • Use frexpf / frexpl for matching types
  • Include <math.h> and link -lm

❌ Don’t

  • Confuse frexp with modf or fmod
  • Pass NULL as the exponent pointer
  • Assume mantissa is always positive (sign follows x)
  • Expect mantissa in [0.5, 1) when x is zero
  • Ignore NaN/INF edge cases in robust code

Key Takeaways

Knowledge Unlocked

Five things to remember about frexp()

Mantissa and exponent split in C, explained simply.

5
Core concepts
📚 02

[0.5, 1)

|m| range.

Rule
📈 03

int *exp

Out param.

API
📄 04

ldexp

Inverse.

Pair
🔄 05

vs modf

Binary.

Compare

❓ Frequently Asked Questions

frexp(x, &exp) splits x into a normalized mantissa (return value) and a binary exponent (stored in *exp) such that x = mantissa * 2^exp. For example, frexp(123.45, &e) returns about 0.964 with e = 7.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double frexp(double x, int *exp).
For nonzero x, the absolute value of the returned mantissa is in [0.5, 1). For x = 0, frexp returns 0.0 and sets *exp to 0.
frexp splits x into mantissa and exponent. ldexp does the reverse: ldexp(m, e) returns m * 2^e. They are inverse operations: ldexp(frexp(x, &e), e) reconstructs x.
frexp decomposes x into a binary mantissa and power-of-two exponent (scientific-style base 2). modf splits x into a decimal integer part and fractional part. Different decompositions for different tasks.
frexp(0.0, &exp) returns 0.0 and sets *exp to 0. This is a special case outside the usual [0.5, 1) mantissa range.
Did you know?

IEEE-754 double values store a sign bit, an 11-bit exponent, and a 52-bit fraction. frexp() exposes the same idea at the math-library level: a normalized mantissa and a power-of-two exponent—without you reading raw bits.

Explore C Math Functions

Continue with hypot() or browse the full math function reference.

Math Functions Index →

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.

5 people found this page helpful