Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Portable fmod(x, y) implementation for systems that don't have it */ |
| 3 | |
Guido van Rossum | 464a0a1 | 1995-02-27 10:15:10 +0000 | [diff] [blame] | 4 | #include "config.h" |
Guido van Rossum | 464a0a1 | 1995-02-27 10:15:10 +0000 | [diff] [blame] | 5 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 6 | #include "pyport.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | double |
Guido van Rossum | 464a0a1 | 1995-02-27 10:15:10 +0000 | [diff] [blame] | 10 | fmod(double x, double y) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | { |
| 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 | } |