home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / LDFLOOR.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  46 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* ldfloor() -- long double floor
  4. ** public domain by Raymond Gardner  Englewood, CO
  5. ** tested with TC++
  6. ** assumptions: 80-bit IEEE format numbers, stored LSB first 
  7. **   (Intel style), longs & ints are accessed from arbitrary boundaries
  8. */
  9.  
  10. #include "snipmath.h"
  11.  
  12. long double ldfloor(long double a)
  13. {
  14.     long double a0;
  15.     int e, n;
  16.    
  17.     a0 = a;
  18.     e = ((int *)&a)[4] & 0x7FFF;        /* extract exponent         */
  19.     if ( e == 0 )                       /* 0 is special case        */
  20.         return (long double) 0.0;
  21.     e -= 16383;                         /* unbias exponent          */
  22.     if (e < 0)                          /* if < 0, num is < 1,...   */
  23.     {
  24.         a = 0.0;                        /* so floor is zero         */
  25.     }
  26.     else if ((n = 63 - e) > 0)          /* clear n least sig. bits  */
  27.     {
  28.         if (n < 32)                     /* clear n lowest bits      */
  29.         {
  30.             ((unsigned long *)&a)[0] &= ~((1L << n) - 1);
  31.         }
  32.         else                            /* n >= 32 */
  33.         {
  34.             ((unsigned long *)&a)[0] = 0; /* clear lower 32 bits            */
  35.             n -= 32;                    /* how many left to clear ?         */
  36.             if (n)                      /* if any, clear n next lowest bits */
  37.             {
  38.                 ((unsigned long *)&a)[1] &= ~((1L << n) - 1);
  39.             }
  40.         }
  41.     }
  42.     if (a0 < 0 && a0 != a)          /* if neg. and it had fractional bits */
  43.         a -= 1.0;                   /* adjust the floor                   */
  44.     return a;                       /* return it                          */
  45. }
  46.