blob: cdc305710349185d3bd167be1754f52e94d69ba4 [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
Neal Norwitz5f95a792008-01-25 08:04:16 +000015#ifdef _OSF_SOURCE
16/* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */
17extern double copysign(double, double);
18#endif
19
Tim Peters1d120612000-10-12 06:10:25 +000020/* Call is_error when errno != 0, and where x is the result libm
21 * returned. is_error will usually set up an exception and return
22 * true (1), but may return false (0) without setting up an exception.
23 */
24static int
25is_error(double x)
Guido van Rossum8832b621991-12-16 15:44:24 +000026{
Tim Peters1d120612000-10-12 06:10:25 +000027 int result = 1; /* presumption of guilt */
Tim Peters2bf405a2000-10-12 19:42:00 +000028 assert(errno); /* non-zero errno is a precondition for calling */
Guido van Rossum8832b621991-12-16 15:44:24 +000029 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000030 PyErr_SetString(PyExc_ValueError, "math domain error");
Tim Petersa40c7932001-09-05 22:36:56 +000031
Tim Peters1d120612000-10-12 06:10:25 +000032 else if (errno == ERANGE) {
33 /* ANSI C generally requires libm functions to set ERANGE
34 * on overflow, but also generally *allows* them to set
35 * ERANGE on underflow too. There's no consistency about
Tim Petersa40c7932001-09-05 22:36:56 +000036 * the latter across platforms.
37 * Alas, C99 never requires that errno be set.
38 * Here we suppress the underflow errors (libm functions
39 * should return a zero on underflow, and +- HUGE_VAL on
40 * overflow, so testing the result for zero suffices to
41 * distinguish the cases).
Tim Peters1d120612000-10-12 06:10:25 +000042 */
43 if (x)
Tim Petersfe71f812001-08-07 22:10:00 +000044 PyErr_SetString(PyExc_OverflowError,
Tim Peters1d120612000-10-12 06:10:25 +000045 "math range error");
46 else
47 result = 0;
48 }
Guido van Rossum8832b621991-12-16 15:44:24 +000049 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000050 /* Unexpected math error */
51 PyErr_SetFromErrno(PyExc_ValueError);
Tim Peters1d120612000-10-12 06:10:25 +000052 return result;
Guido van Rossum8832b621991-12-16 15:44:24 +000053}
54
Barry Warsaw8b43b191996-12-09 22:32:36 +000055static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +000056math_1(PyObject *arg, double (*func) (double))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057{
Neal Norwitz45e230a2006-11-19 21:26:53 +000058 double x = PyFloat_AsDouble(arg);
59 if (x == -1.0 && PyErr_Occurred())
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060 return NULL;
61 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000062 PyFPE_START_PROTECT("in math_1", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +000064 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +000065 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +000066 if (errno && is_error(x))
67 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000069 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070}
71
Barry Warsaw8b43b191996-12-09 22:32:36 +000072static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +000073math_2(PyObject *args, double (*func) (double, double), char *funcname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074{
Neal Norwitz45e230a2006-11-19 21:26:53 +000075 PyObject *ox, *oy;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 double x, y;
Neal Norwitz45e230a2006-11-19 21:26:53 +000077 if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy))
78 return NULL;
79 x = PyFloat_AsDouble(ox);
80 y = PyFloat_AsDouble(oy);
81 if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 return NULL;
83 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000084 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000086 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +000087 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +000088 if (errno && is_error(x))
89 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000091 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092}
93
Fred Drake40c48682000-07-03 18:11:56 +000094#define FUNC1(funcname, func, docstring) \
95 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
Neal Norwitz45e230a2006-11-19 21:26:53 +000096 return math_1(args, func); \
Guido van Rossumc6e22901998-12-04 19:26:43 +000097 }\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000098 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099
Fred Drake40c48682000-07-03 18:11:56 +0000100#define FUNC2(funcname, func, docstring) \
101 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
Neal Norwitz45e230a2006-11-19 21:26:53 +0000102 return math_2(args, func, #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000103 }\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105
Fred Drake40c48682000-07-03 18:11:56 +0000106FUNC1(acos, acos,
Tim Petersfe71f812001-08-07 22:10:00 +0000107 "acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000108FUNC1(asin, asin,
Tim Petersfe71f812001-08-07 22:10:00 +0000109 "asin(x)\n\nReturn the arc sine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000110FUNC1(atan, atan,
Tim Petersfe71f812001-08-07 22:10:00 +0000111 "atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
Martin v. Löwis387c5472001-09-06 08:16:17 +0000112FUNC2(atan2, atan2,
Tim Petersfe71f812001-08-07 22:10:00 +0000113 "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
114 "Unlike atan(y/x), the signs of both x and y are considered.")
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000115FUNC1(ceil, ceil,
116 "ceil(x)\n\nReturn the ceiling of x as a float.\n"
117 "This is the smallest integral value >= x.")
Fred Drake40c48682000-07-03 18:11:56 +0000118FUNC1(cos, cos,
Tim Petersfe71f812001-08-07 22:10:00 +0000119 "cos(x)\n\nReturn the cosine of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000120FUNC1(cosh, cosh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000121 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
Christian Heimes66ecda42008-01-04 01:48:50 +0000122
Christian Heimeseebb79c2008-01-03 22:32:26 +0000123#ifdef MS_WINDOWS
Christian Heimes66ecda42008-01-04 01:48:50 +0000124# define copysign _copysign
125# define HAVE_COPYSIGN 1
126#endif
127#ifdef HAVE_COPYSIGN
Christian Heimeseebb79c2008-01-03 22:32:26 +0000128FUNC2(copysign, copysign,
Christian Heimes66ecda42008-01-04 01:48:50 +0000129 "copysign(x,y)\n\nReturn x with the sign of y.");
Christian Heimeseebb79c2008-01-03 22:32:26 +0000130#endif
Christian Heimes66ecda42008-01-04 01:48:50 +0000131
Fred Drake40c48682000-07-03 18:11:56 +0000132FUNC1(exp, exp,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000133 "exp(x)\n\nReturn e raised to the power of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000134FUNC1(fabs, fabs,
Tim Petersfe71f812001-08-07 22:10:00 +0000135 "fabs(x)\n\nReturn the absolute value of the float x.")
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000136FUNC1(floor, floor,
137 "floor(x)\n\nReturn the floor of x as a float.\n"
138 "This is the largest integral value <= x.")
Peter Schneider-Kamp07c19222001-06-10 16:45:08 +0000139FUNC2(fmod, fmod,
Tim Peters78fc0b52000-09-16 03:54:24 +0000140 "fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
141 " x % y may differ.")
Fred Drake40c48682000-07-03 18:11:56 +0000142FUNC2(hypot, hypot,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000143 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
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).")
Fred Drake40c48682000-07-03 18:11:56 +0000146FUNC1(sin, sin,
Tim Petersfe71f812001-08-07 22:10:00 +0000147 "sin(x)\n\nReturn the sine of x (measured in radians).")
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,
Tim Petersfe71f812001-08-07 22:10:00 +0000153 "tan(x)\n\nReturn the tangent of x (measured in radians).")
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
Barry Warsaw8b43b191996-12-09 22:32:36 +0000157static PyObject *
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000158math_trunc(PyObject *self, PyObject *number)
159{
160 /* XXX: The py3k branch gets better errors for this by using
161 _PyType_Lookup(), but since float's mro isn't set in py2.6,
162 we just use PyObject_CallMethod here. */
163 return PyObject_CallMethod(number, "__trunc__", NULL);
164}
165
166PyDoc_STRVAR(math_trunc_doc,
167"trunc(x:Real) -> Integral\n"
168"\n"
169"Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic"
170"method.");
171
172static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000173math_frexp(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000174{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000175 int i;
Neal Norwitz45e230a2006-11-19 21:26:53 +0000176 double x = PyFloat_AsDouble(arg);
177 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000178 return NULL;
179 errno = 0;
180 x = frexp(x, &i);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000181 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000182 if (errno && is_error(x))
183 return NULL;
184 else
185 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000186}
187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000188PyDoc_STRVAR(math_frexp_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000189"frexp(x)\n"
190"\n"
191"Return the mantissa and exponent of x, as pair (m, e).\n"
192"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 +0000193"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 +0000194
Barry Warsaw8b43b191996-12-09 22:32:36 +0000195static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000196math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000197{
Guido van Rossumc5545052000-05-08 14:29:38 +0000198 double x;
199 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000200 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000201 return NULL;
202 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000203 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000204 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000205 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000206 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000207 if (errno && is_error(x))
208 return NULL;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000209 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000210 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000211}
212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000213PyDoc_STRVAR(math_ldexp_doc,
214"ldexp(x, i) -> x * (2**i)");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000215
Barry Warsaw8b43b191996-12-09 22:32:36 +0000216static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000217math_modf(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000218{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000219 double y, x = PyFloat_AsDouble(arg);
220 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000221 return NULL;
222 errno = 0;
223 x = modf(x, &y);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000224 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000225 if (errno && is_error(x))
226 return NULL;
227 else
228 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000229}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000231PyDoc_STRVAR(math_modf_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000232"modf(x)\n"
233"\n"
234"Return the fractional and integer parts of x. Both results carry the sign\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000235"of x. The integer part is returned as a real.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000236
Tim Peters78526162001-09-05 00:53:45 +0000237/* A decent logarithm is easy to compute even for huge longs, but libm can't
238 do that by itself -- loghelper can. func is log or log10, and name is
239 "log" or "log10". Note that overflow isn't possible: a long can contain
240 no more than INT_MAX * SHIFT bits, so has value certainly less than
241 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
242 small enough to fit in an IEEE single. log and log10 are even smaller.
243*/
244
245static PyObject*
Neal Norwitz45e230a2006-11-19 21:26:53 +0000246loghelper(PyObject* arg, double (*func)(double), char *funcname)
Tim Peters78526162001-09-05 00:53:45 +0000247{
Tim Peters78526162001-09-05 00:53:45 +0000248 /* If it is long, do it ourselves. */
249 if (PyLong_Check(arg)) {
250 double x;
251 int e;
252 x = _PyLong_AsScaledDouble(arg, &e);
253 if (x <= 0.0) {
254 PyErr_SetString(PyExc_ValueError,
255 "math domain error");
256 return NULL;
257 }
Christian Heimes543cabc2008-01-25 14:54:23 +0000258 /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~=
259 log(x) + log(2) * e * PyLong_SHIFT.
260 CAUTION: e*PyLong_SHIFT may overflow using int arithmetic,
Tim Peters78526162001-09-05 00:53:45 +0000261 so force use of double. */
Christian Heimes543cabc2008-01-25 14:54:23 +0000262 x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0);
Tim Peters78526162001-09-05 00:53:45 +0000263 return PyFloat_FromDouble(x);
264 }
265
266 /* Else let libm handle it by itself. */
Neal Norwitz45e230a2006-11-19 21:26:53 +0000267 return math_1(arg, func);
Tim Peters78526162001-09-05 00:53:45 +0000268}
269
270static PyObject *
271math_log(PyObject *self, PyObject *args)
272{
Raymond Hettinger866964c2002-12-14 19:51:34 +0000273 PyObject *arg;
274 PyObject *base = NULL;
275 PyObject *num, *den;
276 PyObject *ans;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000277
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000278 if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
Raymond Hettinger866964c2002-12-14 19:51:34 +0000279 return NULL;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000280
Neal Norwitz45e230a2006-11-19 21:26:53 +0000281 num = loghelper(arg, log, "log");
282 if (num == NULL || base == NULL)
283 return num;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000284
Neal Norwitz45e230a2006-11-19 21:26:53 +0000285 den = loghelper(base, log, "log");
Raymond Hettinger866964c2002-12-14 19:51:34 +0000286 if (den == NULL) {
287 Py_DECREF(num);
288 return NULL;
289 }
290
291 ans = PyNumber_Divide(num, den);
292 Py_DECREF(num);
293 Py_DECREF(den);
294 return ans;
Tim Peters78526162001-09-05 00:53:45 +0000295}
296
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000297PyDoc_STRVAR(math_log_doc,
Raymond Hettinger866964c2002-12-14 19:51:34 +0000298"log(x[, base]) -> the logarithm of x to the given base.\n\
299If the base not specified, returns the natural logarithm (base e) of x.");
Tim Peters78526162001-09-05 00:53:45 +0000300
301static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000302math_log10(PyObject *self, PyObject *arg)
Tim Peters78526162001-09-05 00:53:45 +0000303{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000304 return loghelper(arg, log10, "log10");
Tim Peters78526162001-09-05 00:53:45 +0000305}
306
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000307PyDoc_STRVAR(math_log10_doc,
308"log10(x) -> the base 10 logarithm of x.");
Tim Peters78526162001-09-05 00:53:45 +0000309
Christian Heimese2ca4242008-01-03 20:23:15 +0000310static const double degToRad = Py_MATH_PI / 180.0;
311static const double radToDeg = 180.0 / Py_MATH_PI;
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000312
313static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000314math_degrees(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000315{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000316 double x = PyFloat_AsDouble(arg);
317 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000318 return NULL;
Christian Heimese2ca4242008-01-03 20:23:15 +0000319 return PyFloat_FromDouble(x * radToDeg);
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000320}
321
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000322PyDoc_STRVAR(math_degrees_doc,
323"degrees(x) -> converts angle x from radians to degrees");
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000324
325static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000326math_radians(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000327{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000328 double x = PyFloat_AsDouble(arg);
329 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000330 return NULL;
331 return PyFloat_FromDouble(x * degToRad);
332}
333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000334PyDoc_STRVAR(math_radians_doc,
335"radians(x) -> converts angle x from degrees to radians");
Tim Peters78526162001-09-05 00:53:45 +0000336
Christian Heimese2ca4242008-01-03 20:23:15 +0000337static PyObject *
338math_isnan(PyObject *self, PyObject *arg)
339{
340 double x = PyFloat_AsDouble(arg);
341 if (x == -1.0 && PyErr_Occurred())
342 return NULL;
343 return PyBool_FromLong((long)Py_IS_NAN(x));
344}
345
346PyDoc_STRVAR(math_isnan_doc,
347"isnan(x) -> bool\n\
348Checks if float x is not a number (NaN)");
349
350static PyObject *
351math_isinf(PyObject *self, PyObject *arg)
352{
353 double x = PyFloat_AsDouble(arg);
354 if (x == -1.0 && PyErr_Occurred())
355 return NULL;
356 return PyBool_FromLong((long)Py_IS_INFINITY(x));
357}
358
359PyDoc_STRVAR(math_isinf_doc,
360"isinf(x) -> bool\n\
361Checks if float x is infinite (positive or negative)");
362
363
Barry Warsaw8b43b191996-12-09 22:32:36 +0000364static PyMethodDef math_methods[] = {
Neal Norwitz45e230a2006-11-19 21:26:53 +0000365 {"acos", math_acos, METH_O, math_acos_doc},
366 {"asin", math_asin, METH_O, math_asin_doc},
367 {"atan", math_atan, METH_O, math_atan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000368 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000369 {"ceil", math_ceil, METH_O, math_ceil_doc},
Christian Heimes66ecda42008-01-04 01:48:50 +0000370#ifdef HAVE_COPYSIGN
Christian Heimeseebb79c2008-01-03 22:32:26 +0000371 {"copysign", math_copysign, METH_VARARGS, math_copysign_doc},
372#endif
Neal Norwitz45e230a2006-11-19 21:26:53 +0000373 {"cos", math_cos, METH_O, math_cos_doc},
374 {"cosh", math_cosh, METH_O, math_cosh_doc},
375 {"degrees", math_degrees, METH_O, math_degrees_doc},
376 {"exp", math_exp, METH_O, math_exp_doc},
377 {"fabs", math_fabs, METH_O, math_fabs_doc},
378 {"floor", math_floor, METH_O, math_floor_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000379 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000380 {"frexp", math_frexp, METH_O, math_frexp_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000381 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
Christian Heimese2ca4242008-01-03 20:23:15 +0000382 {"isinf", math_isinf, METH_O, math_isinf_doc},
383 {"isnan", math_isnan, METH_O, math_isnan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000384 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
385 {"log", math_log, METH_VARARGS, math_log_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000386 {"log10", math_log10, METH_O, math_log10_doc},
387 {"modf", math_modf, METH_O, math_modf_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000388 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000389 {"radians", math_radians, METH_O, math_radians_doc},
390 {"sin", math_sin, METH_O, math_sin_doc},
391 {"sinh", math_sinh, METH_O, math_sinh_doc},
392 {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
393 {"tan", math_tan, METH_O, math_tan_doc},
394 {"tanh", math_tanh, METH_O, math_tanh_doc},
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000395 {"trunc", math_trunc, METH_O, math_trunc_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396 {NULL, NULL} /* sentinel */
397};
398
Guido van Rossumc6e22901998-12-04 19:26:43 +0000399
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000400PyDoc_STRVAR(module_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000401"This module is always available. It provides access to the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000402"mathematical functions defined by the C standard.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000403
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000404PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000405initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000407 PyObject *m, *d, *v;
Tim Petersfe71f812001-08-07 22:10:00 +0000408
Guido van Rossumc6e22901998-12-04 19:26:43 +0000409 m = Py_InitModule3("math", math_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000410 if (m == NULL)
411 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000412 d = PyModule_GetDict(m);
Neal Norwitz45e230a2006-11-19 21:26:53 +0000413 if (d == NULL)
414 goto finally;
Barry Warsawfc93f751996-12-17 00:47:03 +0000415
Christian Heimese2ca4242008-01-03 20:23:15 +0000416 if (!(v = PyFloat_FromDouble(Py_MATH_PI)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000417 goto finally;
418 if (PyDict_SetItemString(d, "pi", v) < 0)
419 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000420 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000421
Christian Heimese2ca4242008-01-03 20:23:15 +0000422 if (!(v = PyFloat_FromDouble(Py_MATH_E)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000423 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000424 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000425 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000426 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000427
428 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000429 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430}