2 /* @(#)e_remainder.c 5.1 93/09/24 */
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
11 * ====================================================
16 <<remainder>>, <<remainderf>>---round and remainder
24 double remainder(double <[x]>, double <[y]>);
25 float remainderf(float <[x]>, float <[y]>);
28 <<remainder>> and <<remainderf>> find the remainder of
29 <[x]>/<[y]>; this value is in the range -<[y]>/2 .. +<[y]>/2.
32 <<remainder>> returns the integer result as a double.
35 <<remainder>> is a System V release 4.
36 <<remainderf>> is an extension.
42 * returns x REM p = x - [x/p]*p as if in infinite
43 * precise arithmetic, where [x/p] is the (infinite bit)
44 * integer nearest x/p (in half way case choose the even one).
46 * Based on fmod() return x-[x/p]chopped*p exactlp.
51 #ifndef _DOUBLE_IS_32BITS
54 static const double zero
= 0.0;
56 static double zero
= 0.0;
61 double remainder(double x
, double p
)
71 EXTRACT_WORDS(hx
,lx
,x
);
72 EXTRACT_WORDS(hp
,lp
,p
);
77 /* purge off exception values */
78 if((hp
|lp
)==0) return (x
*p
)/(x
*p
); /* p = 0 */
79 if((hx
>=0x7ff00000)|| /* x not finite */
80 ((hp
>=0x7ff00000)&& /* p is NaN */
81 (((hp
-0x7ff00000)|lp
)!=0)))
85 if (hp
<=0x7fdfffff) x
= fmod(x
,p
+p
); /* now x < 2p */
86 if (((hx
-hp
)|(lx
-lp
))==0) return zero
*x
;
102 SET_HIGH_WORD(x
,hx
^sx
);
106 #endif /* defined(_DOUBLE_IS_32BITS) */