What is the frexp() function in C++?

Overview

In C++, the frexp() function is used to return the multiple of the binary significand and 2 raised to the power of the exponent. In simpler words, the frexp() function returns the binary significand value of a floating point number.

Mathematically:

frexp(x, exp) => x = binary significand * 2exponent2^{exponent}

Note: A floating point number whose absolute value lies between 0.5 and 1 is referred to as the binary significand.

Syntax

double frexp (double x, int* exp);
float frexp (float x, int* exp);
long double frexp (long double x, int* exp);

Parameter

The frexp() function takes two parameter values:

  • x: This represents the value that needs to be decomposed.
  • exp: This represents the pointer to an integer where the exponent’s value is stored.

Return value

The frexp() function returns the binary signinificand value of type double, float, or long double. These absolute values lie between 0.5 and 1. If the given x parameter is 0, then both the significand and the exponent values are 0.

Code example

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
// creatimg our variables
double x = 7.24, significand;
int *exp;
// using the frexp() function
significand = frexp(x , exp);
cout << x << " = " << significand << " * 2^" << *exp << endl;
return 0;
}

Code explanation

  • Lines 9–10: We create our variables x, significand, and *exp.

  • Line 13: We implement the frexp() function, using x and exp as the parameter values of the function. We assign the output of the result to the variable signifciand.

  • Line 14: We print the variable x alongside the variables significand and exp.

Free Resources