New, improved fmod.c

This commit is contained in:
ceriel 1993-08-31 10:54:08 +00:00
parent a37e49b619
commit 668b3fc2d8

View file

@ -1,33 +1,34 @@
/* /* fmod function */
* (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands. /* Author Robert R. Hall (hall@crach.cts.com) */
* See the copyright notice in the ACK home directory, in the file "Copyright".
*
* Author: Hans van Eck
*/
/* $Header$ */ /* $Header$ */
#include <math.h> #include <math.h>
#include <errno.h> #include <errno.h>
double double
fmod(double x, double y) (fmod)(double x, double y)
{ { /* compute fmod(x, y) */
double val; double t;
double frac; int n, neg;
int ychar, xchar;
if (y == 0) { if (y == 0.0) {
errno = EDOM; errno = EDOM;
return 0; return 0.0;
} }
frac = modf( x / y, &val); /* fmod(finite, finite) */
if (y < 0.0) y = -y;
if (x < 0.0) x = -x, neg = 1;
else neg = 0;
t = frexp(y, &ychar);
/* substract |y| until |x| < |y| */
return frac * y; t = frexp(x, &xchar);
for (n = xchar - ychar; 0 <= n; --n) {
/* /* try to substract |y|*2^n */
val = x / y; t = ldexp(y, n);
if (val > LONG_MIN && val < LONG_MAX) { if (t <= x) x -= t;
long i = val;
return x - i * y;
} }
*/ return (neg ? -x : x);
} }