blob: 09f7135077ef7debf5cdfc0cb711d0ffa7396dcc [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
21 * What CHECK does if errno != 0 and x is a NaN is a platform-dependent crap
22 * 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 */
Guido van Rossum8832b621991-12-16 15:44:24 +000041 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000042 PyErr_SetString(PyExc_ValueError, "math domain error");
Tim Peters1d120612000-10-12 06:10:25 +000043 else if (errno == ERANGE) {
44 /* ANSI C generally requires libm functions to set ERANGE
45 * on overflow, but also generally *allows* them to set
46 * ERANGE on underflow too. There's no consistency about
47 * the latter across platforms. Here we suppress the
48 * underflow errors (libm functions should return a zero
49 * on underflow, and +- HUGE_VAL on overflow, so testing
50 * the result for zero suffices to distinguish the cases).
51 */
52 if (x)
53 PyErr_SetString(PyExc_OverflowError,
54 "math range error");
55 else
56 result = 0;
57 }
Guido van Rossum8832b621991-12-16 15:44:24 +000058 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000059 /* Unexpected math error */
60 PyErr_SetFromErrno(PyExc_ValueError);
Tim Peters1d120612000-10-12 06:10:25 +000061 return result;
Guido van Rossum8832b621991-12-16 15:44:24 +000062}
63
Barry Warsaw8b43b191996-12-09 22:32:36 +000064static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000065math_1(PyObject *args, double (*func) (double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066{
67 double x;
Fred Drake40c48682000-07-03 18:11:56 +000068 if (! PyArg_ParseTuple(args, argsfmt, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069 return NULL;
70 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000071 PyFPE_START_PROTECT("in math_1", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +000073 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000074 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +000075 if (errno && is_error(x))
76 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000078 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079}
80
Barry Warsaw8b43b191996-12-09 22:32:36 +000081static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000082math_2(PyObject *args, double (*func) (double, double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083{
84 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +000085 if (! PyArg_ParseTuple(args, argsfmt, &x, &y))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086 return NULL;
87 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000088 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000090 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000091 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +000092 if (errno && is_error(x))
93 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000095 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096}
97
Fred Drake40c48682000-07-03 18:11:56 +000098#define FUNC1(funcname, func, docstring) \
99 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
100 return math_1(args, func, "d:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000101 }\
Fred Drake40c48682000-07-03 18:11:56 +0000102 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103
Fred Drake40c48682000-07-03 18:11:56 +0000104#define FUNC2(funcname, func, docstring) \
105 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
106 return math_2(args, func, "dd:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000107 }\
Fred Drake40c48682000-07-03 18:11:56 +0000108 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109
Fred Drake40c48682000-07-03 18:11:56 +0000110FUNC1(acos, acos,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000111 "acos(x)\n\nReturn the arc cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000112FUNC1(asin, asin,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000113 "asin(x)\n\nReturn the arc sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000114FUNC1(atan, atan,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000115 "atan(x)\n\nReturn the arc tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000116FUNC2(atan2, atan2,
Guido van Rossumb057dd81998-12-08 16:27:10 +0000117 "atan2(y, x)\n\nReturn atan(y/x).")
Fred Drake40c48682000-07-03 18:11:56 +0000118FUNC1(ceil, ceil,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000119 "ceil(x)\n\nReturn the ceiling of x as a real.")
Fred Drake40c48682000-07-03 18:11:56 +0000120FUNC1(cos, cos,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000121 "cos(x)\n\nReturn the cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000122FUNC1(cosh, cosh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000123 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000124FUNC1(exp, exp,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000125 "exp(x)\n\nReturn e raised to the power of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000126FUNC1(fabs, fabs,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000127 "fabs(x)\n\nReturn the absolute value of the real x.")
Fred Drake40c48682000-07-03 18:11:56 +0000128FUNC1(floor, floor,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000129 "floor(x)\n\nReturn the floor of x as a real.")
Fred Drake40c48682000-07-03 18:11:56 +0000130 FUNC2(fmod, fmod,
Tim Peters78fc0b52000-09-16 03:54:24 +0000131 "fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
132 " x % y may differ.")
Fred Drake40c48682000-07-03 18:11:56 +0000133FUNC2(hypot, hypot,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000134 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
Fred Drake40c48682000-07-03 18:11:56 +0000135FUNC1(log, log,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000136 "log(x)\n\nReturn the natural logarithm of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000137FUNC1(log10, log10,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000138 "log10(x)\n\nReturn the base-10 logarithm of x.")
Guido van Rossum1492c271991-07-27 21:38:43 +0000139#ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
Fred Drake40c48682000-07-03 18:11:56 +0000140FUNC2(pow, power,
141 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000142#else
Fred Drake40c48682000-07-03 18:11:56 +0000143FUNC2(pow, pow,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000144 "pow(x,y)\n\nReturn x**y.")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000145#endif
Fred Drake40c48682000-07-03 18:11:56 +0000146FUNC1(sin, sin,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000147 "sin(x)\n\nReturn the sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000148FUNC1(sinh, sinh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000149 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000150FUNC1(sqrt, sqrt,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000151 "sqrt(x)\n\nReturn the square root of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000152FUNC1(tan, tan,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000153 "tan(x)\n\nReturn the tangent of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000154FUNC1(tanh, tanh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000155 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156
Guido van Rossumb6775db1994-08-01 11:34:53 +0000157
Barry Warsaw8b43b191996-12-09 22:32:36 +0000158static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000159math_frexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000160{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000161 double x;
162 int i;
Fred Drake40c48682000-07-03 18:11:56 +0000163 if (! PyArg_ParseTuple(args, "d:frexp", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000164 return NULL;
165 errno = 0;
166 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000167 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +0000168 if (errno && is_error(x))
169 return NULL;
170 else
171 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000172}
173
Guido van Rossumc6e22901998-12-04 19:26:43 +0000174static char math_frexp_doc [] =
175"frexp(x)\n\
176\n\
Tim Peters489d54e2000-07-03 22:41:34 +0000177Return the mantissa and exponent of x, as pair (m, e).\n\
Tim Petersc9c02c42000-07-02 20:17:08 +0000178m is a float and e is an int, such that x = m * 2.**e.\n\
179If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000180
181
Barry Warsaw8b43b191996-12-09 22:32:36 +0000182static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000183math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000184{
Guido van Rossumc5545052000-05-08 14:29:38 +0000185 double x;
186 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000187 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000188 return NULL;
189 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000190 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000191 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000192 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +0000193 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +0000194 if (errno && is_error(x))
195 return NULL;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000196 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000197 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000198}
199
Guido van Rossumc6e22901998-12-04 19:26:43 +0000200static char math_ldexp_doc [] =
201"ldexp_doc(x, i)\n\
202\n\
203Return x * (2**i).";
204
205
Barry Warsaw8b43b191996-12-09 22:32:36 +0000206static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000207math_modf(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000208{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000209 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +0000210 if (! PyArg_ParseTuple(args, "d:modf", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000211 return NULL;
212 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
Fred Drake40c48682000-07-03 18:11:56 +0000214 {
215 extended e;
216 x = modf(x, &e);
217 y = e;
218 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000220 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000222 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +0000223 if (errno && is_error(x))
224 return NULL;
225 else
226 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[] = {
Fred Drake40c48682000-07-03 18:11:56 +0000237 {"acos", math_acos, METH_VARARGS, math_acos_doc},
238 {"asin", math_asin, METH_VARARGS, math_asin_doc},
239 {"atan", math_atan, METH_VARARGS, math_atan_doc},
240 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
241 {"ceil", math_ceil, METH_VARARGS, math_ceil_doc},
242 {"cos", math_cos, METH_VARARGS, math_cos_doc},
243 {"cosh", math_cosh, METH_VARARGS, math_cosh_doc},
244 {"exp", math_exp, METH_VARARGS, math_exp_doc},
245 {"fabs", math_fabs, METH_VARARGS, math_fabs_doc},
246 {"floor", math_floor, METH_VARARGS, math_floor_doc},
247 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
248 {"frexp", math_frexp, METH_VARARGS, math_frexp_doc},
249 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
250 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
251 {"log", math_log, METH_VARARGS, math_log_doc},
252 {"log10", math_log10, METH_VARARGS, math_log10_doc},
253 {"modf", math_modf, METH_VARARGS, math_modf_doc},
254 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000255 {"sin", math_sin, METH_VARARGS, math_sin_doc},
256 {"sinh", math_sinh, METH_VARARGS, math_sinh_doc},
257 {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc},
258 {"tan", math_tan, METH_VARARGS, math_tan_doc},
259 {"tanh", math_tanh, METH_VARARGS, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260 {NULL, NULL} /* sentinel */
261};
262
Guido van Rossumc6e22901998-12-04 19:26:43 +0000263
264static char module_doc [] =
265"This module is always available. It provides access to the\n\
266mathematical functions defined by the C standard.";
267
Guido van Rossum3886bb61998-12-04 18:50:17 +0000268DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000269initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000271 PyObject *m, *d, *v;
Guido van Rossum738d4dd1990-10-26 14:59:30 +0000272
Guido van Rossumc6e22901998-12-04 19:26:43 +0000273 m = Py_InitModule3("math", math_methods, module_doc);
Barry Warsaw8b43b191996-12-09 22:32:36 +0000274 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000275
276 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
277 goto finally;
278 if (PyDict_SetItemString(d, "pi", v) < 0)
279 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000280 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000281
282 if (!(v = PyFloat_FromDouble(exp(1.0))))
283 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000284 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000285 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000286 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000287
288 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000289 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290}