blob: 6469bde649eeacd68b027a899ca4f7caeb0f86a5 [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 *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000158math_frexp(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000159{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000160 int i;
Neal Norwitz45e230a2006-11-19 21:26:53 +0000161 double x = PyFloat_AsDouble(arg);
162 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000163 return NULL;
164 errno = 0;
165 x = frexp(x, &i);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000166 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000167 if (errno && is_error(x))
168 return NULL;
169 else
170 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000171}
172
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000173PyDoc_STRVAR(math_frexp_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000174"frexp(x)\n"
175"\n"
176"Return the mantissa and exponent of x, as pair (m, e).\n"
177"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 +0000178"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 +0000179
Barry Warsaw8b43b191996-12-09 22:32:36 +0000180static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000181math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000182{
Guido van Rossumc5545052000-05-08 14:29:38 +0000183 double x;
184 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000185 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000186 return NULL;
187 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000188 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000189 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000190 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000191 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000192 if (errno && is_error(x))
193 return NULL;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000194 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000195 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000196}
197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000198PyDoc_STRVAR(math_ldexp_doc,
199"ldexp(x, i) -> x * (2**i)");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000200
Barry Warsaw8b43b191996-12-09 22:32:36 +0000201static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000202math_modf(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000203{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000204 double y, x = PyFloat_AsDouble(arg);
205 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000206 return NULL;
207 errno = 0;
208 x = modf(x, &y);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000209 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000210 if (errno && is_error(x))
211 return NULL;
212 else
213 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000214}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000216PyDoc_STRVAR(math_modf_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000217"modf(x)\n"
218"\n"
219"Return the fractional and integer parts of x. Both results carry the sign\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000220"of x. The integer part is returned as a real.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000221
Tim Peters78526162001-09-05 00:53:45 +0000222/* A decent logarithm is easy to compute even for huge longs, but libm can't
223 do that by itself -- loghelper can. func is log or log10, and name is
224 "log" or "log10". Note that overflow isn't possible: a long can contain
225 no more than INT_MAX * SHIFT bits, so has value certainly less than
226 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
227 small enough to fit in an IEEE single. log and log10 are even smaller.
228*/
229
230static PyObject*
Neal Norwitz45e230a2006-11-19 21:26:53 +0000231loghelper(PyObject* arg, double (*func)(double), char *funcname)
Tim Peters78526162001-09-05 00:53:45 +0000232{
Tim Peters78526162001-09-05 00:53:45 +0000233 /* If it is long, do it ourselves. */
234 if (PyLong_Check(arg)) {
235 double x;
236 int e;
237 x = _PyLong_AsScaledDouble(arg, &e);
238 if (x <= 0.0) {
239 PyErr_SetString(PyExc_ValueError,
240 "math domain error");
241 return NULL;
242 }
Christian Heimes543cabc2008-01-25 14:54:23 +0000243 /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~=
244 log(x) + log(2) * e * PyLong_SHIFT.
245 CAUTION: e*PyLong_SHIFT may overflow using int arithmetic,
Tim Peters78526162001-09-05 00:53:45 +0000246 so force use of double. */
Christian Heimes543cabc2008-01-25 14:54:23 +0000247 x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0);
Tim Peters78526162001-09-05 00:53:45 +0000248 return PyFloat_FromDouble(x);
249 }
250
251 /* Else let libm handle it by itself. */
Neal Norwitz45e230a2006-11-19 21:26:53 +0000252 return math_1(arg, func);
Tim Peters78526162001-09-05 00:53:45 +0000253}
254
255static PyObject *
256math_log(PyObject *self, PyObject *args)
257{
Raymond Hettinger866964c2002-12-14 19:51:34 +0000258 PyObject *arg;
259 PyObject *base = NULL;
260 PyObject *num, *den;
261 PyObject *ans;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000262
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000263 if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
Raymond Hettinger866964c2002-12-14 19:51:34 +0000264 return NULL;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000265
Neal Norwitz45e230a2006-11-19 21:26:53 +0000266 num = loghelper(arg, log, "log");
267 if (num == NULL || base == NULL)
268 return num;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000269
Neal Norwitz45e230a2006-11-19 21:26:53 +0000270 den = loghelper(base, log, "log");
Raymond Hettinger866964c2002-12-14 19:51:34 +0000271 if (den == NULL) {
272 Py_DECREF(num);
273 return NULL;
274 }
275
276 ans = PyNumber_Divide(num, den);
277 Py_DECREF(num);
278 Py_DECREF(den);
279 return ans;
Tim Peters78526162001-09-05 00:53:45 +0000280}
281
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000282PyDoc_STRVAR(math_log_doc,
Raymond Hettinger866964c2002-12-14 19:51:34 +0000283"log(x[, base]) -> the logarithm of x to the given base.\n\
284If the base not specified, returns the natural logarithm (base e) of x.");
Tim Peters78526162001-09-05 00:53:45 +0000285
286static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000287math_log10(PyObject *self, PyObject *arg)
Tim Peters78526162001-09-05 00:53:45 +0000288{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000289 return loghelper(arg, log10, "log10");
Tim Peters78526162001-09-05 00:53:45 +0000290}
291
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000292PyDoc_STRVAR(math_log10_doc,
293"log10(x) -> the base 10 logarithm of x.");
Tim Peters78526162001-09-05 00:53:45 +0000294
Christian Heimese2ca4242008-01-03 20:23:15 +0000295static const double degToRad = Py_MATH_PI / 180.0;
296static const double radToDeg = 180.0 / Py_MATH_PI;
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000297
298static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000299math_degrees(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000300{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000301 double x = PyFloat_AsDouble(arg);
302 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000303 return NULL;
Christian Heimese2ca4242008-01-03 20:23:15 +0000304 return PyFloat_FromDouble(x * radToDeg);
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000305}
306
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000307PyDoc_STRVAR(math_degrees_doc,
308"degrees(x) -> converts angle x from radians to degrees");
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000309
310static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000311math_radians(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000312{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000313 double x = PyFloat_AsDouble(arg);
314 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000315 return NULL;
316 return PyFloat_FromDouble(x * degToRad);
317}
318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000319PyDoc_STRVAR(math_radians_doc,
320"radians(x) -> converts angle x from degrees to radians");
Tim Peters78526162001-09-05 00:53:45 +0000321
Christian Heimese2ca4242008-01-03 20:23:15 +0000322static PyObject *
323math_isnan(PyObject *self, PyObject *arg)
324{
325 double x = PyFloat_AsDouble(arg);
326 if (x == -1.0 && PyErr_Occurred())
327 return NULL;
328 return PyBool_FromLong((long)Py_IS_NAN(x));
329}
330
331PyDoc_STRVAR(math_isnan_doc,
332"isnan(x) -> bool\n\
333Checks if float x is not a number (NaN)");
334
335static PyObject *
336math_isinf(PyObject *self, PyObject *arg)
337{
338 double x = PyFloat_AsDouble(arg);
339 if (x == -1.0 && PyErr_Occurred())
340 return NULL;
341 return PyBool_FromLong((long)Py_IS_INFINITY(x));
342}
343
344PyDoc_STRVAR(math_isinf_doc,
345"isinf(x) -> bool\n\
346Checks if float x is infinite (positive or negative)");
347
348
Barry Warsaw8b43b191996-12-09 22:32:36 +0000349static PyMethodDef math_methods[] = {
Neal Norwitz45e230a2006-11-19 21:26:53 +0000350 {"acos", math_acos, METH_O, math_acos_doc},
351 {"asin", math_asin, METH_O, math_asin_doc},
352 {"atan", math_atan, METH_O, math_atan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000353 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000354 {"ceil", math_ceil, METH_O, math_ceil_doc},
Christian Heimes66ecda42008-01-04 01:48:50 +0000355#ifdef HAVE_COPYSIGN
Christian Heimeseebb79c2008-01-03 22:32:26 +0000356 {"copysign", math_copysign, METH_VARARGS, math_copysign_doc},
357#endif
Neal Norwitz45e230a2006-11-19 21:26:53 +0000358 {"cos", math_cos, METH_O, math_cos_doc},
359 {"cosh", math_cosh, METH_O, math_cosh_doc},
360 {"degrees", math_degrees, METH_O, math_degrees_doc},
361 {"exp", math_exp, METH_O, math_exp_doc},
362 {"fabs", math_fabs, METH_O, math_fabs_doc},
363 {"floor", math_floor, METH_O, math_floor_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000364 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000365 {"frexp", math_frexp, METH_O, math_frexp_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000366 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
Christian Heimese2ca4242008-01-03 20:23:15 +0000367 {"isinf", math_isinf, METH_O, math_isinf_doc},
368 {"isnan", math_isnan, METH_O, math_isnan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000369 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
370 {"log", math_log, METH_VARARGS, math_log_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000371 {"log10", math_log10, METH_O, math_log10_doc},
372 {"modf", math_modf, METH_O, math_modf_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000373 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000374 {"radians", math_radians, METH_O, math_radians_doc},
375 {"sin", math_sin, METH_O, math_sin_doc},
376 {"sinh", math_sinh, METH_O, math_sinh_doc},
377 {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
378 {"tan", math_tan, METH_O, math_tan_doc},
379 {"tanh", math_tanh, METH_O, math_tanh_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380 {NULL, NULL} /* sentinel */
381};
382
Guido van Rossumc6e22901998-12-04 19:26:43 +0000383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000384PyDoc_STRVAR(module_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000385"This module is always available. It provides access to the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000386"mathematical functions defined by the C standard.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000387
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000388PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000389initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000391 PyObject *m, *d, *v;
Tim Petersfe71f812001-08-07 22:10:00 +0000392
Guido van Rossumc6e22901998-12-04 19:26:43 +0000393 m = Py_InitModule3("math", math_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000394 if (m == NULL)
395 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000396 d = PyModule_GetDict(m);
Neal Norwitz45e230a2006-11-19 21:26:53 +0000397 if (d == NULL)
398 goto finally;
Barry Warsawfc93f751996-12-17 00:47:03 +0000399
Christian Heimese2ca4242008-01-03 20:23:15 +0000400 if (!(v = PyFloat_FromDouble(Py_MATH_PI)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000401 goto finally;
402 if (PyDict_SetItemString(d, "pi", v) < 0)
403 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000404 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000405
Christian Heimese2ca4242008-01-03 20:23:15 +0000406 if (!(v = PyFloat_FromDouble(Py_MATH_E)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000407 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000408 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000409 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000410 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000411
412 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000413 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414}