blob: f8d5f84f10a3381546d83c16076bc08f4628ab37 [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)
Tim Petersfe71f812001-08-07 22:10:00 +000054 PyErr_SetString(PyExc_OverflowError,
Tim Peters1d120612000-10-12 06:10:25 +000055 "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,
Tim Petersfe71f812001-08-07 22:10:00 +0000112 "acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000113FUNC1(asin, asin,
Tim Petersfe71f812001-08-07 22:10:00 +0000114 "asin(x)\n\nReturn the arc sine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000115FUNC1(atan, atan,
Tim Petersfe71f812001-08-07 22:10:00 +0000116 "atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000117FUNC2(atan2, atan2,
Tim Petersfe71f812001-08-07 22:10:00 +0000118 "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
119 "Unlike atan(y/x), the signs of both x and y are considered.")
Fred Drake40c48682000-07-03 18:11:56 +0000120FUNC1(ceil, ceil,
Tim Petersfe71f812001-08-07 22:10:00 +0000121 "ceil(x)\n\nReturn the ceiling of x as a float.\n"
122 "This is the smallest integral value >= x.")
Fred Drake40c48682000-07-03 18:11:56 +0000123FUNC1(cos, cos,
Tim Petersfe71f812001-08-07 22:10:00 +0000124 "cos(x)\n\nReturn the cosine of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000125FUNC1(cosh, cosh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000126 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000127FUNC1(exp, exp,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000128 "exp(x)\n\nReturn e raised to the power of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000129FUNC1(fabs, fabs,
Tim Petersfe71f812001-08-07 22:10:00 +0000130 "fabs(x)\n\nReturn the absolute value of the float x.")
Fred Drake40c48682000-07-03 18:11:56 +0000131FUNC1(floor, floor,
Tim Petersfe71f812001-08-07 22:10:00 +0000132 "floor(x)\n\nReturn the floor of x as a float.\n"
133 "This is the largest integral value <= x.")
Peter Schneider-Kamp07c19222001-06-10 16:45:08 +0000134FUNC2(fmod, fmod,
Tim Peters78fc0b52000-09-16 03:54:24 +0000135 "fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
136 " x % y may differ.")
Fred Drake40c48682000-07-03 18:11:56 +0000137FUNC2(hypot, hypot,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000138 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
Fred Drake40c48682000-07-03 18:11:56 +0000139FUNC1(log, log,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000140 "log(x)\n\nReturn the natural logarithm of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000141FUNC1(log10, log10,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000142 "log10(x)\n\nReturn the base-10 logarithm of x.")
Guido van Rossum1492c271991-07-27 21:38:43 +0000143#ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
Fred Drake40c48682000-07-03 18:11:56 +0000144FUNC2(pow, power,
Tim Petersfe71f812001-08-07 22:10:00 +0000145 "pow(x,y)\n\nReturn x**y (x to the power of y).")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000146#else
Fred Drake40c48682000-07-03 18:11:56 +0000147FUNC2(pow, pow,
Tim Petersfe71f812001-08-07 22:10:00 +0000148 "pow(x,y)\n\nReturn x**y (x to the power of y).")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000149#endif
Fred Drake40c48682000-07-03 18:11:56 +0000150FUNC1(sin, sin,
Tim Petersfe71f812001-08-07 22:10:00 +0000151 "sin(x)\n\nReturn the sine of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000152FUNC1(sinh, sinh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000153 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000154FUNC1(sqrt, sqrt,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000155 "sqrt(x)\n\nReturn the square root of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000156FUNC1(tan, tan,
Tim Petersfe71f812001-08-07 22:10:00 +0000157 "tan(x)\n\nReturn the tangent of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000158FUNC1(tanh, tanh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000159 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161
Barry Warsaw8b43b191996-12-09 22:32:36 +0000162static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000163math_frexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000164{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000165 double x;
166 int i;
Fred Drake40c48682000-07-03 18:11:56 +0000167 if (! PyArg_ParseTuple(args, "d:frexp", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000168 return NULL;
169 errno = 0;
170 x = frexp(x, &i);
Guido van Rossum8832b621991-12-16 15:44:24 +0000171 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +0000172 if (errno && is_error(x))
173 return NULL;
174 else
175 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000176}
177
Guido van Rossumc6e22901998-12-04 19:26:43 +0000178static char math_frexp_doc [] =
179"frexp(x)\n\
180\n\
Tim Peters489d54e2000-07-03 22:41:34 +0000181Return the mantissa and exponent of x, as pair (m, e).\n\
Tim Petersc9c02c42000-07-02 20:17:08 +0000182m is a float and e is an int, such that x = m * 2.**e.\n\
183If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000184
185
Barry Warsaw8b43b191996-12-09 22:32:36 +0000186static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000187math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000188{
Guido van Rossumc5545052000-05-08 14:29:38 +0000189 double x;
190 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000191 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000192 return NULL;
193 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000194 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000195 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000196 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +0000197 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +0000198 if (errno && is_error(x))
199 return NULL;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000200 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000201 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000202}
203
Tim Petersfe71f812001-08-07 22:10:00 +0000204static char math_ldexp_doc [] =
Guido van Rossumc6e22901998-12-04 19:26:43 +0000205"ldexp_doc(x, i)\n\
206\n\
207Return x * (2**i).";
208
209
Barry Warsaw8b43b191996-12-09 22:32:36 +0000210static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000211math_modf(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000212{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000213 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +0000214 if (! PyArg_ParseTuple(args, "d:modf", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000215 return NULL;
216 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
Fred Drake40c48682000-07-03 18:11:56 +0000218 {
219 extended e;
220 x = modf(x, &e);
221 y = e;
222 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000224 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000226 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +0000227 if (errno && is_error(x))
228 return NULL;
229 else
230 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000231}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232
Guido van Rossumc6e22901998-12-04 19:26:43 +0000233static char math_modf_doc [] =
234"modf(x)\n\
235\n\
236Return the fractional and integer parts of x. Both results carry the sign\n\
237of x. The integer part is returned as a real.";
238
239
Barry Warsaw8b43b191996-12-09 22:32:36 +0000240static PyMethodDef math_methods[] = {
Fred Drake40c48682000-07-03 18:11:56 +0000241 {"acos", math_acos, METH_VARARGS, math_acos_doc},
242 {"asin", math_asin, METH_VARARGS, math_asin_doc},
243 {"atan", math_atan, METH_VARARGS, math_atan_doc},
244 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
245 {"ceil", math_ceil, METH_VARARGS, math_ceil_doc},
246 {"cos", math_cos, METH_VARARGS, math_cos_doc},
247 {"cosh", math_cosh, METH_VARARGS, math_cosh_doc},
248 {"exp", math_exp, METH_VARARGS, math_exp_doc},
249 {"fabs", math_fabs, METH_VARARGS, math_fabs_doc},
250 {"floor", math_floor, METH_VARARGS, math_floor_doc},
251 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
252 {"frexp", math_frexp, METH_VARARGS, math_frexp_doc},
253 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
254 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
255 {"log", math_log, METH_VARARGS, math_log_doc},
256 {"log10", math_log10, METH_VARARGS, math_log10_doc},
257 {"modf", math_modf, METH_VARARGS, math_modf_doc},
258 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000259 {"sin", math_sin, METH_VARARGS, math_sin_doc},
260 {"sinh", math_sinh, METH_VARARGS, math_sinh_doc},
261 {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc},
262 {"tan", math_tan, METH_VARARGS, math_tan_doc},
263 {"tanh", math_tanh, METH_VARARGS, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264 {NULL, NULL} /* sentinel */
265};
266
Guido van Rossumc6e22901998-12-04 19:26:43 +0000267
268static char module_doc [] =
269"This module is always available. It provides access to the\n\
270mathematical functions defined by the C standard.";
271
Guido van Rossum3886bb61998-12-04 18:50:17 +0000272DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000273initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000275 PyObject *m, *d, *v;
Tim Petersfe71f812001-08-07 22:10:00 +0000276
Guido van Rossumc6e22901998-12-04 19:26:43 +0000277 m = Py_InitModule3("math", math_methods, module_doc);
Barry Warsaw8b43b191996-12-09 22:32:36 +0000278 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000279
280 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
281 goto finally;
282 if (PyDict_SetItemString(d, "pi", v) < 0)
283 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000284 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000285
286 if (!(v = PyFloat_FromDouble(exp(1.0))))
287 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000288 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000289 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000290 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000291
292 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000293 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294}