home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / Bonus / normal / frexp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  703 b   |  47 lines

  1. #if defined(LIBC_SCCS) && !defined(lint)
  2. static char sccsid[] = "@(#)frexp.c    5.2 (Berkeley) 3/9/86";
  3. #endif LIBC_SCCS and not lint
  4.  
  5. /*
  6.  *    the call
  7.  *        x = frexp(arg,&exp);
  8.  *    must return a double fp quantity x which is <1.0
  9.  *    and the corresponding binary exponent "exp".
  10.  *    such that
  11.  *        arg = x*2^exp
  12.  *    if the argument is 0.0, return 0.0 mantissa and 0 exponent.
  13.  */
  14.  
  15. double
  16. frexp(double x, int *i)
  17. {
  18.   int neg;
  19.   int j;
  20.  
  21.   j = 0;
  22.   neg = 0;
  23.  
  24.   if (x < 0)
  25.     {
  26.       x = -x;
  27.       neg = 1;
  28.     }
  29.  
  30.   if (x >= 1.0)
  31.     while(x >= 1.0)
  32.       {
  33.     j = j+1;
  34.     x = x/2;
  35.       }
  36.   else if(x < 0.5 && x != 0.0)
  37.     while(x < 0.5)
  38.       {
  39.     j = j-1;
  40.     x = 2*x;
  41.       }
  42.  
  43.   *i = j;
  44.   if (neg) x = -x;
  45.   return x;
  46. }
  47.