Open In App

frexp() in C++

Improve
Improve
Like Article
Like
Save
Share
Report

The frexp() function breaks the floating point number x into its binary significand i., e., floating point with an absolute value between [0.5, 1.0) and an integral exponent for 2. x = significand * (2^exponent). It used to :

1. It is used to find significand which is always between 0.5 and 1.0
2. It is used to find exponent which is power of 2.

Syntax:

double frexp (double x);
float frexp (float x);
long double frexp (long double x);
  • The frexp() function takes a single argument.
  • 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.

Error and Exception:

  1. It is mandatory to give both the arguments otherwise it will give error no matching function for call to ‘frexp()’.
  2. If we pass a string as an argument we will get an error no matching function for call to ‘frexp(const char [n]).
  3.  
    • If x=0 then significand is zero and exponent is zero
    • x >= 1 then significand is positive number and exponent is positive number
    • x <= -1 then significand is negative number and exponent is positive number
    • -1 < x < 0 then significand is negative number and exponent is negative number
    • 0 < x < 1 then significand is positive number and exponent is negative number

# CODE 1 

CPP




// CPP implementation of
// above function
#include <cmath>
#include <iostream>
 
using namespace std;
 
// Driver program
int main()
{
    double x = 5.35, significand;
    int exponent;
    significand = frexp(x, &exponent);
    cout << x << " = " << significand
         << " * 2^" << exponent << endl;
 
    return 0;
}


Output:

5.35 = 0.66875 * 2^3

# CODE 2 

CPP




// CPP implementation of the
// above function
#include <cmath>
#include <iostream>
 
using namespace std;
 
// Driver program
int main()
{
    double significand;
    int exponent, x = 5;
    significand = frexp(x, &exponent);
    cout << x << " = " << significand
         << " * 2^" << exponent << endl;
 
    return 0;
}


Output:

5 = 0.625 * 2^3


Last Updated : 14 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments