Home | History | Annotate | Line # | Download | only in noieee_src
n_fmod.c revision 1.1
      1 /*	$NetBSD: n_fmod.c,v 1.1 1995/10/10 23:36:49 ragge Exp $	*/
      2 /*
      3  * Copyright (c) 1989, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by the University of
     17  *	California, Berkeley and its contributors.
     18  * 4. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #ifndef lint
     36 static char sccsid[] = "@(#)fmod.c	8.1 (Berkeley) 6/4/93";
     37 #endif /* not lint */
     38 
     39 #include "mathimpl.h"
     40 
     41 /* fmod.c
     42  *
     43  * SYNOPSIS
     44  *
     45  *    #include <math.h>
     46  *    double fmod(double x, double y)
     47  *
     48  * DESCRIPTION
     49  *
     50  *    The fmod function computes the floating-point remainder of x/y.
     51  *
     52  * RETURNS
     53  *
     54  *    The fmod function returns the value x-i*y, for some integer i
     55  * such that, if y is nonzero, the result has the same sign as x and
     56  * magnitude less than the magnitude of y.
     57  *
     58  * On a VAX or CCI,
     59  *
     60  *    fmod(x,0) traps/faults on floating-point divided-by-zero.
     61  *
     62  * On IEEE-754 conforming machines with "isnan()" primitive,
     63  *
     64  *    fmod(x,0), fmod(INF,y) are invalid operations and NaN is returned.
     65  *
     66  */
     67 #if !defined(vax) && !defined(tahoe)
     68 extern int isnan(),finite();
     69 #endif	/* !defined(vax) && !defined(tahoe) */
     70 extern double frexp(),ldexp(),fabs();
     71 
     72 #ifdef TEST_FMOD
     73 static double
     74 _fmod(x,y)
     75 #else	/* TEST_FMOD */
     76 double
     77 fmod(x,y)
     78 #endif	/* TEST_FMOD */
     79 double x,y;
     80 {
     81 	int ir,iy;
     82 	double r,w;
     83 
     84 	if (y == (double)0
     85 #if !defined(vax) && !defined(tahoe)	/* per "fmod" manual entry, SunOS 4.0 */
     86 		|| isnan(y) || !finite(x)
     87 #endif	/* !defined(vax) && !defined(tahoe) */
     88 	    )
     89 	    return (x*y)/(x*y);
     90 
     91 	r = fabs(x);
     92 	y = fabs(y);
     93 	(void)frexp(y,&iy);
     94 	while (r >= y) {
     95 		(void)frexp(r,&ir);
     96 		w = ldexp(y,ir-iy);
     97 		r -= w <= r ? w : w*(double)0.5;
     98 	}
     99 	return x >= (double)0 ? r : -r;
    100 }
    101 
    102 #ifdef TEST_FMOD
    103 extern long random();
    104 extern double fmod();
    105 
    106 #define	NTEST	10000
    107 #define	NCASES	3
    108 
    109 static int nfail = 0;
    110 
    111 static void
    112 doit(x,y)
    113 double x,y;
    114 {
    115 	double ro = fmod(x,y),rn = _fmod(x,y);
    116 	if (ro != rn) {
    117 		(void)printf(" x    = 0x%08.8x %08.8x (%24.16e)\n",x,x);
    118 		(void)printf(" y    = 0x%08.8x %08.8x (%24.16e)\n",y,y);
    119 		(void)printf(" fmod = 0x%08.8x %08.8x (%24.16e)\n",ro,ro);
    120 		(void)printf("_fmod = 0x%08.8x %08.8x (%24.16e)\n",rn,rn);
    121 		(void)printf("\n");
    122 	}
    123 }
    124 
    125 main()
    126 {
    127 	register int i,cases;
    128 	double x,y;
    129 
    130 	srandom(12345);
    131 	for (i = 0; i < NTEST; i++) {
    132 		x = (double)random();
    133 		y = (double)random();
    134 		for (cases = 0; cases < NCASES; cases++) {
    135 			switch (cases) {
    136 			case 0:
    137 				break;
    138 			case 1:
    139 				y = (double)1/y; break;
    140 			case 2:
    141 				x = (double)1/x; break;
    142 			default:
    143 				abort(); break;
    144 			}
    145 			doit(x,y);
    146 			doit(x,-y);
    147 			doit(-x,y);
    148 			doit(-x,-y);
    149 		}
    150 	}
    151 	if (nfail)
    152 		(void)printf("Number of failures: %d (out of a total of %d)\n",
    153 			nfail,NTEST*NCASES*4);
    154 	else
    155 		(void)printf("No discrepancies were found\n");
    156 	exit(0);
    157 }
    158 #endif	/* TEST_FMOD */
    159