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]>);
29 double remainder(<[x]>,<[y]>)
31 float remainderf(<[x]>,<[y]>)
35 <<remainder>> and <<remainderf>> find the remainder of
36 <[x]>/<[y]>; this value is in the range -<[y]>/2 .. +<[y]>/2.
39 <<remainder>> returns the integer result as a double.
42 <<remainder>> is a System V release 4.
43 <<remainderf>> is an extension.
49 * returns x REM p = x - [x/p]*p as if in infinite
50 * precise arithmetic, where [x/p] is the (infinite bit)
51 * integer nearest x/p (in half way case choose the even one).
53 * Based on fmod() return x-[x/p]chopped*p exactlp.
58 #ifndef _DOUBLE_IS_32BITS
61 static const double zero
= 0.0;
63 static double zero
= 0.0;
68 double remainder(double x
, double p
)
78 EXTRACT_WORDS(hx
,lx
,x
);
79 EXTRACT_WORDS(hp
,lp
,p
);
84 /* purge off exception values */
85 if((hp
|lp
)==0) return (x
*p
)/(x
*p
); /* p = 0 */
86 if((hx
>=0x7ff00000)|| /* x not finite */
87 ((hp
>=0x7ff00000)&& /* p is NaN */
88 (((hp
-0x7ff00000)|lp
)!=0)))
92 if (hp
<=0x7fdfffff) x
= fmod(x
,p
+p
); /* now x < 2p */
93 if (((hx
-hp
)|(lx
-lp
))==0) return zero
*x
;
105 if(x
>=p_half
) x
-= p
;
109 SET_HIGH_WORD(x
,hx
^sx
);
113 #endif /* defined(_DOUBLE_IS_32BITS) */