blob: b983b549e92d31a527c17d7294c554e0a1edd0e3 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000012See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000014
15******************************************************************/
16
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017/* Portable fmod(x, y) implementation for systems that don't have it */
18
Guido van Rossum464a0a11995-02-27 10:15:10 +000019#include "config.h"
Guido van Rossum464a0a11995-02-27 10:15:10 +000020
21#include "mymath.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022#include <errno.h>
23
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024double
Guido van Rossum464a0a11995-02-27 10:15:10 +000025fmod(double x, double y)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026{
27 double i, f;
28
29 if (y == 0.0) {
30 errno = EDOM;
31 return 0.0;
32 }
33
34 /* return f such that x = i*y + f for some integer i
35 such that |f| < |y| and f has the same sign as x */
36
37 i = floor(x/y);
38 f = x - i*y;
39 if ((x < 0.0) != (y < 0.0))
40 f = f-y;
41 return f;
42}