1*84d9c625SLionel Sambuc /* $NetBSD: n_fmod.c,v 1.7 2013/11/22 10:59:31 martin Exp $ */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras * Copyright (c) 1989, 1993
42fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras * modification, are permitted provided that the following conditions
82fe8fb19SBen Gras * are met:
92fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras * without specific prior written permission.
172fe8fb19SBen Gras *
182fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras * SUCH DAMAGE.
292fe8fb19SBen Gras */
302fe8fb19SBen Gras
312fe8fb19SBen Gras #ifndef lint
322fe8fb19SBen Gras #if 0
332fe8fb19SBen Gras static char sccsid[] = "@(#)fmod.c 8.1 (Berkeley) 6/4/93";
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras #endif /* not lint */
362fe8fb19SBen Gras
372fe8fb19SBen Gras #include "mathimpl.h"
382fe8fb19SBen Gras
392fe8fb19SBen Gras /* fmod.c
402fe8fb19SBen Gras *
412fe8fb19SBen Gras * SYNOPSIS
422fe8fb19SBen Gras *
432fe8fb19SBen Gras * #include <math.h>
442fe8fb19SBen Gras * double fmod(double x, double y)
452fe8fb19SBen Gras *
462fe8fb19SBen Gras * DESCRIPTION
472fe8fb19SBen Gras *
482fe8fb19SBen Gras * The fmod function computes the floating-point remainder of x/y.
492fe8fb19SBen Gras *
502fe8fb19SBen Gras * RETURNS
512fe8fb19SBen Gras *
522fe8fb19SBen Gras * The fmod function returns the value x-i*y, for some integer i
532fe8fb19SBen Gras * such that, if y is nonzero, the result has the same sign as x and
542fe8fb19SBen Gras * magnitude less than the magnitude of y.
552fe8fb19SBen Gras *
562fe8fb19SBen Gras * On a VAX or CCI,
572fe8fb19SBen Gras *
582fe8fb19SBen Gras * fmod(x,0) traps/faults on floating-point divided-by-zero.
592fe8fb19SBen Gras *
602fe8fb19SBen Gras * On IEEE-754 conforming machines with "isnan()" primitive,
612fe8fb19SBen Gras *
622fe8fb19SBen Gras * fmod(x,0), fmod(INF,y) are invalid operations and NaN is returned.
632fe8fb19SBen Gras *
642fe8fb19SBen Gras */
652fe8fb19SBen Gras #if !defined(__vax__) && !defined(tahoe)
662fe8fb19SBen Gras extern int isnan(),finite();
672fe8fb19SBen Gras #endif /* !defined(__vax__) && !defined(tahoe) */
682fe8fb19SBen Gras
69*84d9c625SLionel Sambuc #if DBL_MANT_DIG == LDBL_MANT_DIG && DBL_MIN_EXP == LDBL_MIN_EXP \
70*84d9c625SLionel Sambuc && DBL_MIN_EXP == LDBL_MIN_EXP
71*84d9c625SLionel Sambuc #ifdef __weak_alias
72*84d9c625SLionel Sambuc __weak_alias(fmodl, fmod);
73*84d9c625SLionel Sambuc #endif
74*84d9c625SLionel Sambuc #endif
75*84d9c625SLionel Sambuc
762fe8fb19SBen Gras #ifdef TEST_FMOD
772fe8fb19SBen Gras static double
_fmod(double x,double y)782fe8fb19SBen Gras _fmod(double x, double y)
792fe8fb19SBen Gras #else /* TEST_FMOD */
802fe8fb19SBen Gras double
812fe8fb19SBen Gras fmod(double x, double y)
822fe8fb19SBen Gras #endif /* TEST_FMOD */
832fe8fb19SBen Gras {
842fe8fb19SBen Gras int ir,iy;
852fe8fb19SBen Gras double r,w;
862fe8fb19SBen Gras
872fe8fb19SBen Gras if (y == (double)0
882fe8fb19SBen Gras #if !defined(__vax__) && !defined(tahoe) /* per "fmod" manual entry, SunOS 4.0 */
892fe8fb19SBen Gras || isnan(y) || !finite(x)
902fe8fb19SBen Gras #endif /* !defined(__vax__) && !defined(tahoe) */
912fe8fb19SBen Gras )
922fe8fb19SBen Gras return (x*y)/(x*y);
932fe8fb19SBen Gras
942fe8fb19SBen Gras r = fabs(x);
952fe8fb19SBen Gras y = fabs(y);
962fe8fb19SBen Gras (void)frexp(y,&iy);
972fe8fb19SBen Gras while (r >= y) {
982fe8fb19SBen Gras (void)frexp(r,&ir);
992fe8fb19SBen Gras w = ldexp(y,ir-iy);
1002fe8fb19SBen Gras r -= w <= r ? w : w*(double)0.5;
1012fe8fb19SBen Gras }
1022fe8fb19SBen Gras return x >= (double)0 ? r : -r;
1032fe8fb19SBen Gras }
1042fe8fb19SBen Gras
105*84d9c625SLionel Sambuc float
fmodf(float x,float y)106*84d9c625SLionel Sambuc fmodf(float x, float y)
107*84d9c625SLionel Sambuc {
108*84d9c625SLionel Sambuc return fmod(x, y);
109*84d9c625SLionel Sambuc }
110*84d9c625SLionel Sambuc
1112fe8fb19SBen Gras #ifdef TEST_FMOD
1122fe8fb19SBen Gras extern long random();
1132fe8fb19SBen Gras extern double fmod();
1142fe8fb19SBen Gras
1152fe8fb19SBen Gras #define NTEST 10000
1162fe8fb19SBen Gras #define NCASES 3
1172fe8fb19SBen Gras
1182fe8fb19SBen Gras static int nfail = 0;
1192fe8fb19SBen Gras
1202fe8fb19SBen Gras static void
doit(double x,double y)1212fe8fb19SBen Gras doit(double x, double y)
1222fe8fb19SBen Gras {
1232fe8fb19SBen Gras double ro = fmod(x,y),rn = _fmod(x,y);
1242fe8fb19SBen Gras if (ro != rn) {
1252fe8fb19SBen Gras (void)printf(" x = 0x%08.8x %08.8x (%24.16e)\n",x,x);
1262fe8fb19SBen Gras (void)printf(" y = 0x%08.8x %08.8x (%24.16e)\n",y,y);
1272fe8fb19SBen Gras (void)printf(" fmod = 0x%08.8x %08.8x (%24.16e)\n",ro,ro);
1282fe8fb19SBen Gras (void)printf("_fmod = 0x%08.8x %08.8x (%24.16e)\n",rn,rn);
1292fe8fb19SBen Gras (void)printf("\n");
1302fe8fb19SBen Gras }
1312fe8fb19SBen Gras }
1322fe8fb19SBen Gras
1332fe8fb19SBen Gras int
main(int argc,char ** argv)1342fe8fb19SBen Gras main(int argc, char **argv)
1352fe8fb19SBen Gras {
1362fe8fb19SBen Gras int i,cases;
1372fe8fb19SBen Gras double x,y;
1382fe8fb19SBen Gras
1392fe8fb19SBen Gras srandom(12345);
1402fe8fb19SBen Gras for (i = 0; i < NTEST; i++) {
1412fe8fb19SBen Gras x = (double)random();
1422fe8fb19SBen Gras y = (double)random();
1432fe8fb19SBen Gras for (cases = 0; cases < NCASES; cases++) {
1442fe8fb19SBen Gras switch (cases) {
1452fe8fb19SBen Gras case 0:
1462fe8fb19SBen Gras break;
1472fe8fb19SBen Gras case 1:
1482fe8fb19SBen Gras y = (double)1/y; break;
1492fe8fb19SBen Gras case 2:
1502fe8fb19SBen Gras x = (double)1/x; break;
1512fe8fb19SBen Gras default:
1522fe8fb19SBen Gras abort(); break;
1532fe8fb19SBen Gras }
1542fe8fb19SBen Gras doit(x,y);
1552fe8fb19SBen Gras doit(x,-y);
1562fe8fb19SBen Gras doit(-x,y);
1572fe8fb19SBen Gras doit(-x,-y);
1582fe8fb19SBen Gras }
1592fe8fb19SBen Gras }
1602fe8fb19SBen Gras if (nfail)
1612fe8fb19SBen Gras (void)printf("Number of failures: %d (out of a total of %d)\n",
1622fe8fb19SBen Gras nfail,NTEST*NCASES*4);
1632fe8fb19SBen Gras else
1642fe8fb19SBen Gras (void)printf("No discrepancies were found\n");
1652fe8fb19SBen Gras exit(0);
1662fe8fb19SBen Gras }
1672fe8fb19SBen Gras #endif /* TEST_FMOD */
168