frexp


frexp

Splits a real number into a mantissa and exponent

 #include <math.h> double frexp ( double x , int *exp  ); float frexpf ( float x , int *exp  );         (C99) long double frexpl ( long double x , 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 x 2e and 0.5 f < 1.

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;   }   return ldexp( sqrt( sqrt( mantissa )), exponent / 4 ); } 

See Also

The ldexp( ) function, which performs the reverse calculation.



C(c) In a Nutshell
C in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596006977
EAN: 2147483647
Year: 2006
Pages: 473

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net