blob: 07283bc12da13f3f9210e9b80a78b63be591fa33 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* Portable fmod(x, y) implementation for systems that don't have it */
12
Guido van Rossum464a0a11995-02-27 10:15:10 +000013#include "config.h"
Guido van Rossum464a0a11995-02-27 10:15:10 +000014
15#include "mymath.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000016#include <errno.h>
17
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018double
Guido van Rossum464a0a11995-02-27 10:15:10 +000019fmod(double x, double y)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020{
21 double i, f;
22
23 if (y == 0.0) {
24 errno = EDOM;
25 return 0.0;
26 }
27
28 /* return f such that x = i*y + f for some integer i
29 such that |f| < |y| and f has the same sign as x */
30
31 i = floor(x/y);
32 f = x - i*y;
33 if ((x < 0.0) != (y < 0.0))
34 f = f-y;
35 return f;
36}