blob: f51b63a98bddff3e48402a2edf3fe84e1c48326c [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
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000132#ifdef HAVE_RINT
Fred Drake40c48682000-07-03 18:11:56 +0000133FUNC1(rint, rint,
Guido van Rossum71260b82000-05-11 18:19:42 +0000134 "rint(x)\n\nReturn the integer nearest to x as a real.")
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000135#endif
Fred Drake40c48682000-07-03 18:11:56 +0000136FUNC1(sin, sin,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000137 "sin(x)\n\nReturn the sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000138FUNC1(sinh, sinh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000139 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000140FUNC1(sqrt, sqrt,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000141 "sqrt(x)\n\nReturn the square root of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000142FUNC1(tan, tan,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000143 "tan(x)\n\nReturn the tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000144FUNC1(tanh, tanh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000145 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146
Guido van Rossumb6775db1994-08-01 11:34:53 +0000147
Barry Warsaw8b43b191996-12-09 22:32:36 +0000148static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000149math_frexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000150{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000151 double x;
152 int i;
Fred Drake40c48682000-07-03 18:11:56 +0000153 if (! PyArg_ParseTuple(args, "d:frexp", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000154 return NULL;
155 errno = 0;
156 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000157 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000158 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000159 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000160 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000161}
162
Guido van Rossumc6e22901998-12-04 19:26:43 +0000163static char math_frexp_doc [] =
164"frexp(x)\n\
165\n\
Tim Peters489d54e2000-07-03 22:41:34 +0000166Return the mantissa and exponent of x, as pair (m, e).\n\
Tim Petersc9c02c42000-07-02 20:17:08 +0000167m is a float and e is an int, such that x = m * 2.**e.\n\
168If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000169
170
Barry Warsaw8b43b191996-12-09 22:32:36 +0000171static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000172math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000173{
Guido van Rossumc5545052000-05-08 14:29:38 +0000174 double x;
175 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000176 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000177 return NULL;
178 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000179 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000180 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000181 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +0000182 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000183 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000184 return math_error();
Guido van Rossumd18ad581991-10-24 14:57:21 +0000185 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000186 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000187}
188
Guido van Rossumc6e22901998-12-04 19:26:43 +0000189static char math_ldexp_doc [] =
190"ldexp_doc(x, i)\n\
191\n\
192Return x * (2**i).";
193
194
Barry Warsaw8b43b191996-12-09 22:32:36 +0000195static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000196math_modf(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000197{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000198 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +0000199 if (! PyArg_ParseTuple(args, "d:modf", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000200 return NULL;
201 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
Fred Drake40c48682000-07-03 18:11:56 +0000203 {
204 extended e;
205 x = modf(x, &e);
206 y = e;
207 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000209 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000210#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000211 CHECK(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000212 if (errno != 0)
Guido van Rossum8832b621991-12-16 15:44:24 +0000213 return math_error();
Barry Warsaw8b43b191996-12-09 22:32:36 +0000214 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000215}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216
Guido van Rossumc6e22901998-12-04 19:26:43 +0000217static char math_modf_doc [] =
218"modf(x)\n\
219\n\
220Return the fractional and integer parts of x. Both results carry the sign\n\
221of x. The integer part is returned as a real.";
222
223
Barry Warsaw8b43b191996-12-09 22:32:36 +0000224static PyMethodDef math_methods[] = {
Fred Drake40c48682000-07-03 18:11:56 +0000225 {"acos", math_acos, METH_VARARGS, math_acos_doc},
226 {"asin", math_asin, METH_VARARGS, math_asin_doc},
227 {"atan", math_atan, METH_VARARGS, math_atan_doc},
228 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
229 {"ceil", math_ceil, METH_VARARGS, math_ceil_doc},
230 {"cos", math_cos, METH_VARARGS, math_cos_doc},
231 {"cosh", math_cosh, METH_VARARGS, math_cosh_doc},
232 {"exp", math_exp, METH_VARARGS, math_exp_doc},
233 {"fabs", math_fabs, METH_VARARGS, math_fabs_doc},
234 {"floor", math_floor, METH_VARARGS, math_floor_doc},
235 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
236 {"frexp", math_frexp, METH_VARARGS, math_frexp_doc},
237 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
238 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
239 {"log", math_log, METH_VARARGS, math_log_doc},
240 {"log10", math_log10, METH_VARARGS, math_log10_doc},
241 {"modf", math_modf, METH_VARARGS, math_modf_doc},
242 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000243#ifdef HAVE_RINT
Fred Drake40c48682000-07-03 18:11:56 +0000244 {"rint", math_rint, METH_VARARGS, math_rint_doc},
Guido van Rossumc9a5f342000-05-11 18:42:27 +0000245#endif
Fred Drake40c48682000-07-03 18:11:56 +0000246 {"sin", math_sin, METH_VARARGS, math_sin_doc},
247 {"sinh", math_sinh, METH_VARARGS, math_sinh_doc},
248 {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc},
249 {"tan", math_tan, METH_VARARGS, math_tan_doc},
250 {"tanh", math_tanh, METH_VARARGS, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251 {NULL, NULL} /* sentinel */
252};
253
Guido van Rossumc6e22901998-12-04 19:26:43 +0000254
255static char module_doc [] =
256"This module is always available. It provides access to the\n\
257mathematical functions defined by the C standard.";
258
Guido van Rossum3886bb61998-12-04 18:50:17 +0000259DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000260initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000262 PyObject *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000263
Guido van Rossumc6e22901998-12-04 19:26:43 +0000264 m = Py_InitModule3("math", math_methods, module_doc);
Barry Warsaw8b43b191996-12-09 22:32:36 +0000265 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000266
267 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
268 goto finally;
269 if (PyDict_SetItemString(d, "pi", v) < 0)
270 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000271 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000272
273 if (!(v = PyFloat_FromDouble(exp(1.0))))
274 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000275 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000276 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000277 Py_DECREF(v);
Guido van Rossum4a488091996-12-18 14:14:33 +0000278 return;
Barry Warsawfc93f751996-12-17 00:47:03 +0000279
280 finally:
281 Py_FatalError("can't initialize math module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282}