blob: e8ef9909543ae8c9e51fe8af4ea399ed92c408cc [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/* Math module -- standard C math library functions, pi and e */
12
Barry Warsaw8b43b191996-12-09 22:32:36 +000013#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Guido van Rossum6964f731995-03-01 10:34:29 +000015#include "mymath.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000016
Guido van Rossum7081cf51996-05-23 22:56:19 +000017#ifndef _MSC_VER
Guido van Rossumb9418681995-01-12 11:28:16 +000018#ifndef __STDC__
Barry Warsaw8b43b191996-12-09 22:32:36 +000019extern double fmod Py_PROTO((double, double));
20extern double frexp Py_PROTO((double, int *));
21extern double ldexp Py_PROTO((double, int));
22extern double modf Py_PROTO((double, double *));
Guido van Rossum7081cf51996-05-23 22:56:19 +000023#endif /* __STDC__ */
24#endif /* _MSC_VER */
25
Guido van Rossumb9418681995-01-12 11:28:16 +000026
Guido van Rossum9575a441993-04-07 14:06:14 +000027#ifdef i860
28/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
29#undef HUGE_VAL
30#endif
31
Guido van Rossum8832b621991-12-16 15:44:24 +000032#ifdef HUGE_VAL
33#define CHECK(x) if (errno != 0) ; \
34 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
35 else errno = ERANGE
36#else
37#define CHECK(x) /* Don't know how to check */
38#endif
39
Barry Warsaw8b43b191996-12-09 22:32:36 +000040static PyObject *
Guido van Rossum8832b621991-12-16 15:44:24 +000041math_error()
42{
43 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000044 PyErr_SetString(PyExc_ValueError, "math domain error");
Guido van Rossum8832b621991-12-16 15:44:24 +000045 else if (errno == ERANGE)
Barry Warsaw8b43b191996-12-09 22:32:36 +000046 PyErr_SetString(PyExc_OverflowError, "math range error");
Guido van Rossum8832b621991-12-16 15:44:24 +000047 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000048 /* Unexpected math error */
49 PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum8832b621991-12-16 15:44:24 +000050 return NULL;
51}
52
Barry Warsaw8b43b191996-12-09 22:32:36 +000053static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054math_1(args, func)
Barry Warsaw8b43b191996-12-09 22:32:36 +000055 PyObject *args;
56 double (*func) Py_FPROTO((double));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057{
58 double x;
Roger E. Masse20c63811996-12-18 21:59:01 +000059 if (! PyArg_Parse(args, "d", &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060 return NULL;
61 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000062 PyFPE_START_PROTECT("in math_1", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +000064 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000065 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000067 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000069 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070}
71
Barry Warsaw8b43b191996-12-09 22:32:36 +000072static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073math_2(args, func)
Barry Warsaw8b43b191996-12-09 22:32:36 +000074 PyObject *args;
75 double (*func) Py_FPROTO((double, double));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076{
77 double x, y;
Roger E. Masse20c63811996-12-18 21:59:01 +000078 if (! PyArg_Parse(args, "(dd)", &x, &y))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 return NULL;
80 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000081 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000083 PyFPE_END_PROTECT(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
Guido van Rossumc6e22901998-12-04 19:26:43 +000091#define FUNC1(stubname, func, docstring_name, docstring) \
Barry Warsaw8b43b191996-12-09 22:32:36 +000092 static PyObject * stubname(self, args) PyObject *self, *args; { \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 return math_1(args, func); \
Guido van Rossumc6e22901998-12-04 19:26:43 +000094 }\
95 static char docstring_name [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096
Guido van Rossumc6e22901998-12-04 19:26:43 +000097#define FUNC2(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_2(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 +0000103FUNC1(math_acos, acos, math_acos_doc,
104 "acos(x)\n\nReturn the arc cosine of x.")
105FUNC1(math_asin, asin, math_asin_doc,
106 "asin(x)\n\nReturn the arc sine of x.")
107FUNC1(math_atan, atan, math_atan_doc,
108 "atan(x)\n\nReturn the arc tangent of x.")
109FUNC2(math_atan2, atan2, math_atan2_doc,
Guido van Rossumb057dd81998-12-08 16:27:10 +0000110 "atan2(y, x)\n\nReturn atan(y/x).")
Guido van Rossumc6e22901998-12-04 19:26:43 +0000111FUNC1(math_ceil, ceil, math_ceil_doc,
112 "ceil(x)\n\nReturn the ceiling of x as a real.")
113FUNC1(math_cos, cos, math_cos_doc,
114 "cos(x)\n\nReturn the cosine of x.")
115FUNC1(math_cosh, cosh, math_cosh_doc,
116 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
117FUNC1(math_exp, exp, math_exp_doc,
118 "exp(x)\n\nReturn e raised to the power of x.")
119FUNC1(math_fabs, fabs, math_fabs_doc,
120 "fabs(x)\n\nReturn the absolute value of the real x.")
121FUNC1(math_floor, floor, math_floor_doc,
122 "floor(x)\n\nReturn the floor of x as a real.")
123FUNC2(math_fmod, fmod, math_fmod_doc,
124 "fmod(x,y)\n\nReturn x % y.")
125FUNC2(math_hypot, hypot, math_hypot_doc,
126 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
127FUNC1(math_log, log, math_log_doc,
128 "log(x)\n\nReturn the natural logarithm of x.")
129FUNC1(math_log10, log10, math_log10_doc,
130 "log10(x)\n\nReturn the base-10 logarithm of x.")
Guido van Rossum1492c271991-07-27 21:38:43 +0000131#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 +0000132FUNC2(math_pow, power, math_pow_doc,
133 "power(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000134#else
Guido van Rossumc6e22901998-12-04 19:26:43 +0000135FUNC2(math_pow, pow, math_pow_doc,
136 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000137#endif
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000138#ifdef HAVE_RINT
Guido van Rossum71260b82000-05-11 18:19:42 +0000139FUNC1(math_rint, rint, math_rint_doc,
140 "rint(x)\n\nReturn the integer nearest to x as a real.")
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000141#endif
Guido van Rossumc6e22901998-12-04 19:26:43 +0000142FUNC1(math_sin, sin, math_sin_doc,
143 "sin(x)\n\nReturn the sine of x.")
144FUNC1(math_sinh, sinh, math_sinh_doc,
145 "sinh(x)\n\nReturn the hyperbolic sine of x.")
146FUNC1(math_sqrt, sqrt, math_sqrt_doc,
147 "sqrt(x)\n\nReturn the square root of x.")
148FUNC1(math_tan, tan, math_tan_doc,
149 "tan(x)\n\nReturn the tangent of x.")
150FUNC1(math_tanh, tanh, math_tanh_doc,
151 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152
Guido van Rossumb6775db1994-08-01 11:34:53 +0000153
Barry Warsaw8b43b191996-12-09 22:32:36 +0000154static PyObject *
Guido van Rossumd18ad581991-10-24 14:57:21 +0000155math_frexp(self, args)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000156 PyObject *self;
157 PyObject *args;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000158{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000159 double x;
160 int i;
Roger E. Masse20c63811996-12-18 21:59:01 +0000161 if (! PyArg_Parse(args, "d", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000162 return NULL;
163 errno = 0;
164 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000165 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000166 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000167 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000168 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000169}
170
Guido van Rossumc6e22901998-12-04 19:26:43 +0000171static char math_frexp_doc [] =
172"frexp(x)\n\
173\n\
Tim Petersc9c02c42000-07-02 20:17:08 +0000174Return the matissa and exponent of x, as pair (m, e).\n\
175m is a float and e is an int, such that x = m * 2.**e.\n\
176If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000177
178
Barry Warsaw8b43b191996-12-09 22:32:36 +0000179static PyObject *
Guido van Rossumd18ad581991-10-24 14:57:21 +0000180math_ldexp(self, args)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000181 PyObject *self;
182 PyObject *args;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000183{
Guido van Rossumc5545052000-05-08 14:29:38 +0000184 double x;
185 int exp;
186 if (! PyArg_Parse(args, "(di)", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000187 return NULL;
188 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000189 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000190 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000191 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +0000192 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000193 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000194 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000195 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000196 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000197}
198
Guido van Rossumc6e22901998-12-04 19:26:43 +0000199static char math_ldexp_doc [] =
200"ldexp_doc(x, i)\n\
201\n\
202Return x * (2**i).";
203
204
Barry Warsaw8b43b191996-12-09 22:32:36 +0000205static PyObject *
Guido van Rossumd18ad581991-10-24 14:57:21 +0000206math_modf(self, args)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000207 PyObject *self;
208 PyObject *args;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000209{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000210 double x, y;
Roger E. Masse20c63811996-12-18 21:59:01 +0000211 if (! PyArg_Parse(args, "d", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000212 return NULL;
213 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
215{
216 extended e;
217 x = modf(x, &e);
218 y = e;
219}
220#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000221 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000223 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000224 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000225 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000226 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000227}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228
Guido van Rossumc6e22901998-12-04 19:26:43 +0000229static char math_modf_doc [] =
230"modf(x)\n\
231\n\
232Return the fractional and integer parts of x. Both results carry the sign\n\
233of x. The integer part is returned as a real.";
234
235
Barry Warsaw8b43b191996-12-09 22:32:36 +0000236static PyMethodDef math_methods[] = {
Guido van Rossumc6e22901998-12-04 19:26:43 +0000237 {"acos", math_acos, 0, math_acos_doc},
238 {"asin", math_asin, 0, math_asin_doc},
239 {"atan", math_atan, 0, math_atan_doc},
240 {"atan2", math_atan2, 0, math_atan2_doc},
241 {"ceil", math_ceil, 0, math_ceil_doc},
242 {"cos", math_cos, 0, math_cos_doc},
243 {"cosh", math_cosh, 0, math_cosh_doc},
244 {"exp", math_exp, 0, math_exp_doc},
245 {"fabs", math_fabs, 0, math_fabs_doc},
246 {"floor", math_floor, 0, math_floor_doc},
247 {"fmod", math_fmod, 0, math_fmod_doc},
248 {"frexp", math_frexp, 0, math_frexp_doc},
249 {"hypot", math_hypot, 0, math_hypot_doc},
250 {"ldexp", math_ldexp, 0, math_ldexp_doc},
251 {"log", math_log, 0, math_log_doc},
252 {"log10", math_log10, 0, math_log10_doc},
253 {"modf", math_modf, 0, math_modf_doc},
254 {"pow", math_pow, 0, math_pow_doc},
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000255#ifdef HAVE_RINT
Guido van Rossum71260b82000-05-11 18:19:42 +0000256 {"rint", math_rint, 0, math_rint_doc},
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000257#endif
Guido van Rossumc6e22901998-12-04 19:26:43 +0000258 {"sin", math_sin, 0, math_sin_doc},
259 {"sinh", math_sinh, 0, math_sinh_doc},
260 {"sqrt", math_sqrt, 0, math_sqrt_doc},
261 {"tan", math_tan, 0, math_tan_doc},
262 {"tanh", math_tanh, 0, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263 {NULL, NULL} /* sentinel */
264};
265
Guido van Rossumc6e22901998-12-04 19:26:43 +0000266
267static char module_doc [] =
268"This module is always available. It provides access to the\n\
269mathematical functions defined by the C standard.";
270
Guido van Rossum3886bb61998-12-04 18:50:17 +0000271DL_EXPORT(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272initmath()
273{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000274 PyObject *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000275
Guido van Rossumc6e22901998-12-04 19:26:43 +0000276 m = Py_InitModule3("math", math_methods, module_doc);
Barry Warsaw8b43b191996-12-09 22:32:36 +0000277 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000278
279 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
280 goto finally;
281 if (PyDict_SetItemString(d, "pi", v) < 0)
282 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000283 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000284
285 if (!(v = PyFloat_FromDouble(exp(1.0))))
286 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000287 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000288 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000289 Py_DECREF(v);
Guido van Rossum4a488091996-12-18 14:14:33 +0000290 return;
Barry Warsawfc93f751996-12-17 00:47:03 +0000291
292 finally:
293 Py_FatalError("can't initialize math module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294}