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 *
Note: A floating point number whose absolute value lies between
0.5and1is 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 variablesdouble x = 7.24, significand;int *exp;// using the frexp() functionsignificand = 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, usingxandexpas the parameter values of the function. We assign the output of the result to the variablesignifciand. -
Line 14: We print the variable
xalongside the variablessignificandandexp.