blob: eef8b78c3fab7699dd1532b0abf77ce7152a88ac [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"
Tim Peters78526162001-09-05 00:53:45 +00004#include "longintrepr.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum7081cf51996-05-23 22:56:19 +00006#ifndef _MSC_VER
Guido van Rossumb9418681995-01-12 11:28:16 +00007#ifndef __STDC__
Fred Drake40c48682000-07-03 18:11:56 +00008extern double fmod (double, double);
9extern double frexp (double, int *);
10extern double ldexp (double, int);
11extern double modf (double, double *);
Guido van Rossum7081cf51996-05-23 22:56:19 +000012#endif /* __STDC__ */
13#endif /* _MSC_VER */
14
Guido van Rossumb9418681995-01-12 11:28:16 +000015
Guido van Rossum9575a441993-04-07 14:06:14 +000016#ifdef i860
17/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
18#undef HUGE_VAL
19#endif
20
Tim Peters1d120612000-10-12 06:10:25 +000021/* RED_FLAG 12-Oct-2000 Tim
Tim Peters2bf405a2000-10-12 19:42:00 +000022 * What CHECK does if errno == 0 and x is a NaN is a platform-dependent crap
Tim Peters1d120612000-10-12 06:10:25 +000023 * shoot. Most (but not all!) platforms will end up setting errno to ERANGE
24 * then, but EDOM is probably better.
25 */
Guido van Rossum8832b621991-12-16 15:44:24 +000026#ifdef HUGE_VAL
27#define CHECK(x) if (errno != 0) ; \
28 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
29 else errno = ERANGE
30#else
31#define CHECK(x) /* Don't know how to check */
32#endif
33
Tim Peters1d120612000-10-12 06:10:25 +000034/* Call is_error when errno != 0, and where x is the result libm
35 * returned. is_error will usually set up an exception and return
36 * true (1), but may return false (0) without setting up an exception.
37 */
38static int
39is_error(double x)
Guido van Rossum8832b621991-12-16 15:44:24 +000040{
Tim Peters1d120612000-10-12 06:10:25 +000041 int result = 1; /* presumption of guilt */
Tim Peters2bf405a2000-10-12 19:42:00 +000042 assert(errno); /* non-zero errno is a precondition for calling */
Guido van Rossum8832b621991-12-16 15:44:24 +000043 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000044 PyErr_SetString(PyExc_ValueError, "math domain error");
Tim Peters1d120612000-10-12 06:10:25 +000045 else if (errno == ERANGE) {
46 /* ANSI C generally requires libm functions to set ERANGE
47 * on overflow, but also generally *allows* them to set
48 * ERANGE on underflow too. There's no consistency about
49 * the latter across platforms. Here we suppress the
50 * underflow errors (libm functions should return a zero
51 * on underflow, and +- HUGE_VAL on overflow, so testing
52 * the result for zero suffices to distinguish the cases).
53 */
54 if (x)
Tim Petersfe71f812001-08-07 22:10:00 +000055 PyErr_SetString(PyExc_OverflowError,
Tim Peters1d120612000-10-12 06:10:25 +000056 "math range error");
57 else
58 result = 0;
59 }
Guido van Rossum8832b621991-12-16 15:44:24 +000060 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000061 /* Unexpected math error */
62 PyErr_SetFromErrno(PyExc_ValueError);
Tim Peters1d120612000-10-12 06:10:25 +000063 return result;
Guido van Rossum8832b621991-12-16 15:44:24 +000064}
65
Barry Warsaw8b43b191996-12-09 22:32:36 +000066static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000067math_1(PyObject *args, double (*func) (double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068{
69 double x;
Fred Drake40c48682000-07-03 18:11:56 +000070 if (! PyArg_ParseTuple(args, argsfmt, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071 return NULL;
72 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000073 PyFPE_START_PROTECT("in math_1", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +000075 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000076 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +000077 if (errno && is_error(x))
78 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000080 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081}
82
Barry Warsaw8b43b191996-12-09 22:32:36 +000083static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +000084math_2(PyObject *args, double (*func) (double, double), char *argsfmt)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085{
86 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +000087 if (! PyArg_ParseTuple(args, argsfmt, &x, &y))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 return NULL;
89 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000090 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000092 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +000093 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +000094 if (errno && is_error(x))
95 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000097 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098}
99
Fred Drake40c48682000-07-03 18:11:56 +0000100#define FUNC1(funcname, func, docstring) \
101 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
102 return math_1(args, func, "d:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000103 }\
Fred Drake40c48682000-07-03 18:11:56 +0000104 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105
Fred Drake40c48682000-07-03 18:11:56 +0000106#define FUNC2(funcname, func, docstring) \
107 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
108 return math_2(args, func, "dd:" #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000109 }\
Fred Drake40c48682000-07-03 18:11:56 +0000110 static char math_##funcname##_doc [] = docstring;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111
Fred Drake40c48682000-07-03 18:11:56 +0000112FUNC1(acos, acos,
Tim Petersfe71f812001-08-07 22:10:00 +0000113 "acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000114FUNC1(asin, asin,
Tim Petersfe71f812001-08-07 22:10:00 +0000115 "asin(x)\n\nReturn the arc sine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000116FUNC1(atan, atan,
Tim Petersfe71f812001-08-07 22:10:00 +0000117 "atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000118FUNC2(atan2, atan2,
Tim Petersfe71f812001-08-07 22:10:00 +0000119 "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
120 "Unlike atan(y/x), the signs of both x and y are considered.")
Fred Drake40c48682000-07-03 18:11:56 +0000121FUNC1(ceil, ceil,
Tim Petersfe71f812001-08-07 22:10:00 +0000122 "ceil(x)\n\nReturn the ceiling of x as a float.\n"
123 "This is the smallest integral value >= x.")
Fred Drake40c48682000-07-03 18:11:56 +0000124FUNC1(cos, cos,
Tim Petersfe71f812001-08-07 22:10:00 +0000125 "cos(x)\n\nReturn the cosine of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000126FUNC1(cosh, cosh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000127 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000128FUNC1(exp, exp,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000129 "exp(x)\n\nReturn e raised to the power of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000130FUNC1(fabs, fabs,
Tim Petersfe71f812001-08-07 22:10:00 +0000131 "fabs(x)\n\nReturn the absolute value of the float x.")
Fred Drake40c48682000-07-03 18:11:56 +0000132FUNC1(floor, floor,
Tim Petersfe71f812001-08-07 22:10:00 +0000133 "floor(x)\n\nReturn the floor of x as a float.\n"
134 "This is the largest integral value <= x.")
Peter Schneider-Kamp07c19222001-06-10 16:45:08 +0000135FUNC2(fmod, fmod,
Tim Peters78fc0b52000-09-16 03:54:24 +0000136 "fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
137 " x % y may differ.")
Fred Drake40c48682000-07-03 18:11:56 +0000138FUNC2(hypot, hypot,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000139 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
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,
Tim Petersfe71f812001-08-07 22:10:00 +0000142 "pow(x,y)\n\nReturn x**y (x to the power of y).")
Guido van Rossum76f2f2e1991-06-24 22:23:10 +0000143#else
Fred Drake40c48682000-07-03 18:11:56 +0000144FUNC2(pow, pow,
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#endif
Fred Drake40c48682000-07-03 18:11:56 +0000147FUNC1(sin, sin,
Tim Petersfe71f812001-08-07 22:10:00 +0000148 "sin(x)\n\nReturn the sine of x (measured in radians).")
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,
Tim Petersfe71f812001-08-07 22:10:00 +0000154 "tan(x)\n\nReturn the tangent of x (measured in radians).")
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
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 [] =
Tim Peters63c94532001-09-04 23:17:42 +0000175"frexp(x)\n"
176"\n"
177"Return the mantissa and exponent of x, as pair (m, e).\n"
178"m is a float and e is an int, such that x = m * 2.**e.\n"
179"If 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
Barry Warsaw8b43b191996-12-09 22:32:36 +0000181static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000182math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000183{
Guido van Rossumc5545052000-05-08 14:29:38 +0000184 double x;
185 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000186 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000187 return NULL;
188 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000189 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000190 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000191 PyFPE_END_PROTECT(x)
Guido van Rossum8832b621991-12-16 15:44:24 +0000192 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +0000193 if (errno && is_error(x))
194 return NULL;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000195 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000196 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000197}
198
Tim Petersfe71f812001-08-07 22:10:00 +0000199static char math_ldexp_doc [] =
Tim Peters63c94532001-09-04 23:17:42 +0000200"ldexp(x, i) -> x * (2**i)";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000201
Barry Warsaw8b43b191996-12-09 22:32:36 +0000202static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000203math_modf(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000204{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000205 double x, y;
Fred Drake40c48682000-07-03 18:11:56 +0000206 if (! PyArg_ParseTuple(args, "d:modf", &x))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000207 return NULL;
208 errno = 0;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
Fred Drake40c48682000-07-03 18:11:56 +0000210 {
211 extended e;
212 x = modf(x, &e);
213 y = e;
214 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#else
Guido van Rossumd18ad581991-10-24 14:57:21 +0000216 x = modf(x, &y);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217#endif
Guido van Rossum8832b621991-12-16 15:44:24 +0000218 CHECK(x);
Tim Peters1d120612000-10-12 06:10:25 +0000219 if (errno && is_error(x))
220 return NULL;
221 else
222 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000223}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224
Guido van Rossumc6e22901998-12-04 19:26:43 +0000225static char math_modf_doc [] =
Tim Peters63c94532001-09-04 23:17:42 +0000226"modf(x)\n"
227"\n"
228"Return the fractional and integer parts of x. Both results carry the sign\n"
229"of x. The integer part is returned as a real.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000230
Tim Peters78526162001-09-05 00:53:45 +0000231/* A decent logarithm is easy to compute even for huge longs, but libm can't
232 do that by itself -- loghelper can. func is log or log10, and name is
233 "log" or "log10". Note that overflow isn't possible: a long can contain
234 no more than INT_MAX * SHIFT bits, so has value certainly less than
235 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
236 small enough to fit in an IEEE single. log and log10 are even smaller.
237*/
238
239static PyObject*
240loghelper(PyObject* args, double (*func)(double), char *name)
241{
242 PyObject *arg;
243 char format[16];
244
245 /* See whether this is a long. */
246 format[0] = 'O';
247 format[1] = ':';
248 strcpy(format + 2, name);
249 if (! PyArg_ParseTuple(args, format, &arg))
250 return NULL;
251
252 /* If it is long, do it ourselves. */
253 if (PyLong_Check(arg)) {
254 double x;
255 int e;
256 x = _PyLong_AsScaledDouble(arg, &e);
257 if (x <= 0.0) {
258 PyErr_SetString(PyExc_ValueError,
259 "math domain error");
260 return NULL;
261 }
262 /* Value is ~= x * 2**(e*SHIFT), so the log ~=
263 log(x) + log(2) * e * SHIFT.
264 CAUTION: e*SHIFT may overflow using int arithmetic,
265 so force use of double. */
266 x = func(x) + func(2.0) * (double)e * (double)SHIFT;
267 return PyFloat_FromDouble(x);
268 }
269
270 /* Else let libm handle it by itself. */
271 format[0] = 'd';
272 return math_1(args, func, format);
273}
274
275static PyObject *
276math_log(PyObject *self, PyObject *args)
277{
278 return loghelper(args, log, "log");
279}
280
281static char math_log_doc[] =
282"log(x) -> the natural logarithm (base e) of x.";
283
284static PyObject *
285math_log10(PyObject *self, PyObject *args)
286{
287 return loghelper(args, log10, "log10");
288}
289
290static char math_log10_doc[] =
291"log10(x) -> the base 10 logarithm of x.";
292
293
Barry Warsaw8b43b191996-12-09 22:32:36 +0000294static PyMethodDef math_methods[] = {
Fred Drake40c48682000-07-03 18:11:56 +0000295 {"acos", math_acos, METH_VARARGS, math_acos_doc},
296 {"asin", math_asin, METH_VARARGS, math_asin_doc},
297 {"atan", math_atan, METH_VARARGS, math_atan_doc},
298 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
299 {"ceil", math_ceil, METH_VARARGS, math_ceil_doc},
300 {"cos", math_cos, METH_VARARGS, math_cos_doc},
301 {"cosh", math_cosh, METH_VARARGS, math_cosh_doc},
302 {"exp", math_exp, METH_VARARGS, math_exp_doc},
303 {"fabs", math_fabs, METH_VARARGS, math_fabs_doc},
304 {"floor", math_floor, METH_VARARGS, math_floor_doc},
305 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
306 {"frexp", math_frexp, METH_VARARGS, math_frexp_doc},
307 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
308 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
309 {"log", math_log, METH_VARARGS, math_log_doc},
310 {"log10", math_log10, METH_VARARGS, math_log10_doc},
311 {"modf", math_modf, METH_VARARGS, math_modf_doc},
312 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000313 {"sin", math_sin, METH_VARARGS, math_sin_doc},
314 {"sinh", math_sinh, METH_VARARGS, math_sinh_doc},
315 {"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc},
316 {"tan", math_tan, METH_VARARGS, math_tan_doc},
317 {"tanh", math_tanh, METH_VARARGS, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318 {NULL, NULL} /* sentinel */
319};
320
Guido van Rossumc6e22901998-12-04 19:26:43 +0000321
322static char module_doc [] =
Tim Peters63c94532001-09-04 23:17:42 +0000323"This module is always available. It provides access to the\n"
324"mathematical functions defined by the C standard.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000325
Guido van Rossum3886bb61998-12-04 18:50:17 +0000326DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000327initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000329 PyObject *m, *d, *v;
Tim Petersfe71f812001-08-07 22:10:00 +0000330
Guido van Rossumc6e22901998-12-04 19:26:43 +0000331 m = Py_InitModule3("math", math_methods, module_doc);
Barry Warsaw8b43b191996-12-09 22:32:36 +0000332 d = PyModule_GetDict(m);
Barry Warsawfc93f751996-12-17 00:47:03 +0000333
334 if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
335 goto finally;
336 if (PyDict_SetItemString(d, "pi", v) < 0)
337 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000338 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000339
340 if (!(v = PyFloat_FromDouble(exp(1.0))))
341 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000342 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000343 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000344 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000345
346 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000347 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348}