blob: 569e8c9cdda526f3e3d49490d81ee74b50208cd1 [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 Rossum7081cf51996-05-23 22:56:19 +000015#ifndef _MSC_VER
Guido van Rossumb9418681995-01-12 11:28:16 +000016#ifndef __STDC__
Fred Drake40c48682000-07-03 18:11:56 +000017extern double fmod (double, double);
18extern double frexp (double, int *);
19extern double ldexp (double, int);
20extern double modf (double, double *);
Guido van Rossum7081cf51996-05-23 22:56:19 +000021#endif /* __STDC__ */
22#endif /* _MSC_VER */
23
Guido van Rossumb9418681995-01-12 11:28:16 +000024
Guido van Rossum9575a441993-04-07 14:06:14 +000025#ifdef i860
26/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
27#undef HUGE_VAL
28#endif
29
Guido van Rossum8832b621991-12-16 15:44:24 +000030#ifdef HUGE_VAL
31#define CHECK(x) if (errno != 0) ; \
32 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
33 else errno = ERANGE
34#else
35#define CHECK(x) /* Don't know how to check */
36#endif
37
Barry Warsaw8b43b191996-12-09 22:32:36 +000038static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000039math_error(void)
Guido van Rossum8832b621991-12-16 15:44:24 +000040{
41 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000042 PyErr_SetString(PyExc_ValueError, "math domain error");
Guido van Rossum8832b621991-12-16 15:44:24 +000043 else if (errno == ERANGE)
Barry Warsaw8b43b191996-12-09 22:32:36 +000044 PyErr_SetString(PyExc_OverflowError, "math range error");
Guido van Rossum8832b621991-12-16 15:44:24 +000045 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000046 /* Unexpected math error */
47 PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum8832b621991-12-16 15:44:24 +000048 return NULL;
49}
50
Barry Warsaw8b43b191996-12-09 22:32:36 +000051static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000052math_1(PyObject *args, double (*func) (double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053{
54 double x;
Fred Drake40c48682000-07-03 18:11:56 +000055 if (! PyArg_ParseTuple(args, argsfmt, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056 return NULL;
57 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000058 PyFPE_START_PROTECT("in math_1", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +000060 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000061 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000063 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000065 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066}
67
Barry Warsaw8b43b191996-12-09 22:32:36 +000068static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000069math_2(PyObject *args, double (*func) (double, double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070{
71 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +000072 if (! PyArg_ParseTuple(args, argsfmt, &x, &y))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073 return NULL;
74 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000075 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000077 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000078 CHECK(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +000080 return math_error();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000082 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083}
84
Fred Drake40c48682000-07-03 18:11:56 +000085#define FUNC1(funcname, func, docstring) \
86 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
87 return math_1(args, func, "d:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +000088 }\
Fred Drake40c48682000-07-03 18:11:56 +000089 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090
Fred Drake40c48682000-07-03 18:11:56 +000091#define FUNC2(funcname, func, docstring) \
92 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
93 return math_2(args, func, "dd:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +000094 }\
Fred Drake40c48682000-07-03 18:11:56 +000095 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096
Fred Drake40c48682000-07-03 18:11:56 +000097FUNC1(acos, acos,
Guido van Rossumc6e22901998-12-04 19:26:43 +000098 "acos(x)\n\nReturn the arc cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +000099FUNC1(asin, asin,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000100 "asin(x)\n\nReturn the arc sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000101FUNC1(atan, atan,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000102 "atan(x)\n\nReturn the arc tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000103FUNC2(atan2, atan2,
Guido van Rossumb057dd81998-12-08 16:27:10 +0000104 "atan2(y, x)\n\nReturn atan(y/x).")
Fred Drake40c48682000-07-03 18:11:56 +0000105FUNC1(ceil, ceil,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000106 "ceil(x)\n\nReturn the ceiling of x as a real.")
Fred Drake40c48682000-07-03 18:11:56 +0000107FUNC1(cos, cos,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000108 "cos(x)\n\nReturn the cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000109FUNC1(cosh, cosh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000110 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000111FUNC1(exp, exp,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000112 "exp(x)\n\nReturn e raised to the power of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000113FUNC1(fabs, fabs,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000114 "fabs(x)\n\nReturn the absolute value of the real x.")
Fred Drake40c48682000-07-03 18:11:56 +0000115FUNC1(floor, floor,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000116 "floor(x)\n\nReturn the floor of x as a real.")
Fred Drake40c48682000-07-03 18:11:56 +0000117 FUNC2(fmod, fmod,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000118 "fmod(x,y)\n\nReturn x % y.")
Fred Drake40c48682000-07-03 18:11:56 +0000119FUNC2(hypot, hypot,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000120 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
Fred Drake40c48682000-07-03 18:11:56 +0000121FUNC1(log, log,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000122 "log(x)\n\nReturn the natural logarithm of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000123FUNC1(log10, log10,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000124 "log10(x)\n\nReturn the base-10 logarithm of x.")
Guido van Rossum1492c271991-07-27 21:38:43 +0000125#ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
Fred Drake40c48682000-07-03 18:11:56 +0000126FUNC2(pow, power,
127 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000128#else
Fred Drake40c48682000-07-03 18:11:56 +0000129FUNC2(pow, pow,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000130 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000131#endif
Fred Drake40c48682000-07-03 18:11:56 +0000132FUNC1(sin, sin,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000133 "sin(x)\n\nReturn the sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000134FUNC1(sinh, sinh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000135 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000136FUNC1(sqrt, sqrt,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000137 "sqrt(x)\n\nReturn the square root of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000138FUNC1(tan, tan,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000139 "tan(x)\n\nReturn the tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000140FUNC1(tanh, tanh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000141 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142
Guido van Rossumb6775db1994-08-01 11:34:53 +0000143
Barry Warsaw8b43b191996-12-09 22:32:36 +0000144static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000145math_frexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000146{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000147 double x;
148 int i;
Fred Drake40c48682000-07-03 18:11:56 +0000149 if (! PyArg_ParseTuple(args, "d:frexp", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000150 return NULL;
151 errno = 0;
152 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000153 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000154 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000155 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000156 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000157}
158
Guido van Rossumc6e22901998-12-04 19:26:43 +0000159static char math_frexp_doc [] =
160"frexp(x)\n\
161\n\
Tim Peters489d54e2000-07-03 22:41:34 +0000162Return the mantissa and exponent of x, as pair (m, e).\n\
Tim Petersc9c02c42000-07-02 20:17:08 +0000163m is a float and e is an int, such that x = m * 2.**e.\n\
164If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000165
166
Barry Warsaw8b43b191996-12-09 22:32:36 +0000167static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000168math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000169{
Guido van Rossumc5545052000-05-08 14:29:38 +0000170 double x;
171 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000172 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000173 return NULL;
174 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000175 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000176 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000177 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +0000178 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000179 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000180 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000181 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000182 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000183}
184
Guido van Rossumc6e22901998-12-04 19:26:43 +0000185static char math_ldexp_doc [] =
186"ldexp_doc(x, i)\n\
187\n\
188Return x * (2**i).";
189
190
Barry Warsaw8b43b191996-12-09 22:32:36 +0000191static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000192math_modf(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000193{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000194 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +0000195 if (! PyArg_ParseTuple(args, "d:modf", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000196 return NULL;
197 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
Fred Drake40c48682000-07-03 18:11:56 +0000199 {
200 extended e;
201 x = modf(x, &e);
202 y = e;
203 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000205 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000207 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000208 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000209 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000210 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000211}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212
Guido van Rossumc6e22901998-12-04 19:26:43 +0000213static char math_modf_doc [] =
214"modf(x)\n\
215\n\
216Return the fractional and integer parts of x. Both results carry the sign\n\
217of x. The integer part is returned as a real.";
218
219
Barry Warsaw8b43b191996-12-09 22:32:36 +0000220static PyMethodDef math_methods[] = {
Fred Drake40c48682000-07-03 18:11:56 +0000221 {"acos", math_acos, METH_VARARGS, math_acos_doc},
222 {"asin", math_asin, METH_VARARGS, math_asin_doc},
223 {"atan", math_atan, METH_VARARGS, math_atan_doc},
224 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
225 {"ceil", math_ceil, METH_VARARGS, math_ceil_doc},
226 {"cos", math_cos, METH_VARARGS, math_cos_doc},
227 {"cosh", math_cosh, METH_VARARGS, math_cosh_doc},
228 {"exp", math_exp, METH_VARARGS, math_exp_doc},
229 {"fabs", math_fabs, METH_VARARGS, math_fabs_doc},
230 {"floor", math_floor, METH_VARARGS, math_floor_doc},
231 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
232 {"frexp", math_frexp, METH_VARARGS, math_frexp_doc},
233 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
234 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
235 {"log", math_log, METH_VARARGS, math_log_doc},
236 {"log10", math_log10, METH_VARARGS, math_log10_doc},
237 {"modf", math_modf, METH_VARARGS, math_modf_doc},
238 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000239 {"sin", math_sin, METH_VARARGS, math_sin_doc},
240 {"sinh", math_sinh, METH_VARARGS, math_sinh_doc},
241 {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc},
242 {"tan", math_tan, METH_VARARGS, math_tan_doc},
243 {"tanh", math_tanh, METH_VARARGS, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244 {NULL, NULL} /* sentinel */
245};
246
Guido van Rossumc6e22901998-12-04 19:26:43 +0000247
248static char module_doc [] =
249"This module is always available. It provides access to the\n\
250mathematical functions defined by the C standard.";
251
Guido van Rossum3886bb61998-12-04 18:50:17 +0000252DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000253initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000255 PyObject *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000256
Guido van Rossumc6e22901998-12-04 19:26:43 +0000257 m = Py_InitModule3("math", math_methods, module_doc);
Barry Warsaw8b43b191996-12-09 22:32:36 +0000258 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000259
260 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
261 goto finally;
262 if (PyDict_SetItemString(d, "pi", v) < 0)
263 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000264 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000265
266 if (!(v = PyFloat_FromDouble(exp(1.0))))
267 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000268 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000269 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000270 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000271
272 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000273 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274}