blob: ed4e9c268b063ac10b37ac7b88551347fd54951f [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 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/* Math module -- standard C math library functions, pi and e */
18
Barry Warsaw8b43b191996-12-09 22:32:36 +000019#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Guido van Rossum6964f731995-03-01 10:34:29 +000021#include "mymath.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000022
Guido van Rossum7081cf51996-05-23 22:56:19 +000023#ifndef _MSC_VER
Guido van Rossumb9418681995-01-12 11:28:16 +000024#ifndef __STDC__
Barry Warsaw8b43b191996-12-09 22:32:36 +000025extern double fmod Py_PROTO((double, double));
26extern double frexp Py_PROTO((double, int *));
27extern double ldexp Py_PROTO((double, int));
28extern double modf Py_PROTO((double, double *));
Guido van Rossum7081cf51996-05-23 22:56:19 +000029#endif /* __STDC__ */
30#endif /* _MSC_VER */
31
Guido van Rossumb9418681995-01-12 11:28:16 +000032
Guido van Rossum9575a441993-04-07 14:06:14 +000033#ifdef i860
34/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
35#undef HUGE_VAL
36#endif
37
Guido van Rossum8832b621991-12-16 15:44:24 +000038#ifdef HUGE_VAL
39#define CHECK(x) if (errno != 0) ; \
40 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
41 else errno = ERANGE
42#else
43#define CHECK(x) /* Don't know how to check */
44#endif
45
Barry Warsaw8b43b191996-12-09 22:32:36 +000046static PyObject *
Guido van Rossum8832b621991-12-16 15:44:24 +000047math_error()
48{
49 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000050 PyErr_SetString(PyExc_ValueError, "math domain error");
Guido van Rossum8832b621991-12-16 15:44:24 +000051 else if (errno == ERANGE)
Barry Warsaw8b43b191996-12-09 22:32:36 +000052 PyErr_SetString(PyExc_OverflowError, "math range error");
Guido van Rossum8832b621991-12-16 15:44:24 +000053 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000054 /* Unexpected math error */
55 PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum8832b621991-12-16 15:44:24 +000056 return NULL;
57}
58
Barry Warsaw8b43b191996-12-09 22:32:36 +000059static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060math_1(args, func)
Barry Warsaw8b43b191996-12-09 22:32:36 +000061 PyObject *args;
62 double (*func) Py_FPROTO((double));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063{
64 double x;
Roger E. Masse20c63811996-12-18 21:59:01 +000065 if (! PyArg_Parse(args, "d", &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 return NULL;
67 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000068 PyFPE_START_PROTECT("in math_1", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +000070 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000071 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000073 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000075 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076}
77
Barry Warsaw8b43b191996-12-09 22:32:36 +000078static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079math_2(args, func)
Barry Warsaw8b43b191996-12-09 22:32:36 +000080 PyObject *args;
81 double (*func) Py_FPROTO((double, double));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082{
83 double x, y;
Roger E. Masse20c63811996-12-18 21:59:01 +000084 if (! PyArg_Parse(args, "(dd)", &x, &y))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 return NULL;
86 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000087 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000089 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000090 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000092 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000094 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095}
96
Guido van Rossumc6e22901998-12-04 19:26:43 +000097#define FUNC1(stubname, func, docstring_name, docstring) \
Barry Warsaw8b43b191996-12-09 22:32:36 +000098 static PyObject * stubname(self, args) PyObject *self, *args; { \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099 return math_1(args, func); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000100 }\
101 static char docstring_name [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102
Guido van Rossumc6e22901998-12-04 19:26:43 +0000103#define FUNC2(stubname, func, docstring_name, docstring) \
Barry Warsaw8b43b191996-12-09 22:32:36 +0000104 static PyObject * stubname(self, args) PyObject *self, *args; { \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105 return math_2(args, func); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000106 }\
107 static char docstring_name [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108
Guido van Rossumc6e22901998-12-04 19:26:43 +0000109FUNC1(math_acos, acos, math_acos_doc,
110 "acos(x)\n\nReturn the arc cosine of x.")
111FUNC1(math_asin, asin, math_asin_doc,
112 "asin(x)\n\nReturn the arc sine of x.")
113FUNC1(math_atan, atan, math_atan_doc,
114 "atan(x)\n\nReturn the arc tangent of x.")
115FUNC2(math_atan2, atan2, math_atan2_doc,
Guido van Rossumb057dd81998-12-08 16:27:10 +0000116 "atan2(y, x)\n\nReturn atan(y/x).")
Guido van Rossumc6e22901998-12-04 19:26:43 +0000117FUNC1(math_ceil, ceil, math_ceil_doc,
118 "ceil(x)\n\nReturn the ceiling of x as a real.")
119FUNC1(math_cos, cos, math_cos_doc,
120 "cos(x)\n\nReturn the cosine of x.")
121FUNC1(math_cosh, cosh, math_cosh_doc,
122 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
123FUNC1(math_exp, exp, math_exp_doc,
124 "exp(x)\n\nReturn e raised to the power of x.")
125FUNC1(math_fabs, fabs, math_fabs_doc,
126 "fabs(x)\n\nReturn the absolute value of the real x.")
127FUNC1(math_floor, floor, math_floor_doc,
128 "floor(x)\n\nReturn the floor of x as a real.")
129FUNC2(math_fmod, fmod, math_fmod_doc,
130 "fmod(x,y)\n\nReturn x % y.")
131FUNC2(math_hypot, hypot, math_hypot_doc,
132 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
133FUNC1(math_log, log, math_log_doc,
134 "log(x)\n\nReturn the natural logarithm of x.")
135FUNC1(math_log10, log10, math_log10_doc,
136 "log10(x)\n\nReturn the base-10 logarithm of x.")
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 Rossumc6e22901998-12-04 19:26:43 +0000138FUNC2(math_pow, power, math_pow_doc,
139 "power(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000140#else
Guido van Rossumc6e22901998-12-04 19:26:43 +0000141FUNC2(math_pow, pow, math_pow_doc,
142 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000143#endif
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000144#ifdef HAVE_RINT
Guido van Rossum71260b82000-05-11 18:19:42 +0000145FUNC1(math_rint, rint, math_rint_doc,
146 "rint(x)\n\nReturn the integer nearest to x as a real.")
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000147#endif
Guido van Rossumc6e22901998-12-04 19:26:43 +0000148FUNC1(math_sin, sin, math_sin_doc,
149 "sin(x)\n\nReturn the sine of x.")
150FUNC1(math_sinh, sinh, math_sinh_doc,
151 "sinh(x)\n\nReturn the hyperbolic sine of x.")
152FUNC1(math_sqrt, sqrt, math_sqrt_doc,
153 "sqrt(x)\n\nReturn the square root of x.")
154FUNC1(math_tan, tan, math_tan_doc,
155 "tan(x)\n\nReturn the tangent of x.")
156FUNC1(math_tanh, tanh, math_tanh_doc,
157 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159
Barry Warsaw8b43b191996-12-09 22:32:36 +0000160static PyObject *
Guido van Rossumd18ad581991-10-24 14:57:21 +0000161math_frexp(self, args)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000162 PyObject *self;
163 PyObject *args;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000164{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000165 double x;
166 int i;
Roger E. Masse20c63811996-12-18 21:59:01 +0000167 if (! PyArg_Parse(args, "d", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000168 return NULL;
169 errno = 0;
170 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000171 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000172 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000173 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000174 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000175}
176
Guido van Rossumc6e22901998-12-04 19:26:43 +0000177static char math_frexp_doc [] =
178"frexp(x)\n\
179\n\
180Return the matissa and exponent for x. The mantissa is positive.";
181
182
Barry Warsaw8b43b191996-12-09 22:32:36 +0000183static PyObject *
Guido van Rossumd18ad581991-10-24 14:57:21 +0000184math_ldexp(self, args)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000185 PyObject *self;
186 PyObject *args;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000187{
Guido van Rossumc5545052000-05-08 14:29:38 +0000188 double x;
189 int exp;
190 if (! PyArg_Parse(args, "(di)", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000191 return NULL;
192 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000193 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000194 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000195 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +0000196 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000197 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000198 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000199 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000200 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000201}
202
Guido van Rossumc6e22901998-12-04 19:26:43 +0000203static char math_ldexp_doc [] =
204"ldexp_doc(x, i)\n\
205\n\
206Return x * (2**i).";
207
208
Barry Warsaw8b43b191996-12-09 22:32:36 +0000209static PyObject *
Guido van Rossumd18ad581991-10-24 14:57:21 +0000210math_modf(self, args)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000211 PyObject *self;
212 PyObject *args;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000213{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000214 double x, y;
Roger E. Masse20c63811996-12-18 21:59:01 +0000215 if (! PyArg_Parse(args, "d", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000216 return NULL;
217 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
219{
220 extended e;
221 x = modf(x, &e);
222 y = e;
223}
224#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000225 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000227 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000228 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000229 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000230 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000231}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232
Guido van Rossumc6e22901998-12-04 19:26:43 +0000233static char math_modf_doc [] =
234"modf(x)\n\
235\n\
236Return the fractional and integer parts of x. Both results carry the sign\n\
237of x. The integer part is returned as a real.";
238
239
Barry Warsaw8b43b191996-12-09 22:32:36 +0000240static PyMethodDef math_methods[] = {
Guido van Rossumc6e22901998-12-04 19:26:43 +0000241 {"acos", math_acos, 0, math_acos_doc},
242 {"asin", math_asin, 0, math_asin_doc},
243 {"atan", math_atan, 0, math_atan_doc},
244 {"atan2", math_atan2, 0, math_atan2_doc},
245 {"ceil", math_ceil, 0, math_ceil_doc},
246 {"cos", math_cos, 0, math_cos_doc},
247 {"cosh", math_cosh, 0, math_cosh_doc},
248 {"exp", math_exp, 0, math_exp_doc},
249 {"fabs", math_fabs, 0, math_fabs_doc},
250 {"floor", math_floor, 0, math_floor_doc},
251 {"fmod", math_fmod, 0, math_fmod_doc},
252 {"frexp", math_frexp, 0, math_frexp_doc},
253 {"hypot", math_hypot, 0, math_hypot_doc},
254 {"ldexp", math_ldexp, 0, math_ldexp_doc},
255 {"log", math_log, 0, math_log_doc},
256 {"log10", math_log10, 0, math_log10_doc},
257 {"modf", math_modf, 0, math_modf_doc},
258 {"pow", math_pow, 0, math_pow_doc},
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000259#ifdef HAVE_RINT
Guido van Rossum71260b82000-05-11 18:19:42 +0000260 {"rint", math_rint, 0, math_rint_doc},
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000261#endif
Guido van Rossumc6e22901998-12-04 19:26:43 +0000262 {"sin", math_sin, 0, math_sin_doc},
263 {"sinh", math_sinh, 0, math_sinh_doc},
264 {"sqrt", math_sqrt, 0, math_sqrt_doc},
265 {"tan", math_tan, 0, math_tan_doc},
266 {"tanh", math_tanh, 0, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267 {NULL, NULL} /* sentinel */
268};
269
Guido van Rossumc6e22901998-12-04 19:26:43 +0000270
271static char module_doc [] =
272"This module is always available. It provides access to the\n\
273mathematical functions defined by the C standard.";
274
Guido van Rossum3886bb61998-12-04 18:50:17 +0000275DL_EXPORT(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276initmath()
277{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000278 PyObject *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000279
Guido van Rossumc6e22901998-12-04 19:26:43 +0000280 m = Py_InitModule3("math", math_methods, module_doc);
Barry Warsaw8b43b191996-12-09 22:32:36 +0000281 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000282
283 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
284 goto finally;
285 if (PyDict_SetItemString(d, "pi", v) < 0)
286 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000287 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000288
289 if (!(v = PyFloat_FromDouble(exp(1.0))))
290 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000291 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000292 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000293 Py_DECREF(v);
Guido van Rossum4a488091996-12-18 14:14:33 +0000294 return;
Barry Warsawfc93f751996-12-17 00:47:03 +0000295
296 finally:
297 Py_FatalError("can't initialize math module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298}