blob: 72823938f6eca28ef8bb3c1675a0af10208cafd8 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Math module -- standard C math library functions, pi and e */
2
Barry Warsaw8b43b191996-12-09 22:32:36 +00003#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004
Guido van Rossum7081cf51996-05-23 22:56:19 +00005#ifndef _MSC_VER
Guido van Rossumb9418681995-01-12 11:28:16 +00006#ifndef __STDC__
Fred Drake40c48682000-07-03 18:11:56 +00007extern double fmod (double, double);
8extern double frexp (double, int *);
9extern double ldexp (double, int);
10extern double modf (double, double *);
Guido van Rossum7081cf51996-05-23 22:56:19 +000011#endif /* __STDC__ */
12#endif /* _MSC_VER */
13
Guido van Rossumb9418681995-01-12 11:28:16 +000014
Guido van Rossum9575a441993-04-07 14:06:14 +000015#ifdef i860
16/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
17#undef HUGE_VAL
18#endif
19
Tim Peters1d120612000-10-12 06:10:25 +000020/* RED_FLAG 12-Oct-2000 Tim
Tim Peters2bf405a2000-10-12 19:42:00 +000021 * What CHECK does if errno == 0 and x is a NaN is a platform-dependent crap
Tim Peters1d120612000-10-12 06:10:25 +000022 * shoot. Most (but not all!) platforms will end up setting errno to ERANGE
23 * then, but EDOM is probably better.
24 */
Guido van Rossum8832b621991-12-16 15:44:24 +000025#ifdef HUGE_VAL
26#define CHECK(x) if (errno != 0) ; \
27 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
28 else errno = ERANGE
29#else
30#define CHECK(x) /* Don't know how to check */
31#endif
32
Tim Peters1d120612000-10-12 06:10:25 +000033/* Call is_error when errno != 0, and where x is the result libm
34 * returned. is_error will usually set up an exception and return
35 * true (1), but may return false (0) without setting up an exception.
36 */
37static int
38is_error(double x)
Guido van Rossum8832b621991-12-16 15:44:24 +000039{
Tim Peters1d120612000-10-12 06:10:25 +000040 int result = 1; /* presumption of guilt */
Tim Peters2bf405a2000-10-12 19:42:00 +000041 assert(errno); /* non-zero errno is a precondition for calling */
Guido van Rossum8832b621991-12-16 15:44:24 +000042 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000043 PyErr_SetString(PyExc_ValueError, "math domain error");
Tim Peters1d120612000-10-12 06:10:25 +000044 else if (errno == ERANGE) {
45 /* ANSI C generally requires libm functions to set ERANGE
46 * on overflow, but also generally *allows* them to set
47 * ERANGE on underflow too. There's no consistency about
48 * the latter across platforms. Here we suppress the
49 * underflow errors (libm functions should return a zero
50 * on underflow, and +- HUGE_VAL on overflow, so testing
51 * the result for zero suffices to distinguish the cases).
52 */
53 if (x)
54 PyErr_SetString(PyExc_OverflowError,
55 "math range error");
56 else
57 result = 0;
58 }
Guido van Rossum8832b621991-12-16 15:44:24 +000059 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000060 /* Unexpected math error */
61 PyErr_SetFromErrno(PyExc_ValueError);
Tim Peters1d120612000-10-12 06:10:25 +000062 return result;
Guido van Rossum8832b621991-12-16 15:44:24 +000063}
64
Barry Warsaw8b43b191996-12-09 22:32:36 +000065static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000066math_1(PyObject *args, double (*func) (double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067{
68 double x;
Fred Drake40c48682000-07-03 18:11:56 +000069 if (! PyArg_ParseTuple(args, argsfmt, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 return NULL;
71 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000072 PyFPE_START_PROTECT("in math_1", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +000074 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000075 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +000076 if (errno && is_error(x))
77 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000079 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080}
81
Barry Warsaw8b43b191996-12-09 22:32:36 +000082static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000083math_2(PyObject *args, double (*func) (double, double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084{
85 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +000086 if (! PyArg_ParseTuple(args, argsfmt, &x, &y))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087 return NULL;
88 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000089 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000091 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000092 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +000093 if (errno && is_error(x))
94 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000096 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097}
98
Fred Drake40c48682000-07-03 18:11:56 +000099#define FUNC1(funcname, func, docstring) \
100 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
101 return math_1(args, func, "d:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000102 }\
Fred Drake40c48682000-07-03 18:11:56 +0000103 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104
Fred Drake40c48682000-07-03 18:11:56 +0000105#define FUNC2(funcname, func, docstring) \
106 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
107 return math_2(args, func, "dd:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000108 }\
Fred Drake40c48682000-07-03 18:11:56 +0000109 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110
Fred Drake40c48682000-07-03 18:11:56 +0000111FUNC1(acos, acos,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000112 "acos(x)\n\nReturn the arc cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000113FUNC1(asin, asin,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000114 "asin(x)\n\nReturn the arc sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000115FUNC1(atan, atan,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000116 "atan(x)\n\nReturn the arc tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000117FUNC2(atan2, atan2,
Guido van Rossumb057dd81998-12-08 16:27:10 +0000118 "atan2(y, x)\n\nReturn atan(y/x).")
Fred Drake40c48682000-07-03 18:11:56 +0000119FUNC1(ceil, ceil,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000120 "ceil(x)\n\nReturn the ceiling of x as a real.")
Fred Drake40c48682000-07-03 18:11:56 +0000121FUNC1(cos, cos,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000122 "cos(x)\n\nReturn the cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000123FUNC1(cosh, cosh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000124 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000125FUNC1(exp, exp,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000126 "exp(x)\n\nReturn e raised to the power of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000127FUNC1(fabs, fabs,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000128 "fabs(x)\n\nReturn the absolute value of the real x.")
Fred Drake40c48682000-07-03 18:11:56 +0000129FUNC1(floor, floor,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000130 "floor(x)\n\nReturn the floor of x as a real.")
Peter Schneider-Kamp07c19222001-06-10 16:45:08 +0000131FUNC2(fmod, fmod,
Tim Peters78fc0b52000-09-16 03:54:24 +0000132 "fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
133 " x % y may differ.")
Fred Drake40c48682000-07-03 18:11:56 +0000134FUNC2(hypot, hypot,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000135 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
Fred Drake40c48682000-07-03 18:11:56 +0000136FUNC1(log, log,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000137 "log(x)\n\nReturn the natural logarithm of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000138FUNC1(log10, log10,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000139 "log10(x)\n\nReturn the base-10 logarithm of x.")
Guido van Rossum1492c271991-07-27 21:38:43 +0000140#ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
Fred Drake40c48682000-07-03 18:11:56 +0000141FUNC2(pow, power,
142 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000143#else
Fred Drake40c48682000-07-03 18:11:56 +0000144FUNC2(pow, pow,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000145 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000146#endif
Fred Drake40c48682000-07-03 18:11:56 +0000147FUNC1(sin, sin,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000148 "sin(x)\n\nReturn the sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000149FUNC1(sinh, sinh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000150 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000151FUNC1(sqrt, sqrt,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000152 "sqrt(x)\n\nReturn the square root of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000153FUNC1(tan, tan,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000154 "tan(x)\n\nReturn the tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000155FUNC1(tanh, tanh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000156 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157
Guido van Rossumb6775db1994-08-01 11:34:53 +0000158
Barry Warsaw8b43b191996-12-09 22:32:36 +0000159static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000160math_frexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000161{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000162 double x;
163 int i;
Fred Drake40c48682000-07-03 18:11:56 +0000164 if (! PyArg_ParseTuple(args, "d:frexp", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000165 return NULL;
166 errno = 0;
167 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000168 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +0000169 if (errno && is_error(x))
170 return NULL;
171 else
172 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000173}
174
Guido van Rossumc6e22901998-12-04 19:26:43 +0000175static char math_frexp_doc [] =
176"frexp(x)\n\
177\n\
Tim Peters489d54e2000-07-03 22:41:34 +0000178Return the mantissa and exponent of x, as pair (m, e).\n\
Tim Petersc9c02c42000-07-02 20:17:08 +0000179m is a float and e is an int, such that x = m * 2.**e.\n\
180If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000181
182
Barry Warsaw8b43b191996-12-09 22:32:36 +0000183static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000184math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000185{
Guido van Rossumc5545052000-05-08 14:29:38 +0000186 double x;
187 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000188 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000189 return NULL;
190 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000191 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000192 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000193 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +0000194 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +0000195 if (errno && is_error(x))
196 return NULL;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000197 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000198 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000199}
200
Guido van Rossumc6e22901998-12-04 19:26:43 +0000201static char math_ldexp_doc [] =
202"ldexp_doc(x, i)\n\
203\n\
204Return x * (2**i).";
205
206
Barry Warsaw8b43b191996-12-09 22:32:36 +0000207static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000208math_modf(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000209{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000210 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +0000211 if (! PyArg_ParseTuple(args, "d:modf", &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 */
Fred Drake40c48682000-07-03 18:11:56 +0000215 {
216 extended e;
217 x = modf(x, &e);
218 y = e;
219 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220#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);
Tim Peters1d120612000-10-12 06:10:25 +0000224 if (errno && is_error(x))
225 return NULL;
226 else
227 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000228}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229
Guido van Rossumc6e22901998-12-04 19:26:43 +0000230static char math_modf_doc [] =
231"modf(x)\n\
232\n\
233Return the fractional and integer parts of x. Both results carry the sign\n\
234of x. The integer part is returned as a real.";
235
236
Barry Warsaw8b43b191996-12-09 22:32:36 +0000237static PyMethodDef math_methods[] = {
Fred Drake40c48682000-07-03 18:11:56 +0000238 {"acos", math_acos, METH_VARARGS, math_acos_doc},
239 {"asin", math_asin, METH_VARARGS, math_asin_doc},
240 {"atan", math_atan, METH_VARARGS, math_atan_doc},
241 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
242 {"ceil", math_ceil, METH_VARARGS, math_ceil_doc},
243 {"cos", math_cos, METH_VARARGS, math_cos_doc},
244 {"cosh", math_cosh, METH_VARARGS, math_cosh_doc},
245 {"exp", math_exp, METH_VARARGS, math_exp_doc},
246 {"fabs", math_fabs, METH_VARARGS, math_fabs_doc},
247 {"floor", math_floor, METH_VARARGS, math_floor_doc},
248 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
249 {"frexp", math_frexp, METH_VARARGS, math_frexp_doc},
250 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
251 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
252 {"log", math_log, METH_VARARGS, math_log_doc},
253 {"log10", math_log10, METH_VARARGS, math_log10_doc},
254 {"modf", math_modf, METH_VARARGS, math_modf_doc},
255 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000256 {"sin", math_sin, METH_VARARGS, math_sin_doc},
257 {"sinh", math_sinh, METH_VARARGS, math_sinh_doc},
258 {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc},
259 {"tan", math_tan, METH_VARARGS, math_tan_doc},
260 {"tanh", math_tanh, METH_VARARGS, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261 {NULL, NULL} /* sentinel */
262};
263
Guido van Rossumc6e22901998-12-04 19:26:43 +0000264
265static char module_doc [] =
266"This module is always available. It provides access to the\n\
267mathematical functions defined by the C standard.";
268
Guido van Rossum3886bb61998-12-04 18:50:17 +0000269DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000270initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000272 PyObject *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000273
Guido van Rossumc6e22901998-12-04 19:26:43 +0000274 m = Py_InitModule3("math", math_methods, module_doc);
Barry Warsaw8b43b191996-12-09 22:32:36 +0000275 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000276
277 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
278 goto finally;
279 if (PyDict_SetItemString(d, "pi", v) < 0)
280 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000281 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000282
283 if (!(v = PyFloat_FromDouble(exp(1.0))))
284 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000285 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000286 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000287 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000288
289 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000290 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291}