C++ Standard Library C++ STL Library

C++ <cmath> - frexp() Function



The C++ <cmath> frexp() function is used to break the floating point number x into its binary significand (a floating point with an absolute value in range [0.5, 1.0)) and an integral exponent for 2. Mathematically, it can be expressed as:

x = significand * 2exponent

The exponent is stored in the object pointed by exp, and the function returns the significand. If x is zero, both significand and exponent are zero. If x is negative, the significand returned by the function is negative.

Syntax

double frexp (double x, int* exp);
float frexp (float x, int* exp);
long double frexp (long double x, int* exp);
double frexp (double x, int* exp);
float frexp (float x, int* exp);
long double frexp (long double x, int* exp);
//additional overloads for integral types
double frexp (T x, int* exp); 

Parameters

x Specify the value to be decomposed.
exp Specify the pointer to an int object to store the exponent.

Return Value

The binary significand of the x.

Example:

The example below shows the usage of frexp() function.

#include <iostream>
#include <cmath>
using namespace std;
 
int main (){
  double x, sig;
  int exp;
  x = 10;

  sig = frexp(x, &exp);

  cout<<"Number: "<<x<<"\n";
  cout<<"Significand: "<<sig<<"\n";
  cout<<"Exponent: "<<exp<<"\n";
  return 0;
}

The output of the above code will be:

Number: 10
Significand: 0.625
Exponent: 4

❮ C++ <cmath> Library

5