C++ frexp()

The frexp() function in C++ breaks a floating point number into its binary significand.

The binary significand is a floating point whose absolute value (mantissa) lies in the interval [0.5, 1) and an integer exponent for 2.

The function is defined in <cmath> header file.

Mathematically,

x = Binary significand * 2exponent

where, exponent is stored in the location pointed by exp and the Binary significand is the value returned by frexp().


frexp() prototype [As of C++ 11 standard]

double frexp (double x, int* exp);
float frexp (float x, int* exp);
long double frexp (long double x, int* exp);
double frexp (T x, int* exp); // For integral type

The frexp() function takes two arguments and returns the binary significand value of type double, float or long double.


frexp() Parameters

  • x - The value to be decomposed.
  • exp - The pointer to an integer where the value of exponent is to be stored.

frexp() Return value

The frexp() function returns the binary significand whose absolute value lies in the interval [0.5, 1). If x is zero, both significand and exponent are zero.

frexp() return values
Parameter (x) Binary Significand Exponent
0 0 0
x >= 1 Positive Positive
x <= -1 Negative Positive
-1 < x < 0 Negative Negative
0 < x < 1 Positive Negative

Example 1: How frexp() function works in C++?

#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
	double x = 6.81, significand;
	int *exp;
	significand = frexp(x , exp);
	cout << x << " = " << significand << " * 2^" << *exp << endl;

	return 0;
}

When you run the program, the output will be:

6.81 = 0.85125 * 2^3

Example 2: frexp() function with integral type

#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
	double significand;
	int *exp, x = 25;
	significand = frexp (x , exp);
	cout << x << " = " << significand << " * 2^" << *exp << endl;
	return 0;
}

When you run the program, the output will be:

25 = 0.78125 * 2^5
Did you find this article helpful?