Name
frexp
Synopsis
Splits a real number into a mantissa and exponent
#include <math.h> doublefrexp
( doublex
, int *exp
); floatfrexpf
( floatx
, int *exp
); (C99) long doublefrexpl
( long doublex
, int *exp
); (C99)
The frexp()
function
expresses a floating-point number x
as a
normalized fraction f
and an integer
exponent e to base 2. In other words, if the
fraction f
is the return value of
frexp
(x
,
&
e),
then x
= f
×
2e and 0.5 ≤ f
< 1.
The normalized fraction is the return value of the frexp()
function. The function places the
other part of its “answer,” the exponent, in the location addressed
by the pointer argument. If the floating-point argument x
is equal to 0, then the function stores
the value 0 at the exponent location and returns 0.
Example
double fourthrt( double x ) { int exponent, exp_mod_4; double mantissa =frexp
( x, &exponent ); exp_mod_4 = exponent % 4; exponent -= ( exp_mod_4 ); // Get an exponent that's divisible by four ... for ( int i = abs( exp_mod_4 ); i > 0; i− ) { if ( exp_mod_4 > 0 ) // ... and compensate in the mantissa. mantissa *= 2.0; else mantissa /= 2.0; } returnldexp
( sqrt( sqrt( mantissa )), exponent / 4 ); }
See Also
The ldexp()
function,
which performs the reverse calculation.
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.