blob: de88f83ef6398c53610c4cd11ed4b357efdd556e [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Portable fmod(x, y) implementation for systems that don't have it */
3
Guido van Rossum464a0a11995-02-27 10:15:10 +00004#include "config.h"
Guido van Rossum464a0a11995-02-27 10:15:10 +00005
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +00006#include "pyport.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include <errno.h>
8
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009double
Guido van Rossum464a0a11995-02-27 10:15:10 +000010fmod(double x, double y)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011{
12 double i, f;
13
14 if (y == 0.0) {
15 errno = EDOM;
16 return 0.0;
17 }
18
19 /* return f such that x = i*y + f for some integer i
20 such that |f| < |y| and f has the same sign as x */
21
22 i = floor(x/y);
23 f = x - i*y;
24 if ((x < 0.0) != (y < 0.0))
25 f = f-y;
26 return f;
27}