blob: 63a2cca044b85505bcd21e464f867f6b0a23cbef [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{
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000160 return PyObject_CallMethod(number, "__trunc__", NULL);
161}
162
163PyDoc_STRVAR(math_trunc_doc,
164"trunc(x:Real) -> Integral\n"
165"\n"
Raymond Hettingerfe424f72008-02-02 05:24:44 +0000166"Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.");
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000167
168static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000169math_frexp(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000170{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000171 int i;
Neal Norwitz45e230a2006-11-19 21:26:53 +0000172 double x = PyFloat_AsDouble(arg);
173 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000174 return NULL;
175 errno = 0;
176 x = frexp(x, &i);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000177 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000178 if (errno && is_error(x))
179 return NULL;
180 else
181 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000182}
183
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000184PyDoc_STRVAR(math_frexp_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000185"frexp(x)\n"
186"\n"
187"Return the mantissa and exponent of x, as pair (m, e).\n"
188"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 +0000189"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 +0000190
Barry Warsaw8b43b191996-12-09 22:32:36 +0000191static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000192math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000193{
Guido van Rossumc5545052000-05-08 14:29:38 +0000194 double x;
195 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000196 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000197 return NULL;
198 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000199 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000200 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000201 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000202 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000203 if (errno && is_error(x))
204 return NULL;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000205 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000206 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000207}
208
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000209PyDoc_STRVAR(math_ldexp_doc,
210"ldexp(x, i) -> x * (2**i)");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000211
Barry Warsaw8b43b191996-12-09 22:32:36 +0000212static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000213math_modf(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000214{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000215 double y, x = PyFloat_AsDouble(arg);
216 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000217 return NULL;
218 errno = 0;
219 x = modf(x, &y);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000220 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000221 if (errno && is_error(x))
222 return NULL;
223 else
224 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000225}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000227PyDoc_STRVAR(math_modf_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000228"modf(x)\n"
229"\n"
230"Return the fractional and integer parts of x. Both results carry the sign\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000231"of x. The integer part is returned as a real.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000232
Tim Peters78526162001-09-05 00:53:45 +0000233/* A decent logarithm is easy to compute even for huge longs, but libm can't
234 do that by itself -- loghelper can. func is log or log10, and name is
235 "log" or "log10". Note that overflow isn't possible: a long can contain
236 no more than INT_MAX * SHIFT bits, so has value certainly less than
237 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
238 small enough to fit in an IEEE single. log and log10 are even smaller.
239*/
240
241static PyObject*
Neal Norwitz45e230a2006-11-19 21:26:53 +0000242loghelper(PyObject* arg, double (*func)(double), char *funcname)
Tim Peters78526162001-09-05 00:53:45 +0000243{
Tim Peters78526162001-09-05 00:53:45 +0000244 /* If it is long, do it ourselves. */
245 if (PyLong_Check(arg)) {
246 double x;
247 int e;
248 x = _PyLong_AsScaledDouble(arg, &e);
249 if (x <= 0.0) {
250 PyErr_SetString(PyExc_ValueError,
251 "math domain error");
252 return NULL;
253 }
Christian Heimes543cabc2008-01-25 14:54:23 +0000254 /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~=
255 log(x) + log(2) * e * PyLong_SHIFT.
256 CAUTION: e*PyLong_SHIFT may overflow using int arithmetic,
Tim Peters78526162001-09-05 00:53:45 +0000257 so force use of double. */
Christian Heimes543cabc2008-01-25 14:54:23 +0000258 x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0);
Tim Peters78526162001-09-05 00:53:45 +0000259 return PyFloat_FromDouble(x);
260 }
261
262 /* Else let libm handle it by itself. */
Neal Norwitz45e230a2006-11-19 21:26:53 +0000263 return math_1(arg, func);
Tim Peters78526162001-09-05 00:53:45 +0000264}
265
266static PyObject *
267math_log(PyObject *self, PyObject *args)
268{
Raymond Hettinger866964c2002-12-14 19:51:34 +0000269 PyObject *arg;
270 PyObject *base = NULL;
271 PyObject *num, *den;
272 PyObject *ans;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000273
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000274 if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
Raymond Hettinger866964c2002-12-14 19:51:34 +0000275 return NULL;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000276
Neal Norwitz45e230a2006-11-19 21:26:53 +0000277 num = loghelper(arg, log, "log");
278 if (num == NULL || base == NULL)
279 return num;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000280
Neal Norwitz45e230a2006-11-19 21:26:53 +0000281 den = loghelper(base, log, "log");
Raymond Hettinger866964c2002-12-14 19:51:34 +0000282 if (den == NULL) {
283 Py_DECREF(num);
284 return NULL;
285 }
286
287 ans = PyNumber_Divide(num, den);
288 Py_DECREF(num);
289 Py_DECREF(den);
290 return ans;
Tim Peters78526162001-09-05 00:53:45 +0000291}
292
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000293PyDoc_STRVAR(math_log_doc,
Raymond Hettinger866964c2002-12-14 19:51:34 +0000294"log(x[, base]) -> the logarithm of x to the given base.\n\
295If the base not specified, returns the natural logarithm (base e) of x.");
Tim Peters78526162001-09-05 00:53:45 +0000296
297static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000298math_log10(PyObject *self, PyObject *arg)
Tim Peters78526162001-09-05 00:53:45 +0000299{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000300 return loghelper(arg, log10, "log10");
Tim Peters78526162001-09-05 00:53:45 +0000301}
302
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000303PyDoc_STRVAR(math_log10_doc,
304"log10(x) -> the base 10 logarithm of x.");
Tim Peters78526162001-09-05 00:53:45 +0000305
Christian Heimese2ca4242008-01-03 20:23:15 +0000306static const double degToRad = Py_MATH_PI / 180.0;
307static const double radToDeg = 180.0 / Py_MATH_PI;
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000308
309static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000310math_degrees(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000311{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000312 double x = PyFloat_AsDouble(arg);
313 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000314 return NULL;
Christian Heimese2ca4242008-01-03 20:23:15 +0000315 return PyFloat_FromDouble(x * radToDeg);
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000316}
317
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000318PyDoc_STRVAR(math_degrees_doc,
319"degrees(x) -> converts angle x from radians to degrees");
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000320
321static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000322math_radians(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000323{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000324 double x = PyFloat_AsDouble(arg);
325 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000326 return NULL;
327 return PyFloat_FromDouble(x * degToRad);
328}
329
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000330PyDoc_STRVAR(math_radians_doc,
331"radians(x) -> converts angle x from degrees to radians");
Tim Peters78526162001-09-05 00:53:45 +0000332
Christian Heimese2ca4242008-01-03 20:23:15 +0000333static PyObject *
334math_isnan(PyObject *self, PyObject *arg)
335{
336 double x = PyFloat_AsDouble(arg);
337 if (x == -1.0 && PyErr_Occurred())
338 return NULL;
339 return PyBool_FromLong((long)Py_IS_NAN(x));
340}
341
342PyDoc_STRVAR(math_isnan_doc,
343"isnan(x) -> bool\n\
344Checks if float x is not a number (NaN)");
345
346static PyObject *
347math_isinf(PyObject *self, PyObject *arg)
348{
349 double x = PyFloat_AsDouble(arg);
350 if (x == -1.0 && PyErr_Occurred())
351 return NULL;
352 return PyBool_FromLong((long)Py_IS_INFINITY(x));
353}
354
355PyDoc_STRVAR(math_isinf_doc,
356"isinf(x) -> bool\n\
357Checks if float x is infinite (positive or negative)");
358
359
Barry Warsaw8b43b191996-12-09 22:32:36 +0000360static PyMethodDef math_methods[] = {
Neal Norwitz45e230a2006-11-19 21:26:53 +0000361 {"acos", math_acos, METH_O, math_acos_doc},
362 {"asin", math_asin, METH_O, math_asin_doc},
363 {"atan", math_atan, METH_O, math_atan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000364 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000365 {"ceil", math_ceil, METH_O, math_ceil_doc},
Christian Heimes66ecda42008-01-04 01:48:50 +0000366#ifdef HAVE_COPYSIGN
Christian Heimeseebb79c2008-01-03 22:32:26 +0000367 {"copysign", math_copysign, METH_VARARGS, math_copysign_doc},
368#endif
Neal Norwitz45e230a2006-11-19 21:26:53 +0000369 {"cos", math_cos, METH_O, math_cos_doc},
370 {"cosh", math_cosh, METH_O, math_cosh_doc},
371 {"degrees", math_degrees, METH_O, math_degrees_doc},
372 {"exp", math_exp, METH_O, math_exp_doc},
373 {"fabs", math_fabs, METH_O, math_fabs_doc},
374 {"floor", math_floor, METH_O, math_floor_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000375 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000376 {"frexp", math_frexp, METH_O, math_frexp_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000377 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
Christian Heimese2ca4242008-01-03 20:23:15 +0000378 {"isinf", math_isinf, METH_O, math_isinf_doc},
379 {"isnan", math_isnan, METH_O, math_isnan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000380 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
381 {"log", math_log, METH_VARARGS, math_log_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000382 {"log10", math_log10, METH_O, math_log10_doc},
383 {"modf", math_modf, METH_O, math_modf_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000384 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000385 {"radians", math_radians, METH_O, math_radians_doc},
386 {"sin", math_sin, METH_O, math_sin_doc},
387 {"sinh", math_sinh, METH_O, math_sinh_doc},
388 {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
389 {"tan", math_tan, METH_O, math_tan_doc},
390 {"tanh", math_tanh, METH_O, math_tanh_doc},
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000391 {"trunc", math_trunc, METH_O, math_trunc_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392 {NULL, NULL} /* sentinel */
393};
394
Guido van Rossumc6e22901998-12-04 19:26:43 +0000395
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000396PyDoc_STRVAR(module_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000397"This module is always available. It provides access to the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000398"mathematical functions defined by the C standard.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000399
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000400PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000401initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000403 PyObject *m, *d, *v;
Tim Petersfe71f812001-08-07 22:10:00 +0000404
Guido van Rossumc6e22901998-12-04 19:26:43 +0000405 m = Py_InitModule3("math", math_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000406 if (m == NULL)
407 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000408 d = PyModule_GetDict(m);
Neal Norwitz45e230a2006-11-19 21:26:53 +0000409 if (d == NULL)
410 goto finally;
Barry Warsawfc93f751996-12-17 00:47:03 +0000411
Christian Heimese2ca4242008-01-03 20:23:15 +0000412 if (!(v = PyFloat_FromDouble(Py_MATH_PI)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000413 goto finally;
414 if (PyDict_SetItemString(d, "pi", v) < 0)
415 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000416 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000417
Christian Heimese2ca4242008-01-03 20:23:15 +0000418 if (!(v = PyFloat_FromDouble(Py_MATH_E)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000419 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000420 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000421 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000422 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000423
424 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000425 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426}