In C, frexp() splits x into a fraction and a power of two. The call returns the fraction, usually from 0.5 up to but not including 1, and writes the exponent through a pointer. frexp(8, &exp) returns 0.5 and sets exp to 4, because 0.5 × 24 is 8.
You pass the double and a pointer to an int. Both are required. Include <math.h>.
Syntax
Pass the value and the address of an int. Start with these lines.
C
int exp;
double frac = frexp(8.0, &exp);
Call
What it does
frexp(8.0, &exp)
Returns 0.5 and stores 4 in exp.
Include <math.h>. On GCC, link with -lm. The pointer must point to an int you own.
Library declarations
C
double frexp(double x, int *exp);
float frexpf(float x, int *exp);
long double frexpl(long double x, int *exp);
Parameters
Parameter
Description
x
Required. The double to split.
exp
Required. A pointer to an int. The function writes the power of two there.
Return Value
Return Value
Description
Fraction
The significand. For a normal number it is from 0.5 up to, but not including, 1, and it keeps the sign of x.
C Examples
Example 1 — Basic Split
Call frexp and print both the mantissa and the exponent.