blob: 5e52e785416792353730ca69e8f358a6a6e40a2c [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 *
Neal Norwitz45e230a2006-11-19 21:26:53 +000051math_1(PyObject *arg, double (*func) (double))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052{
Neal Norwitz45e230a2006-11-19 21:26:53 +000053 double x = PyFloat_AsDouble(arg);
54 if (x == -1.0 && PyErr_Occurred())
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055 return NULL;
56 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000057 PyFPE_START_PROTECT("in math_1", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +000059 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +000060 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +000061 if (errno && is_error(x))
62 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000064 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065}
66
Barry Warsaw8b43b191996-12-09 22:32:36 +000067static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +000068math_2(PyObject *args, double (*func) (double, double), char *funcname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069{
Neal Norwitz45e230a2006-11-19 21:26:53 +000070 PyObject *ox, *oy;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071 double x, y;
Neal Norwitz45e230a2006-11-19 21:26:53 +000072 if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy))
73 return NULL;
74 x = PyFloat_AsDouble(ox);
75 y = PyFloat_AsDouble(oy);
76 if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077 return NULL;
78 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000079 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000081 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +000082 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +000083 if (errno && is_error(x))
84 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000086 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087}
88
Fred Drake40c48682000-07-03 18:11:56 +000089#define FUNC1(funcname, func, docstring) \
90 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
Neal Norwitz45e230a2006-11-19 21:26:53 +000091 return math_1(args, func); \
Guido van Rossumc6e22901998-12-04 19:26:43 +000092 }\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000093 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094
Fred Drake40c48682000-07-03 18:11:56 +000095#define FUNC2(funcname, func, docstring) \
96 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
Neal Norwitz45e230a2006-11-19 21:26:53 +000097 return math_2(args, func, #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +000098 }\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000099 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100
Fred Drake40c48682000-07-03 18:11:56 +0000101FUNC1(acos, acos,
Tim Petersfe71f812001-08-07 22:10:00 +0000102 "acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000103FUNC1(asin, asin,
Tim Petersfe71f812001-08-07 22:10:00 +0000104 "asin(x)\n\nReturn the arc sine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000105FUNC1(atan, atan,
Tim Petersfe71f812001-08-07 22:10:00 +0000106 "atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
Martin v. Löwis387c5472001-09-06 08:16:17 +0000107FUNC2(atan2, atan2,
Tim Petersfe71f812001-08-07 22:10:00 +0000108 "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
109 "Unlike atan(y/x), the signs of both x and y are considered.")
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000110FUNC1(ceil, ceil,
111 "ceil(x)\n\nReturn the ceiling of x as a float.\n"
112 "This is the smallest integral value >= x.")
Fred Drake40c48682000-07-03 18:11:56 +0000113FUNC1(cos, cos,
Tim Petersfe71f812001-08-07 22:10:00 +0000114 "cos(x)\n\nReturn the cosine of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000115FUNC1(cosh, cosh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000116 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
Christian Heimes66ecda42008-01-04 01:48:50 +0000117
Christian Heimeseebb79c2008-01-03 22:32:26 +0000118#ifdef MS_WINDOWS
Christian Heimes66ecda42008-01-04 01:48:50 +0000119# define copysign _copysign
120# define HAVE_COPYSIGN 1
121#endif
122#ifdef HAVE_COPYSIGN
Christian Heimeseebb79c2008-01-03 22:32:26 +0000123FUNC2(copysign, copysign,
Christian Heimes66ecda42008-01-04 01:48:50 +0000124 "copysign(x,y)\n\nReturn x with the sign of y.");
Christian Heimeseebb79c2008-01-03 22:32:26 +0000125#endif
Christian Heimes66ecda42008-01-04 01:48:50 +0000126
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.")
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000131FUNC1(floor, floor,
132 "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 +0000139FUNC2(pow, pow,
Tim Petersfe71f812001-08-07 22:10:00 +0000140 "pow(x,y)\n\nReturn x**y (x to the power of y).")
Fred Drake40c48682000-07-03 18:11:56 +0000141FUNC1(sin, sin,
Tim Petersfe71f812001-08-07 22:10:00 +0000142 "sin(x)\n\nReturn the sine of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000143FUNC1(sinh, sinh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000144 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000145FUNC1(sqrt, sqrt,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000146 "sqrt(x)\n\nReturn the square root of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000147FUNC1(tan, tan,
Tim Petersfe71f812001-08-07 22:10:00 +0000148 "tan(x)\n\nReturn the tangent of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000149FUNC1(tanh, tanh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000150 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151
Barry Warsaw8b43b191996-12-09 22:32:36 +0000152static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000153math_frexp(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000154{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000155 int i;
Neal Norwitz45e230a2006-11-19 21:26:53 +0000156 double x = PyFloat_AsDouble(arg);
157 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000158 return NULL;
159 errno = 0;
160 x = frexp(x, &i);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000161 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000162 if (errno && is_error(x))
163 return NULL;
164 else
165 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000166}
167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000168PyDoc_STRVAR(math_frexp_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000169"frexp(x)\n"
170"\n"
171"Return the mantissa and exponent of x, as pair (m, e).\n"
172"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 +0000173"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 +0000174
Barry Warsaw8b43b191996-12-09 22:32:36 +0000175static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000176math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000177{
Guido van Rossumc5545052000-05-08 14:29:38 +0000178 double x;
179 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000180 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000181 return NULL;
182 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000183 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000184 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000185 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000186 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000187 if (errno && is_error(x))
188 return NULL;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000189 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000190 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000191}
192
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000193PyDoc_STRVAR(math_ldexp_doc,
194"ldexp(x, i) -> x * (2**i)");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000195
Barry Warsaw8b43b191996-12-09 22:32:36 +0000196static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000197math_modf(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000198{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000199 double y, x = PyFloat_AsDouble(arg);
200 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000201 return NULL;
202 errno = 0;
203 x = modf(x, &y);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000204 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000205 if (errno && is_error(x))
206 return NULL;
207 else
208 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000209}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000211PyDoc_STRVAR(math_modf_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000212"modf(x)\n"
213"\n"
214"Return the fractional and integer parts of x. Both results carry the sign\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000215"of x. The integer part is returned as a real.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000216
Tim Peters78526162001-09-05 00:53:45 +0000217/* A decent logarithm is easy to compute even for huge longs, but libm can't
218 do that by itself -- loghelper can. func is log or log10, and name is
219 "log" or "log10". Note that overflow isn't possible: a long can contain
220 no more than INT_MAX * SHIFT bits, so has value certainly less than
221 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
222 small enough to fit in an IEEE single. log and log10 are even smaller.
223*/
224
225static PyObject*
Neal Norwitz45e230a2006-11-19 21:26:53 +0000226loghelper(PyObject* arg, double (*func)(double), char *funcname)
Tim Peters78526162001-09-05 00:53:45 +0000227{
Tim Peters78526162001-09-05 00:53:45 +0000228 /* If it is long, do it ourselves. */
229 if (PyLong_Check(arg)) {
230 double x;
231 int e;
232 x = _PyLong_AsScaledDouble(arg, &e);
233 if (x <= 0.0) {
234 PyErr_SetString(PyExc_ValueError,
235 "math domain error");
236 return NULL;
237 }
238 /* Value is ~= x * 2**(e*SHIFT), so the log ~=
239 log(x) + log(2) * e * SHIFT.
240 CAUTION: e*SHIFT may overflow using int arithmetic,
241 so force use of double. */
Tim Peterse5ca6c72001-09-05 04:33:11 +0000242 x = func(x) + (e * (double)SHIFT) * func(2.0);
Tim Peters78526162001-09-05 00:53:45 +0000243 return PyFloat_FromDouble(x);
244 }
245
246 /* Else let libm handle it by itself. */
Neal Norwitz45e230a2006-11-19 21:26:53 +0000247 return math_1(arg, func);
Tim Peters78526162001-09-05 00:53:45 +0000248}
249
250static PyObject *
251math_log(PyObject *self, PyObject *args)
252{
Raymond Hettinger866964c2002-12-14 19:51:34 +0000253 PyObject *arg;
254 PyObject *base = NULL;
255 PyObject *num, *den;
256 PyObject *ans;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000257
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000258 if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
Raymond Hettinger866964c2002-12-14 19:51:34 +0000259 return NULL;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000260
Neal Norwitz45e230a2006-11-19 21:26:53 +0000261 num = loghelper(arg, log, "log");
262 if (num == NULL || base == NULL)
263 return num;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000264
Neal Norwitz45e230a2006-11-19 21:26:53 +0000265 den = loghelper(base, log, "log");
Raymond Hettinger866964c2002-12-14 19:51:34 +0000266 if (den == NULL) {
267 Py_DECREF(num);
268 return NULL;
269 }
270
271 ans = PyNumber_Divide(num, den);
272 Py_DECREF(num);
273 Py_DECREF(den);
274 return ans;
Tim Peters78526162001-09-05 00:53:45 +0000275}
276
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000277PyDoc_STRVAR(math_log_doc,
Raymond Hettinger866964c2002-12-14 19:51:34 +0000278"log(x[, base]) -> the logarithm of x to the given base.\n\
279If the base not specified, returns the natural logarithm (base e) of x.");
Tim Peters78526162001-09-05 00:53:45 +0000280
281static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000282math_log10(PyObject *self, PyObject *arg)
Tim Peters78526162001-09-05 00:53:45 +0000283{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000284 return loghelper(arg, log10, "log10");
Tim Peters78526162001-09-05 00:53:45 +0000285}
286
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000287PyDoc_STRVAR(math_log10_doc,
288"log10(x) -> the base 10 logarithm of x.");
Tim Peters78526162001-09-05 00:53:45 +0000289
Christian Heimese2ca4242008-01-03 20:23:15 +0000290static const double degToRad = Py_MATH_PI / 180.0;
291static const double radToDeg = 180.0 / Py_MATH_PI;
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000292
293static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000294math_degrees(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000295{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000296 double x = PyFloat_AsDouble(arg);
297 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000298 return NULL;
Christian Heimese2ca4242008-01-03 20:23:15 +0000299 return PyFloat_FromDouble(x * radToDeg);
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000300}
301
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000302PyDoc_STRVAR(math_degrees_doc,
303"degrees(x) -> converts angle x from radians to degrees");
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000304
305static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000306math_radians(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000307{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000308 double x = PyFloat_AsDouble(arg);
309 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000310 return NULL;
311 return PyFloat_FromDouble(x * degToRad);
312}
313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000314PyDoc_STRVAR(math_radians_doc,
315"radians(x) -> converts angle x from degrees to radians");
Tim Peters78526162001-09-05 00:53:45 +0000316
Christian Heimese2ca4242008-01-03 20:23:15 +0000317static PyObject *
318math_isnan(PyObject *self, PyObject *arg)
319{
320 double x = PyFloat_AsDouble(arg);
321 if (x == -1.0 && PyErr_Occurred())
322 return NULL;
323 return PyBool_FromLong((long)Py_IS_NAN(x));
324}
325
326PyDoc_STRVAR(math_isnan_doc,
327"isnan(x) -> bool\n\
328Checks if float x is not a number (NaN)");
329
330static PyObject *
331math_isinf(PyObject *self, PyObject *arg)
332{
333 double x = PyFloat_AsDouble(arg);
334 if (x == -1.0 && PyErr_Occurred())
335 return NULL;
336 return PyBool_FromLong((long)Py_IS_INFINITY(x));
337}
338
339PyDoc_STRVAR(math_isinf_doc,
340"isinf(x) -> bool\n\
341Checks if float x is infinite (positive or negative)");
342
343
Barry Warsaw8b43b191996-12-09 22:32:36 +0000344static PyMethodDef math_methods[] = {
Neal Norwitz45e230a2006-11-19 21:26:53 +0000345 {"acos", math_acos, METH_O, math_acos_doc},
346 {"asin", math_asin, METH_O, math_asin_doc},
347 {"atan", math_atan, METH_O, math_atan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000348 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000349 {"ceil", math_ceil, METH_O, math_ceil_doc},
Christian Heimes66ecda42008-01-04 01:48:50 +0000350#ifdef HAVE_COPYSIGN
Christian Heimeseebb79c2008-01-03 22:32:26 +0000351 {"copysign", math_copysign, METH_VARARGS, math_copysign_doc},
352#endif
Neal Norwitz45e230a2006-11-19 21:26:53 +0000353 {"cos", math_cos, METH_O, math_cos_doc},
354 {"cosh", math_cosh, METH_O, math_cosh_doc},
355 {"degrees", math_degrees, METH_O, math_degrees_doc},
356 {"exp", math_exp, METH_O, math_exp_doc},
357 {"fabs", math_fabs, METH_O, math_fabs_doc},
358 {"floor", math_floor, METH_O, math_floor_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000359 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000360 {"frexp", math_frexp, METH_O, math_frexp_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000361 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
Christian Heimese2ca4242008-01-03 20:23:15 +0000362 {"isinf", math_isinf, METH_O, math_isinf_doc},
363 {"isnan", math_isnan, METH_O, math_isnan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000364 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
365 {"log", math_log, METH_VARARGS, math_log_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000366 {"log10", math_log10, METH_O, math_log10_doc},
367 {"modf", math_modf, METH_O, math_modf_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000368 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000369 {"radians", math_radians, METH_O, math_radians_doc},
370 {"sin", math_sin, METH_O, math_sin_doc},
371 {"sinh", math_sinh, METH_O, math_sinh_doc},
372 {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
373 {"tan", math_tan, METH_O, math_tan_doc},
374 {"tanh", math_tanh, METH_O, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375 {NULL, NULL} /* sentinel */
376};
377
Guido van Rossumc6e22901998-12-04 19:26:43 +0000378
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000379PyDoc_STRVAR(module_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000380"This module is always available. It provides access to the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000381"mathematical functions defined by the C standard.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000382
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000383PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000384initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000386 PyObject *m, *d, *v;
Tim Petersfe71f812001-08-07 22:10:00 +0000387
Guido van Rossumc6e22901998-12-04 19:26:43 +0000388 m = Py_InitModule3("math", math_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000389 if (m == NULL)
390 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000391 d = PyModule_GetDict(m);
Neal Norwitz45e230a2006-11-19 21:26:53 +0000392 if (d == NULL)
393 goto finally;
Barry Warsawfc93f751996-12-17 00:47:03 +0000394
Christian Heimese2ca4242008-01-03 20:23:15 +0000395 if (!(v = PyFloat_FromDouble(Py_MATH_PI)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000396 goto finally;
397 if (PyDict_SetItemString(d, "pi", v) < 0)
398 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000399 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000400
Christian Heimese2ca4242008-01-03 20:23:15 +0000401 if (!(v = PyFloat_FromDouble(Py_MATH_E)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000402 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000403 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000404 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000405 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000406
407 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000408 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409}