1993-08-31 10:54:08 +00:00
|
|
|
/* fmod function */
|
|
|
|
/* Author Robert R. Hall (hall@crach.cts.com) */
|
|
|
|
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1989-12-18 15:44:36 +00:00
|
|
|
|
1993-08-31 10:54:08 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <errno.h>
|
1989-12-18 15:44:36 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
double(fmod)(double x, double y)
|
|
|
|
{ /* compute fmod(x, y) */
|
1993-08-31 10:54:08 +00:00
|
|
|
double t;
|
|
|
|
int n, neg;
|
|
|
|
int ychar, xchar;
|
1989-12-18 15:44:36 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (y == 0.0)
|
|
|
|
{
|
1989-12-18 15:44:36 +00:00
|
|
|
errno = EDOM;
|
1993-08-31 10:54:08 +00:00
|
|
|
return 0.0;
|
2018-06-21 20:33:47 +00:00
|
|
|
}
|
|
|
|
/* fmod(finite, finite) */
|
|
|
|
if (y < 0.0)
|
|
|
|
y = -y;
|
|
|
|
if (x < 0.0)
|
|
|
|
x = -x, neg = 1;
|
|
|
|
else
|
|
|
|
neg = 0;
|
1993-08-31 10:54:08 +00:00
|
|
|
t = frexp(y, &ychar);
|
2018-06-21 20:33:47 +00:00
|
|
|
/* substract |y| until |x| < |y| */
|
1989-12-18 15:44:36 +00:00
|
|
|
|
1993-08-31 10:54:08 +00:00
|
|
|
t = frexp(x, &xchar);
|
2018-06-21 20:33:47 +00:00
|
|
|
for (n = xchar - ychar; 0 <= n; --n)
|
|
|
|
{
|
|
|
|
/* try to substract |y|*2^n */
|
|
|
|
t = ldexp(y, n);
|
|
|
|
if (t <= x)
|
|
|
|
x -= t;
|
1989-12-18 15:44:36 +00:00
|
|
|
}
|
1993-08-31 10:54:08 +00:00
|
|
|
return (neg ? -x : x);
|
1989-12-18 15:44:36 +00:00
|
|
|
}
|