C++ std::frexp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::frexp(x, &e) breaks a number into two parts: a fraction and a power of two. Every double can be written as m * 2e, where m is between 0.5 and 1. For 8 that is 0.5 * 24.

The function returns the fraction m and stores the exponent e in the int that you pass by address. The opposite function is std::ldexp(), which puts the parts back together. The sign of x goes to the fraction.

std::frexp() is declared in <cmath> and is part of the group Remainder, scale, and compare. These functions split a number, scale it by a power of two, find its remainder, or pick between two numbers. They work directly on the binary form of a double.

Syntax

Include <cmath> and call std::frexp. Start with these lines.

C++
int e;
double m = std::frexp(8.0, &e);    // m = 0.5,   e = 4
double n = std::frexp(10.0, &e);   // n = 0.625, e = 4
CallWhat it does
std::frexp(8.0, &e)Returns 0.5 and stores 4. 0.5 * 24 = 8.
std::frexp(10.0, &e)Returns 0.625 and stores 4. 0.625 * 16 = 10.
std::frexp(0.1, &e)Returns 0.8 and stores -3. 0.8 * 2-3 = 0.1.

The second argument is a pointer to an int, so write &e. Rebuild the number with std::ldexp(m, e).

Library declarations
C++
float       frexp(float x, int* exp);
double      frexp(double x, int* exp);
long double frexp(long double x, int* exp);
double      frexp(Integer x, int* exp);   // for integer types

Parameters

ParameterDescription
xRequired. The number to split. It can be float, double, long double, or an integer type.
expRequired. A pointer to an int. The power of two is stored here.

Return Value

Return ValueDescription
FractionThe value m with 0.5 <= |m| < 1 and the sign of x, so that x = m * 2exp.
0For x = 0 the fraction is 0 and *exp is 0.
UnspecifiedFor infinity and NaN the fraction is infinity or NaN, and *exp is unspecified.

C++ Examples

Example 1 — Split Several Numbers

Print each number as a fraction times a power of two. Negative and zero values are included.

C++
#include <cmath>
#include <iostream>

int main() {
    double values[] = {8.0, 10.0, 0.1, -3.0, 0.0};

    for (double v : values) {
        int e;
        double m = std::frexp(v, &e);
        std::cout << v << " = " << m << " * 2^" << e << '\n';
    }
    return 0;
}

How It Works

  1. The exponent is stored in e, and the fraction m is the return value.
  2. 8 gives 0.5 * 24 and 10 gives 0.625 * 24. The fraction is always from 0.5 up to, but not including, 1.
  3. 0.1 is smaller than one, so the exponent is negative: 0.8 * 2-3. -3 keeps its sign in the fraction (-0.75), and 0 gives 0 with exponent 0.

Example 2 — Rebuild a Number with ldexp()

Split a number with frexp() and join the parts with ldexp(). You get the exact original back.

C++
#include <cmath>
#include <iostream>

int main() {
    double x = 12.375;
    int e;

    double m = std::frexp(x, &e);
    double back = std::ldexp(m, e);

    std::cout << x << " = " << m << " * 2^" << e << '\n';
    std::cout << "ldexp gives back " << back << '\n';
    std::cout << "Same value: " << (back == x) << '\n';
    return 0;
}

How It Works

  1. 12.375 lies between 8 and 16, so the exponent is 4 and the fraction is 12.375 / 16 = 0.773438 (shown with six digits).
  2. std::ldexp(m, e) multiplies m by 2e. Multiplying by a power of two only moves the exponent, so no rounding happens.
  3. The comparison back == x prints 1. Splitting and joining a double in this way is exact.

Example 3 — How Many Binary Digits Does a Number Have

The exponent from frexp() is the number of binary digits of the whole part. Check it for three numbers.

C++
#include <cmath>
#include <iostream>

int main() {
    for (double n : {255.0, 256.0, 1000.0}) {
        int e;
        std::frexp(n, &e);
        std::cout << n << " needs " << e << " bit(s)\n";
    }
    return 0;
}

How It Works

  1. The returned fraction is not needed here, so the call ignores it and only keeps the exponent in e.
  2. 255 is 0.996 * 28, so it needs 8 bits. 256 is exactly 0.5 * 29, so it needs 9.
  3. 1000 is 0.977 * 210, so it needs 10 bits. This works without a log, and it never has rounding trouble at powers of two.

Frequently Asked Questions

std::frexp(x, &e) splits x into a fraction m and a power of two, so that x = m * 2^e. The fraction m is between 0.5 and 1 (or between -1 and -0.5), and e is stored as an int.
The fraction m, with 0.5 <= |m| < 1. The exponent e is stored through the pointer. For 0, both are 0, and for infinity or NaN the result is unspecified.
The fraction is from 0.5 up to, but not including, 1 for positive numbers (and the same size with a minus sign for negatives). The exponent is chosen so that fraction * 2^exp equals x.
std::ldexp(m, e) multiplies m by 2 to the power e. So ldexp(frexp(x, &e), e) gives back x.
Include <cmath> and write std::frexp(). That always works and picks the C++ overloads. The global name ::frexp often works too, but the standard does not promise it, and it may only take a double.

Next: hypot()

Keep going through the <cmath> functions. Length of the hypotenuse of a right triangle.

hypot() tutorial →

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.

15 people found this page helpful