blob: b773f706e602e95f99fa5d20ed6705b664c4d1c8 [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"
Michael W. Hudson9ef852c2005-04-06 13:05:18 +00004#include "longintrepr.h" /* just for SHIFT */
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
Tim Peters1d120612000-10-12 06:10:25 +000015/* Call is_error when errno != 0, and where x is the result libm
16 * returned. is_error will usually set up an exception and return
17 * true (1), but may return false (0) without setting up an exception.
18 */
19static int
20is_error(double x)
Guido van Rossum8832b621991-12-16 15:44:24 +000021{
Tim Peters1d120612000-10-12 06:10:25 +000022 int result = 1; /* presumption of guilt */
Tim Peters2bf405a2000-10-12 19:42:00 +000023 assert(errno); /* non-zero errno is a precondition for calling */
Guido van Rossum8832b621991-12-16 15:44:24 +000024 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000025 PyErr_SetString(PyExc_ValueError, "math domain error");
Tim Petersa40c7932001-09-05 22:36:56 +000026
Tim Peters1d120612000-10-12 06:10:25 +000027 else if (errno == ERANGE) {
28 /* ANSI C generally requires libm functions to set ERANGE
29 * on overflow, but also generally *allows* them to set
30 * ERANGE on underflow too. There's no consistency about
Tim Petersa40c7932001-09-05 22:36:56 +000031 * the latter across platforms.
32 * Alas, C99 never requires that errno be set.
33 * Here we suppress the underflow errors (libm functions
34 * should return a zero on underflow, and +- HUGE_VAL on
35 * overflow, so testing the result for zero suffices to
36 * distinguish the cases).
Tim Peters1d120612000-10-12 06:10:25 +000037 */
38 if (x)
Tim Petersfe71f812001-08-07 22:10:00 +000039 PyErr_SetString(PyExc_OverflowError,
Tim Peters1d120612000-10-12 06:10:25 +000040 "math range error");
41 else
42 result = 0;
43 }
Guido van Rossum8832b621991-12-16 15:44:24 +000044 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000045 /* Unexpected math error */
46 PyErr_SetFromErrno(PyExc_ValueError);
Tim Peters1d120612000-10-12 06:10:25 +000047 return result;
Guido van Rossum8832b621991-12-16 15:44:24 +000048}
49
Barry Warsaw8b43b191996-12-09 22:32:36 +000050static PyObject *
Jeffrey Yasskinc2155832008-01-05 20:03:11 +000051math_1_to_whatever(PyObject *arg, double (*func) (double),
52 PyObject *(*from_double_func) (double))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053{
Thomas Wouters89f507f2006-12-13 04:49:30 +000054 double x = PyFloat_AsDouble(arg);
55 if (x == -1.0 && PyErr_Occurred())
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)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +000061 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +000062 if (errno && is_error(x))
63 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 else
Jeffrey Yasskinc2155832008-01-05 20:03:11 +000065 return (*from_double_func)(x);
66}
67
68static PyObject *
69math_1(PyObject *arg, double (*func) (double))
70{
71 return math_1_to_whatever(arg, func, PyFloat_FromDouble);
72}
73
74static PyObject *
75math_1_to_int(PyObject *arg, double (*func) (double))
76{
77 return math_1_to_whatever(arg, func, PyLong_FromDouble);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078}
79
Barry Warsaw8b43b191996-12-09 22:32:36 +000080static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +000081math_2(PyObject *args, double (*func) (double, double), char *funcname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082{
Thomas Wouters89f507f2006-12-13 04:49:30 +000083 PyObject *ox, *oy;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084 double x, y;
Thomas Wouters89f507f2006-12-13 04:49:30 +000085 if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy))
86 return NULL;
87 x = PyFloat_AsDouble(ox);
88 y = PyFloat_AsDouble(oy);
89 if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 return NULL;
91 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000092 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000094 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +000095 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +000096 if (errno && is_error(x))
97 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000099 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100}
101
Fred Drake40c48682000-07-03 18:11:56 +0000102#define FUNC1(funcname, func, docstring) \
103 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
Thomas Wouters89f507f2006-12-13 04:49:30 +0000104 return math_1(args, func); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000105 }\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107
Fred Drake40c48682000-07-03 18:11:56 +0000108#define FUNC2(funcname, func, docstring) \
109 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110 return math_2(args, func, #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000111 }\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000112 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113
Fred Drake40c48682000-07-03 18:11:56 +0000114FUNC1(acos, acos,
Tim Petersfe71f812001-08-07 22:10:00 +0000115 "acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000116FUNC1(asin, asin,
Tim Petersfe71f812001-08-07 22:10:00 +0000117 "asin(x)\n\nReturn the arc sine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000118FUNC1(atan, atan,
Tim Petersfe71f812001-08-07 22:10:00 +0000119 "atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
Martin v. Löwis387c5472001-09-06 08:16:17 +0000120FUNC2(atan2, atan2,
Tim Petersfe71f812001-08-07 22:10:00 +0000121 "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
122 "Unlike atan(y/x), the signs of both x and y are considered.")
Guido van Rossum13e05de2007-08-23 22:56:55 +0000123
124static PyObject * math_ceil(PyObject *self, PyObject *number) {
125 static PyObject *ceil_str = NULL;
126 PyObject *method;
127
128 if (ceil_str == NULL) {
129 ceil_str = PyUnicode_FromString("__ceil__");
130 if (ceil_str == NULL)
131 return NULL;
132 }
133
Christian Heimes90aa7642007-12-19 02:45:37 +0000134 method = _PyType_Lookup(Py_TYPE(number), ceil_str);
Guido van Rossum13e05de2007-08-23 22:56:55 +0000135 if (method == NULL)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000136 return math_1_to_int(number, ceil);
Guido van Rossum13e05de2007-08-23 22:56:55 +0000137 else
138 return PyObject_CallFunction(method, "O", number);
139}
140
141PyDoc_STRVAR(math_ceil_doc,
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000142 "ceil(x)\n\nReturn the ceiling of x as an int.\n"
Guido van Rossum13e05de2007-08-23 22:56:55 +0000143 "This is the smallest integral value >= x.");
144
Fred Drake40c48682000-07-03 18:11:56 +0000145FUNC1(cos, cos,
Tim Petersfe71f812001-08-07 22:10:00 +0000146 "cos(x)\n\nReturn the cosine of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000147FUNC1(cosh, cosh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000148 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
Christian Heimesa34706f2008-01-04 03:06:10 +0000149
Christian Heimes072c0f12008-01-03 23:01:04 +0000150#ifdef MS_WINDOWS
Christian Heimesa34706f2008-01-04 03:06:10 +0000151# define copysign _copysign
152# define HAVE_COPYSIGN 1
153#endif
154#ifdef HAVE_COPYSIGN
Christian Heimes072c0f12008-01-03 23:01:04 +0000155FUNC2(copysign, copysign,
Christian Heimesa34706f2008-01-04 03:06:10 +0000156 "copysign(x,y)\n\nReturn x with the sign of y.");
Christian Heimes072c0f12008-01-03 23:01:04 +0000157#endif
Christian Heimesa34706f2008-01-04 03:06:10 +0000158
Fred Drake40c48682000-07-03 18:11:56 +0000159FUNC1(exp, exp,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000160 "exp(x)\n\nReturn e raised to the power of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000161FUNC1(fabs, fabs,
Tim Petersfe71f812001-08-07 22:10:00 +0000162 "fabs(x)\n\nReturn the absolute value of the float x.")
Guido van Rossum13e05de2007-08-23 22:56:55 +0000163
164static PyObject * math_floor(PyObject *self, PyObject *number) {
165 static PyObject *floor_str = NULL;
166 PyObject *method;
167
168 if (floor_str == NULL) {
169 floor_str = PyUnicode_FromString("__floor__");
170 if (floor_str == NULL)
171 return NULL;
172 }
173
Christian Heimes90aa7642007-12-19 02:45:37 +0000174 method = _PyType_Lookup(Py_TYPE(number), floor_str);
Guido van Rossum13e05de2007-08-23 22:56:55 +0000175 if (method == NULL)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000176 return math_1_to_int(number, floor);
Guido van Rossum13e05de2007-08-23 22:56:55 +0000177 else
178 return PyObject_CallFunction(method, "O", number);
179}
180
181PyDoc_STRVAR(math_floor_doc,
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000182 "floor(x)\n\nReturn the floor of x as an int.\n"
Guido van Rossum13e05de2007-08-23 22:56:55 +0000183 "This is the largest integral value <= x.");
184
Peter Schneider-Kamp07c19222001-06-10 16:45:08 +0000185FUNC2(fmod, fmod,
Tim Peters78fc0b52000-09-16 03:54:24 +0000186 "fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
187 " x % y may differ.")
Fred Drake40c48682000-07-03 18:11:56 +0000188FUNC2(hypot, hypot,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000189 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
Fred Drake40c48682000-07-03 18:11:56 +0000190FUNC2(pow, pow,
Tim Petersfe71f812001-08-07 22:10:00 +0000191 "pow(x,y)\n\nReturn x**y (x to the power of y).")
Fred Drake40c48682000-07-03 18:11:56 +0000192FUNC1(sin, sin,
Tim Petersfe71f812001-08-07 22:10:00 +0000193 "sin(x)\n\nReturn the sine of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000194FUNC1(sinh, sinh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000195 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000196FUNC1(sqrt, sqrt,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000197 "sqrt(x)\n\nReturn the square root of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000198FUNC1(tan, tan,
Tim Petersfe71f812001-08-07 22:10:00 +0000199 "tan(x)\n\nReturn the tangent of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000200FUNC1(tanh, tanh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000201 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202
Barry Warsaw8b43b191996-12-09 22:32:36 +0000203static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +0000204math_frexp(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000205{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000206 int i;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000207 double x = PyFloat_AsDouble(arg);
208 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000209 return NULL;
210 errno = 0;
211 x = frexp(x, &i);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000212 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000213 if (errno && is_error(x))
214 return NULL;
215 else
216 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000217}
218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000219PyDoc_STRVAR(math_frexp_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000220"frexp(x)\n"
221"\n"
222"Return the mantissa and exponent of x, as pair (m, e).\n"
223"m is a float and e is an int, such that x = m * 2.**e.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000224"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 +0000225
Barry Warsaw8b43b191996-12-09 22:32:36 +0000226static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000227math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000228{
Guido van Rossumc5545052000-05-08 14:29:38 +0000229 double x;
230 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000231 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000232 return NULL;
233 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000234 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000235 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000236 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000237 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000238 if (errno && is_error(x))
239 return NULL;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000240 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000241 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000242}
243
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000244PyDoc_STRVAR(math_ldexp_doc,
245"ldexp(x, i) -> x * (2**i)");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000246
Barry Warsaw8b43b191996-12-09 22:32:36 +0000247static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +0000248math_modf(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000249{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000250 double y, x = PyFloat_AsDouble(arg);
251 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000252 return NULL;
253 errno = 0;
254 x = modf(x, &y);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000255 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000256 if (errno && is_error(x))
257 return NULL;
258 else
259 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000260}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000262PyDoc_STRVAR(math_modf_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000263"modf(x)\n"
264"\n"
265"Return the fractional and integer parts of x. Both results carry the sign\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000266"of x. The integer part is returned as a real.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000267
Tim Peters78526162001-09-05 00:53:45 +0000268/* A decent logarithm is easy to compute even for huge longs, but libm can't
269 do that by itself -- loghelper can. func is log or log10, and name is
270 "log" or "log10". Note that overflow isn't possible: a long can contain
271 no more than INT_MAX * SHIFT bits, so has value certainly less than
272 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
273 small enough to fit in an IEEE single. log and log10 are even smaller.
274*/
275
276static PyObject*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000277loghelper(PyObject* arg, double (*func)(double), char *funcname)
Tim Peters78526162001-09-05 00:53:45 +0000278{
Tim Peters78526162001-09-05 00:53:45 +0000279 /* If it is long, do it ourselves. */
280 if (PyLong_Check(arg)) {
281 double x;
282 int e;
283 x = _PyLong_AsScaledDouble(arg, &e);
284 if (x <= 0.0) {
285 PyErr_SetString(PyExc_ValueError,
286 "math domain error");
287 return NULL;
288 }
289 /* Value is ~= x * 2**(e*SHIFT), so the log ~=
290 log(x) + log(2) * e * SHIFT.
291 CAUTION: e*SHIFT may overflow using int arithmetic,
292 so force use of double. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000293 x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0);
Tim Peters78526162001-09-05 00:53:45 +0000294 return PyFloat_FromDouble(x);
295 }
296
297 /* Else let libm handle it by itself. */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000298 return math_1(arg, func);
Tim Peters78526162001-09-05 00:53:45 +0000299}
300
301static PyObject *
302math_log(PyObject *self, PyObject *args)
303{
Raymond Hettinger866964c2002-12-14 19:51:34 +0000304 PyObject *arg;
305 PyObject *base = NULL;
306 PyObject *num, *den;
307 PyObject *ans;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000308
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000309 if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
Raymond Hettinger866964c2002-12-14 19:51:34 +0000310 return NULL;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000311
Thomas Wouters89f507f2006-12-13 04:49:30 +0000312 num = loghelper(arg, log, "log");
313 if (num == NULL || base == NULL)
314 return num;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000315
Thomas Wouters89f507f2006-12-13 04:49:30 +0000316 den = loghelper(base, log, "log");
Raymond Hettinger866964c2002-12-14 19:51:34 +0000317 if (den == NULL) {
318 Py_DECREF(num);
319 return NULL;
320 }
321
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000322 ans = PyNumber_TrueDivide(num, den);
Raymond Hettinger866964c2002-12-14 19:51:34 +0000323 Py_DECREF(num);
324 Py_DECREF(den);
325 return ans;
Tim Peters78526162001-09-05 00:53:45 +0000326}
327
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000328PyDoc_STRVAR(math_log_doc,
Raymond Hettinger866964c2002-12-14 19:51:34 +0000329"log(x[, base]) -> the logarithm of x to the given base.\n\
330If the base not specified, returns the natural logarithm (base e) of x.");
Tim Peters78526162001-09-05 00:53:45 +0000331
332static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +0000333math_log10(PyObject *self, PyObject *arg)
Tim Peters78526162001-09-05 00:53:45 +0000334{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000335 return loghelper(arg, log10, "log10");
Tim Peters78526162001-09-05 00:53:45 +0000336}
337
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000338PyDoc_STRVAR(math_log10_doc,
339"log10(x) -> the base 10 logarithm of x.");
Tim Peters78526162001-09-05 00:53:45 +0000340
Christian Heimes072c0f12008-01-03 23:01:04 +0000341static const double degToRad = Py_MATH_PI / 180.0;
342static const double radToDeg = 180.0 / Py_MATH_PI;
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000343
344static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +0000345math_degrees(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000346{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000347 double x = PyFloat_AsDouble(arg);
348 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000349 return NULL;
Christian Heimes072c0f12008-01-03 23:01:04 +0000350 return PyFloat_FromDouble(x * radToDeg);
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000351}
352
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000353PyDoc_STRVAR(math_degrees_doc,
354"degrees(x) -> converts angle x from radians to degrees");
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000355
356static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +0000357math_radians(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000358{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000359 double x = PyFloat_AsDouble(arg);
360 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000361 return NULL;
362 return PyFloat_FromDouble(x * degToRad);
363}
364
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000365PyDoc_STRVAR(math_radians_doc,
366"radians(x) -> converts angle x from degrees to radians");
Tim Peters78526162001-09-05 00:53:45 +0000367
Christian Heimes072c0f12008-01-03 23:01:04 +0000368static PyObject *
369math_isnan(PyObject *self, PyObject *arg)
370{
371 double x = PyFloat_AsDouble(arg);
372 if (x == -1.0 && PyErr_Occurred())
373 return NULL;
374 return PyBool_FromLong((long)Py_IS_NAN(x));
375}
376
377PyDoc_STRVAR(math_isnan_doc,
378"isnan(x) -> bool\n\
379Checks if float x is not a number (NaN)");
380
381static PyObject *
382math_isinf(PyObject *self, PyObject *arg)
383{
384 double x = PyFloat_AsDouble(arg);
385 if (x == -1.0 && PyErr_Occurred())
386 return NULL;
387 return PyBool_FromLong((long)Py_IS_INFINITY(x));
388}
389
390PyDoc_STRVAR(math_isinf_doc,
391"isinf(x) -> bool\n\
392Checks if float x is infinite (positive or negative)");
393
394
Barry Warsaw8b43b191996-12-09 22:32:36 +0000395static PyMethodDef math_methods[] = {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000396 {"acos", math_acos, METH_O, math_acos_doc},
397 {"asin", math_asin, METH_O, math_asin_doc},
398 {"atan", math_atan, METH_O, math_atan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000399 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +0000400 {"ceil", math_ceil, METH_O, math_ceil_doc},
Christian Heimesa34706f2008-01-04 03:06:10 +0000401#ifdef HAVE_COPYSIGN
Christian Heimes072c0f12008-01-03 23:01:04 +0000402 {"copysign", math_copysign, METH_VARARGS, math_copysign_doc},
403#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +0000404 {"cos", math_cos, METH_O, math_cos_doc},
405 {"cosh", math_cosh, METH_O, math_cosh_doc},
406 {"degrees", math_degrees, METH_O, math_degrees_doc},
407 {"exp", math_exp, METH_O, math_exp_doc},
408 {"fabs", math_fabs, METH_O, math_fabs_doc},
409 {"floor", math_floor, METH_O, math_floor_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000410 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +0000411 {"frexp", math_frexp, METH_O, math_frexp_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000412 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
Christian Heimes072c0f12008-01-03 23:01:04 +0000413 {"isinf", math_isinf, METH_O, math_isinf_doc},
414 {"isnan", math_isnan, METH_O, math_isnan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000415 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
416 {"log", math_log, METH_VARARGS, math_log_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +0000417 {"log10", math_log10, METH_O, math_log10_doc},
418 {"modf", math_modf, METH_O, math_modf_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000419 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +0000420 {"radians", math_radians, METH_O, math_radians_doc},
421 {"sin", math_sin, METH_O, math_sin_doc},
422 {"sinh", math_sinh, METH_O, math_sinh_doc},
423 {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
424 {"tan", math_tan, METH_O, math_tan_doc},
425 {"tanh", math_tanh, METH_O, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426 {NULL, NULL} /* sentinel */
427};
428
Guido van Rossumc6e22901998-12-04 19:26:43 +0000429
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000430PyDoc_STRVAR(module_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000431"This module is always available. It provides access to the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000432"mathematical functions defined by the C standard.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000433
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000434PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000435initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000437 PyObject *m, *d, *v;
Tim Petersfe71f812001-08-07 22:10:00 +0000438
Guido van Rossumc6e22901998-12-04 19:26:43 +0000439 m = Py_InitModule3("math", math_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000440 if (m == NULL)
441 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000442 d = PyModule_GetDict(m);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000443 if (d == NULL)
444 goto finally;
Barry Warsawfc93f751996-12-17 00:47:03 +0000445
Christian Heimes072c0f12008-01-03 23:01:04 +0000446 if (!(v = PyFloat_FromDouble(Py_MATH_PI)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000447 goto finally;
448 if (PyDict_SetItemString(d, "pi", v) < 0)
449 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000450 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000451
Christian Heimes072c0f12008-01-03 23:01:04 +0000452 if (!(v = PyFloat_FromDouble(Py_MATH_E)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000453 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000454 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000455 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000456 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000457
458 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000459 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460}