1 /* origin: FreeBSD /usr/src/lib/msun/src/e_remainder.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunSoft, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
10 * ====================================================
14 * returns x REM p = x - [x/p]*p as if in infinite
15 * precise arithmetic, where [x/p] is the (infinite bit)
16 * integer nearest x/p (in half way case choose the even one).
18 * Based on fmod() return x-[x/p]chopped*p exactlp.
23 static const double zero = 0.0;
25 double remainder(double x, double p)
31 EXTRACT_WORDS(hx, lx, x);
32 EXTRACT_WORDS(hp, lp, p);
37 /* purge off exception values */
38 if ((hp|lp) == 0) /* p = 0 */
40 if (hx >= 0x7ff00000 || /* x not finite */
41 (hp >= 0x7ff00000 && (hp-0x7ff00000 | lp) != 0)) /* p is NaN */
42 // FIXME: why long double?
43 return ((long double)x*p)/((long double)x*p);
46 x = fmod(x, p+p); /* now x < 2p */
47 if (((hx-hp)|(lx-lp)) == 0)
51 if (hp < 0x00200000) {
66 if ((hx&0x7fffffff) == 0)
68 SET_HIGH_WORD(x, hx^sx);