9 * Floating point numeric utilities
15 * double ceil(), floor(), frexp(), ldexp();
16 * int signbit(), isnan(), isfinite();
22 * y = frexp( x, &expnt );
32 * All four routines return a double precision floating point
35 * floor() returns the largest integer less than or equal to x.
36 * It truncates toward minus infinity.
38 * ceil() returns the smallest integer greater than or equal
39 * to x. It truncates toward plus infinity.
41 * frexp() extracts the exponent from x. It returns an integer
42 * power of two to expnt and the significand between 0.5 and 1
43 * to y. Thus x = y * 2**expn.
45 * ldexp() multiplies x by 2**n.
47 * signbit(x) returns 1 if the sign bit of x is 1, else 0.
49 * These functions are part of the standard C run time library
50 * for many but not all C compilers. The ones supplied are
51 * written in C for either DEC or IEEE arithmetic. They should
52 * be used only if your compiler library does not already have
55 * The IEEE versions assume that denormal numbers are implemented
56 * in the arithmetic. Some modifications will be required if
57 * the arithmetic has abrupt rather than gradual underflow.
62 Cephes Math Library Release 2.8: June, 2000
63 Copyright 1984, 1995, 2000 by Stephen L. Moshier
70 /* ceil(), floor(), frexp(), ldexp() may need to be rewritten. */
97 extern double MAXNUM, NEGZERO;
99 double floor ( double );
100 int isnan ( double );
101 int isfinite ( double );
102 double ldexp ( double, int );
105 int isnan(), isfinite();
115 mtherr( "ceil", DOMAIN );
131 if( y == 0.0 && x < 0.0 )
140 /* Bit clearing masks: */
142 static unsigned short bmask[] = {
172 unsigned short sh[4];
178 mtherr( "floor", DOMAIN );
194 /* find the exponent (power of 2) */
196 p = (unsigned short *)&u.sh[0];
197 e = (( *p >> 7) & 0377) - 0201;
202 p = (unsigned short *)&u.sh[3];
203 e = (( *p >> 4) & 0x7ff) - 0x3ff;
208 p = (unsigned short *)&u.sh[0];
209 e = (( *p >> 4) & 0x7ff) - 0x3ff;
222 /* clean out 16 bits at a time */
239 /* clear the remaining bits */
243 if( (x < 0) && (u.y != x) )
252 double frexp( x, pw2 )
259 unsigned short sh[4];
270 mtherr( "frexp", DOMAIN );
275 q = (short *)&u.sh[3];
279 q = (short *)&u.sh[0];
283 q = (short *)&u.sh[0];
286 /* find the exponent (power of 2) */
288 i = ( *q >> 7) & 0377;
296 *q &= 0x807f; /* strip all exponent bits */
297 *q |= 040000; /* mantissa between 0.5 and 1 */
302 i = ( *q >> 4) & 0x7ff;
323 /* Number is denormal or zero */
332 /* Handle denormal number. */
337 k = ( *q >> 4) & 0x7ff;
341 #endif /* DENORMAL */
359 double ldexp( x, pw2 )
366 unsigned short sh[4];
372 mtherr( "ldexp", DOMAIN );
378 q = (short *)&u.sh[0];
379 e = ( *q >> 7) & 0377;
385 q = (short *)&u.sh[3];
388 q = (short *)&u.sh[0];
390 while( (e = (*q & 0x7ff0) >> 4) == 0 )
396 /* Input is denormal. */
416 /* Handle overflow */
422 return( 2.0*MAXNUM );
425 /* Handle denormalized results */
433 /* For denormals, significant bits may be lost even
434 when dividing by 2. Construct 2^-(1-e) so the result
435 is obtained with only one multiplication. */
436 u.y *= ldexp(1.0, e-1);
445 *q &= 0x807f; /* strip all exponent bits */
446 *q |= (e & 0xff) << 7;
449 *q |= (e & 0x7ff) << 4;