blob: abf1d5b2dbb119cffe32ac0f1bc27b0599554d5b [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +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 Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Math module -- standard C math library functions, pi and e */
33
Barry Warsaw8b43b191996-12-09 22:32:36 +000034#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Guido van Rossum6964f731995-03-01 10:34:29 +000036#include "mymath.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000037
Guido van Rossum7081cf51996-05-23 22:56:19 +000038#ifndef _MSC_VER
Guido van Rossumb9418681995-01-12 11:28:16 +000039#ifndef __STDC__
Barry Warsaw8b43b191996-12-09 22:32:36 +000040extern double fmod Py_PROTO((double, double));
41extern double frexp Py_PROTO((double, int *));
42extern double ldexp Py_PROTO((double, int));
43extern double modf Py_PROTO((double, double *));
Guido van Rossum7081cf51996-05-23 22:56:19 +000044#endif /* __STDC__ */
45#endif /* _MSC_VER */
46
Guido van Rossumb9418681995-01-12 11:28:16 +000047
Guido van Rossum9575a441993-04-07 14:06:14 +000048#ifdef i860
49/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
50#undef HUGE_VAL
51#endif
52
Guido van Rossum8832b621991-12-16 15:44:24 +000053#ifdef HUGE_VAL
54#define CHECK(x) if (errno != 0) ; \
55 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
56 else errno = ERANGE
57#else
58#define CHECK(x) /* Don't know how to check */
59#endif
60
Barry Warsaw8b43b191996-12-09 22:32:36 +000061static PyObject *
Guido van Rossum8832b621991-12-16 15:44:24 +000062math_error()
63{
64 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000065 PyErr_SetString(PyExc_ValueError, "math domain error");
Guido van Rossum8832b621991-12-16 15:44:24 +000066 else if (errno == ERANGE)
Barry Warsaw8b43b191996-12-09 22:32:36 +000067 PyErr_SetString(PyExc_OverflowError, "math range error");
Guido van Rossum8832b621991-12-16 15:44:24 +000068 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000069 /* Unexpected math error */
70 PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum8832b621991-12-16 15:44:24 +000071 return NULL;
72}
73
Barry Warsaw8b43b191996-12-09 22:32:36 +000074static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075math_1(args, func)
Barry Warsaw8b43b191996-12-09 22:32:36 +000076 PyObject *args;
77 double (*func) Py_FPROTO((double));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078{
79 double x;
Roger E. Masse20c63811996-12-18 21:59:01 +000080 if (! PyArg_Parse(args, "d", &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081 return NULL;
82 errno = 0;
83 x = (*func)(x);
Guido van Rossum8832b621991-12-16 15:44:24 +000084 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000086 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000088 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089}
90
Barry Warsaw8b43b191996-12-09 22:32:36 +000091static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092math_2(args, func)
Barry Warsaw8b43b191996-12-09 22:32:36 +000093 PyObject *args;
94 double (*func) Py_FPROTO((double, double));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095{
96 double x, y;
Roger E. Masse20c63811996-12-18 21:59:01 +000097 if (! PyArg_Parse(args, "(dd)", &x, &y))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098 return NULL;
99 errno = 0;
100 x = (*func)(x, y);
Guido van Rossum8832b621991-12-16 15:44:24 +0000101 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000103 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000105 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106}
107
108#define FUNC1(stubname, func) \
Barry Warsaw8b43b191996-12-09 22:32:36 +0000109 static PyObject * stubname(self, args) PyObject *self, *args; { \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110 return math_1(args, func); \
111 }
112
113#define FUNC2(stubname, func) \
Barry Warsaw8b43b191996-12-09 22:32:36 +0000114 static PyObject * stubname(self, args) PyObject *self, *args; { \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 return math_2(args, func); \
116 }
117
118FUNC1(math_acos, acos)
119FUNC1(math_asin, asin)
120FUNC1(math_atan, atan)
121FUNC2(math_atan2, atan2)
122FUNC1(math_ceil, ceil)
123FUNC1(math_cos, cos)
124FUNC1(math_cosh, cosh)
125FUNC1(math_exp, exp)
Guido van Rossum6412b1d1996-08-08 19:10:21 +0000126#ifdef __MWERKS__
127double myfabs(double x) { return fabs(x); }
128FUNC1(math_fabs, myfabs)
129#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130FUNC1(math_fabs, fabs)
Guido van Rossum6412b1d1996-08-08 19:10:21 +0000131#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132FUNC1(math_floor, floor)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133FUNC2(math_fmod, fmod)
Guido van Rossum411a8bd1994-10-20 22:00:28 +0000134FUNC2(math_hypot, hypot)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135FUNC1(math_log, log)
136FUNC1(math_log10, log10)
Guido van Rossum1492c271991-07-27 21:38:43 +0000137#ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000138FUNC2(math_pow, power)
139#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140FUNC2(math_pow, pow)
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000141#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142FUNC1(math_sin, sin)
143FUNC1(math_sinh, sinh)
144FUNC1(math_sqrt, sqrt)
145FUNC1(math_tan, tan)
146FUNC1(math_tanh, tanh)
147
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148
Barry Warsaw8b43b191996-12-09 22:32:36 +0000149static PyObject *
Guido van Rossumd18ad581991-10-24 14:57:21 +0000150math_frexp(self, args)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000151 PyObject *self;
152 PyObject *args;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000153{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000154 double x;
155 int i;
Roger E. Masse20c63811996-12-18 21:59:01 +0000156 if (! PyArg_Parse(args, "d", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000157 return NULL;
158 errno = 0;
159 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000160 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000161 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000162 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000163 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000164}
165
Barry Warsaw8b43b191996-12-09 22:32:36 +0000166static PyObject *
Guido van Rossumd18ad581991-10-24 14:57:21 +0000167math_ldexp(self, args)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000168 PyObject *self;
169 PyObject *args;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000170{
171 double x, y;
172 /* Cheat -- allow float as second argument */
Roger E. Masse20c63811996-12-18 21:59:01 +0000173 if (! PyArg_Parse(args, "(dd)", &x, &y))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000174 return NULL;
175 errno = 0;
176 x = ldexp(x, (int)y);
Guido van Rossum8832b621991-12-16 15:44:24 +0000177 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000178 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000179 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000180 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000181 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000182}
183
Barry Warsaw8b43b191996-12-09 22:32:36 +0000184static PyObject *
Guido van Rossumd18ad581991-10-24 14:57:21 +0000185math_modf(self, args)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000186 PyObject *self;
187 PyObject *args;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000188{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000189 double x, y;
Roger E. Masse20c63811996-12-18 21:59:01 +0000190 if (! PyArg_Parse(args, "d", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000191 return NULL;
192 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000193#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
194{
195 extended e;
196 x = modf(x, &e);
197 y = e;
198}
199#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000200 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000201#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000202 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000203 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000204 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000205 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000206}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207
Barry Warsaw8b43b191996-12-09 22:32:36 +0000208static PyMethodDef math_methods[] = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209 {"acos", math_acos},
210 {"asin", math_asin},
211 {"atan", math_atan},
212 {"atan2", math_atan2},
213 {"ceil", math_ceil},
214 {"cos", math_cos},
215 {"cosh", math_cosh},
216 {"exp", math_exp},
217 {"fabs", math_fabs},
218 {"floor", math_floor},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219 {"fmod", math_fmod},
Guido van Rossumd18ad581991-10-24 14:57:21 +0000220 {"frexp", math_frexp},
Guido van Rossum411a8bd1994-10-20 22:00:28 +0000221 {"hypot", math_hypot},
Guido van Rossumd18ad581991-10-24 14:57:21 +0000222 {"ldexp", math_ldexp},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223 {"log", math_log},
224 {"log10", math_log10},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225 {"modf", math_modf},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226 {"pow", math_pow},
227 {"sin", math_sin},
228 {"sinh", math_sinh},
229 {"sqrt", math_sqrt},
230 {"tan", math_tan},
231 {"tanh", math_tanh},
232 {NULL, NULL} /* sentinel */
233};
234
235void
236initmath()
237{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000238 PyObject *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000239
Barry Warsaw8b43b191996-12-09 22:32:36 +0000240 m = Py_InitModule("math", math_methods);
241 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000242
243 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
244 goto finally;
245 if (PyDict_SetItemString(d, "pi", v) < 0)
246 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000247 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000248
249 if (!(v = PyFloat_FromDouble(exp(1.0))))
250 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000251 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000252 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000253 Py_DECREF(v);
Guido van Rossum4a488091996-12-18 14:14:33 +0000254 return;
Barry Warsawfc93f751996-12-17 00:47:03 +0000255
256 finally:
257 Py_FatalError("can't initialize math module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258}