blob: f50a4bb5daf6ed174311eb2d9f6369b165437fba [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
Christian Heimes969fe572008-01-25 11:23:10 +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 *
Jeffrey Yasskinc2155832008-01-05 20:03:11 +000056math_1_to_whatever(PyObject *arg, double (*func) (double),
57 PyObject *(*from_double_func) (double))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058{
Thomas Wouters89f507f2006-12-13 04:49:30 +000059 double x = PyFloat_AsDouble(arg);
60 if (x == -1.0 && PyErr_Occurred())
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061 return NULL;
62 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000063 PyFPE_START_PROTECT("in math_1", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +000065 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +000066 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +000067 if (errno && is_error(x))
68 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069 else
Jeffrey Yasskinc2155832008-01-05 20:03:11 +000070 return (*from_double_func)(x);
71}
72
73static PyObject *
74math_1(PyObject *arg, double (*func) (double))
75{
76 return math_1_to_whatever(arg, func, PyFloat_FromDouble);
77}
78
79static PyObject *
80math_1_to_int(PyObject *arg, double (*func) (double))
81{
82 return math_1_to_whatever(arg, func, PyLong_FromDouble);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083}
84
Barry Warsaw8b43b191996-12-09 22:32:36 +000085static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +000086math_2(PyObject *args, double (*func) (double, double), char *funcname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087{
Thomas Wouters89f507f2006-12-13 04:49:30 +000088 PyObject *ox, *oy;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089 double x, y;
Thomas Wouters89f507f2006-12-13 04:49:30 +000090 if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy))
91 return NULL;
92 x = PyFloat_AsDouble(ox);
93 y = PyFloat_AsDouble(oy);
94 if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095 return NULL;
96 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000097 PyFPE_START_PROTECT("in math_2", return 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098 x = (*func)(x, y);
Guido van Rossum45b83911997-03-14 04:32:50 +000099 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000100 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000101 if (errno && is_error(x))
102 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000104 return PyFloat_FromDouble(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105}
106
Fred Drake40c48682000-07-03 18:11:56 +0000107#define FUNC1(funcname, func, docstring) \
108 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
Thomas Wouters89f507f2006-12-13 04:49:30 +0000109 return math_1(args, func); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000110 }\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000111 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112
Fred Drake40c48682000-07-03 18:11:56 +0000113#define FUNC2(funcname, func, docstring) \
114 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 return math_2(args, func, #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000116 }\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000117 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118
Fred Drake40c48682000-07-03 18:11:56 +0000119FUNC1(acos, acos,
Tim Petersfe71f812001-08-07 22:10:00 +0000120 "acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000121FUNC1(asin, asin,
Tim Petersfe71f812001-08-07 22:10:00 +0000122 "asin(x)\n\nReturn the arc sine (measured in radians) of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000123FUNC1(atan, atan,
Tim Petersfe71f812001-08-07 22:10:00 +0000124 "atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
Martin v. Löwis387c5472001-09-06 08:16:17 +0000125FUNC2(atan2, atan2,
Tim Petersfe71f812001-08-07 22:10:00 +0000126 "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
127 "Unlike atan(y/x), the signs of both x and y are considered.")
Guido van Rossum13e05de2007-08-23 22:56:55 +0000128
129static PyObject * math_ceil(PyObject *self, PyObject *number) {
130 static PyObject *ceil_str = NULL;
131 PyObject *method;
132
133 if (ceil_str == NULL) {
Christian Heimesfe82e772008-01-28 02:38:20 +0000134 ceil_str = PyUnicode_InternFromString("__ceil__");
Guido van Rossum13e05de2007-08-23 22:56:55 +0000135 if (ceil_str == NULL)
136 return NULL;
137 }
138
Christian Heimes90aa7642007-12-19 02:45:37 +0000139 method = _PyType_Lookup(Py_TYPE(number), ceil_str);
Guido van Rossum13e05de2007-08-23 22:56:55 +0000140 if (method == NULL)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000141 return math_1_to_int(number, ceil);
Guido van Rossum13e05de2007-08-23 22:56:55 +0000142 else
143 return PyObject_CallFunction(method, "O", number);
144}
145
146PyDoc_STRVAR(math_ceil_doc,
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000147 "ceil(x)\n\nReturn the ceiling of x as an int.\n"
Guido van Rossum13e05de2007-08-23 22:56:55 +0000148 "This is the smallest integral value >= x.");
149
Fred Drake40c48682000-07-03 18:11:56 +0000150FUNC1(cos, cos,
Tim Petersfe71f812001-08-07 22:10:00 +0000151 "cos(x)\n\nReturn the cosine of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000152FUNC1(cosh, cosh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000153 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
Christian Heimesa34706f2008-01-04 03:06:10 +0000154
Christian Heimes072c0f12008-01-03 23:01:04 +0000155#ifdef MS_WINDOWS
Christian Heimesa34706f2008-01-04 03:06:10 +0000156# define copysign _copysign
157# define HAVE_COPYSIGN 1
158#endif
159#ifdef HAVE_COPYSIGN
Christian Heimes072c0f12008-01-03 23:01:04 +0000160FUNC2(copysign, copysign,
Christian Heimesa34706f2008-01-04 03:06:10 +0000161 "copysign(x,y)\n\nReturn x with the sign of y.");
Christian Heimes072c0f12008-01-03 23:01:04 +0000162#endif
Christian Heimesa34706f2008-01-04 03:06:10 +0000163
Fred Drake40c48682000-07-03 18:11:56 +0000164FUNC1(exp, exp,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000165 "exp(x)\n\nReturn e raised to the power of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000166FUNC1(fabs, fabs,
Tim Petersfe71f812001-08-07 22:10:00 +0000167 "fabs(x)\n\nReturn the absolute value of the float x.")
Guido van Rossum13e05de2007-08-23 22:56:55 +0000168
169static PyObject * math_floor(PyObject *self, PyObject *number) {
170 static PyObject *floor_str = NULL;
171 PyObject *method;
172
173 if (floor_str == NULL) {
Christian Heimesfe82e772008-01-28 02:38:20 +0000174 floor_str = PyUnicode_InternFromString("__floor__");
Guido van Rossum13e05de2007-08-23 22:56:55 +0000175 if (floor_str == NULL)
176 return NULL;
177 }
178
Christian Heimes90aa7642007-12-19 02:45:37 +0000179 method = _PyType_Lookup(Py_TYPE(number), floor_str);
Guido van Rossum13e05de2007-08-23 22:56:55 +0000180 if (method == NULL)
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000181 return math_1_to_int(number, floor);
Guido van Rossum13e05de2007-08-23 22:56:55 +0000182 else
183 return PyObject_CallFunction(method, "O", number);
184}
185
186PyDoc_STRVAR(math_floor_doc,
Jeffrey Yasskinc2155832008-01-05 20:03:11 +0000187 "floor(x)\n\nReturn the floor of x as an int.\n"
Guido van Rossum13e05de2007-08-23 22:56:55 +0000188 "This is the largest integral value <= x.");
189
Peter Schneider-Kamp07c19222001-06-10 16:45:08 +0000190FUNC2(fmod, fmod,
Tim Peters78fc0b52000-09-16 03:54:24 +0000191 "fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
192 " x % y may differ.")
Fred Drake40c48682000-07-03 18:11:56 +0000193FUNC2(hypot, hypot,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000194 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
Fred Drake40c48682000-07-03 18:11:56 +0000195FUNC2(pow, pow,
Tim Petersfe71f812001-08-07 22:10:00 +0000196 "pow(x,y)\n\nReturn x**y (x to the power of y).")
Fred Drake40c48682000-07-03 18:11:56 +0000197FUNC1(sin, sin,
Tim Petersfe71f812001-08-07 22:10:00 +0000198 "sin(x)\n\nReturn the sine of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000199FUNC1(sinh, sinh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000200 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000201FUNC1(sqrt, sqrt,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000202 "sqrt(x)\n\nReturn the square root of x.")
Fred Drake40c48682000-07-03 18:11:56 +0000203FUNC1(tan, tan,
Tim Petersfe71f812001-08-07 22:10:00 +0000204 "tan(x)\n\nReturn the tangent of x (measured in radians).")
Fred Drake40c48682000-07-03 18:11:56 +0000205FUNC1(tanh, tanh,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000206 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207
Barry Warsaw8b43b191996-12-09 22:32:36 +0000208static PyObject *
Christian Heimes400adb02008-02-01 08:12:03 +0000209math_trunc(PyObject *self, PyObject *number)
210{
211 static PyObject *trunc_str = NULL;
212 PyObject *trunc;
213
214 if (Py_TYPE(number)->tp_dict == NULL) {
215 if (PyType_Ready(Py_TYPE(number)) < 0)
216 return NULL;
217 }
218
219 if (trunc_str == NULL) {
220 trunc_str = PyUnicode_InternFromString("__trunc__");
221 if (trunc_str == NULL)
222 return NULL;
223 }
224
225 trunc = _PyType_Lookup(Py_TYPE(number), trunc_str);
226 if (trunc == NULL) {
227 PyErr_Format(PyExc_TypeError,
228 "type %.100s doesn't define __trunc__ method",
229 Py_TYPE(number)->tp_name);
230 return NULL;
231 }
232 return PyObject_CallFunctionObjArgs(trunc, number, NULL);
233}
234
235PyDoc_STRVAR(math_trunc_doc,
236"trunc(x:Real) -> Integral\n"
237"\n"
238"Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic"
239"method.");
240
241static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +0000242math_frexp(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000243{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000244 int i;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000245 double x = PyFloat_AsDouble(arg);
246 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000247 return NULL;
248 errno = 0;
249 x = frexp(x, &i);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000250 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000251 if (errno && is_error(x))
252 return NULL;
253 else
254 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000255}
256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257PyDoc_STRVAR(math_frexp_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000258"frexp(x)\n"
259"\n"
260"Return the mantissa and exponent of x, as pair (m, e).\n"
261"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 +0000262"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 +0000263
Barry Warsaw8b43b191996-12-09 22:32:36 +0000264static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000265math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000266{
Guido van Rossumc5545052000-05-08 14:29:38 +0000267 double x;
268 int exp;
Fred Drake40c48682000-07-03 18:11:56 +0000269 if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000270 return NULL;
271 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000272 PyFPE_START_PROTECT("ldexp", return 0)
Guido van Rossumc5545052000-05-08 14:29:38 +0000273 x = ldexp(x, exp);
Guido van Rossum45b83911997-03-14 04:32:50 +0000274 PyFPE_END_PROTECT(x)
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000275 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000276 if (errno && is_error(x))
277 return NULL;
Guido van Rossumd18ad581991-10-24 14:57:21 +0000278 else
Barry Warsaw8b43b191996-12-09 22:32:36 +0000279 return PyFloat_FromDouble(x);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000280}
281
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000282PyDoc_STRVAR(math_ldexp_doc,
283"ldexp(x, i) -> x * (2**i)");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000284
Barry Warsaw8b43b191996-12-09 22:32:36 +0000285static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +0000286math_modf(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000287{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000288 double y, x = PyFloat_AsDouble(arg);
289 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000290 return NULL;
291 errno = 0;
292 x = modf(x, &y);
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000293 Py_SET_ERRNO_ON_MATH_ERROR(x);
Tim Peters1d120612000-10-12 06:10:25 +0000294 if (errno && is_error(x))
295 return NULL;
296 else
297 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000298}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000300PyDoc_STRVAR(math_modf_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000301"modf(x)\n"
302"\n"
303"Return the fractional and integer parts of x. Both results carry the sign\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000304"of x. The integer part is returned as a real.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000305
Tim Peters78526162001-09-05 00:53:45 +0000306/* A decent logarithm is easy to compute even for huge longs, but libm can't
307 do that by itself -- loghelper can. func is log or log10, and name is
308 "log" or "log10". Note that overflow isn't possible: a long can contain
309 no more than INT_MAX * SHIFT bits, so has value certainly less than
310 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
311 small enough to fit in an IEEE single. log and log10 are even smaller.
312*/
313
314static PyObject*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000315loghelper(PyObject* arg, double (*func)(double), char *funcname)
Tim Peters78526162001-09-05 00:53:45 +0000316{
Tim Peters78526162001-09-05 00:53:45 +0000317 /* If it is long, do it ourselves. */
318 if (PyLong_Check(arg)) {
319 double x;
320 int e;
321 x = _PyLong_AsScaledDouble(arg, &e);
322 if (x <= 0.0) {
323 PyErr_SetString(PyExc_ValueError,
324 "math domain error");
325 return NULL;
326 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000327 /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~=
328 log(x) + log(2) * e * PyLong_SHIFT.
329 CAUTION: e*PyLong_SHIFT may overflow using int arithmetic,
Tim Peters78526162001-09-05 00:53:45 +0000330 so force use of double. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000331 x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0);
Tim Peters78526162001-09-05 00:53:45 +0000332 return PyFloat_FromDouble(x);
333 }
334
335 /* Else let libm handle it by itself. */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000336 return math_1(arg, func);
Tim Peters78526162001-09-05 00:53:45 +0000337}
338
339static PyObject *
340math_log(PyObject *self, PyObject *args)
341{
Raymond Hettinger866964c2002-12-14 19:51:34 +0000342 PyObject *arg;
343 PyObject *base = NULL;
344 PyObject *num, *den;
345 PyObject *ans;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000346
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000347 if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
Raymond Hettinger866964c2002-12-14 19:51:34 +0000348 return NULL;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000349
Thomas Wouters89f507f2006-12-13 04:49:30 +0000350 num = loghelper(arg, log, "log");
351 if (num == NULL || base == NULL)
352 return num;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000353
Thomas Wouters89f507f2006-12-13 04:49:30 +0000354 den = loghelper(base, log, "log");
Raymond Hettinger866964c2002-12-14 19:51:34 +0000355 if (den == NULL) {
356 Py_DECREF(num);
357 return NULL;
358 }
359
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000360 ans = PyNumber_TrueDivide(num, den);
Raymond Hettinger866964c2002-12-14 19:51:34 +0000361 Py_DECREF(num);
362 Py_DECREF(den);
363 return ans;
Tim Peters78526162001-09-05 00:53:45 +0000364}
365
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000366PyDoc_STRVAR(math_log_doc,
Raymond Hettinger866964c2002-12-14 19:51:34 +0000367"log(x[, base]) -> the logarithm of x to the given base.\n\
368If the base not specified, returns the natural logarithm (base e) of x.");
Tim Peters78526162001-09-05 00:53:45 +0000369
370static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +0000371math_log10(PyObject *self, PyObject *arg)
Tim Peters78526162001-09-05 00:53:45 +0000372{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000373 return loghelper(arg, log10, "log10");
Tim Peters78526162001-09-05 00:53:45 +0000374}
375
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000376PyDoc_STRVAR(math_log10_doc,
377"log10(x) -> the base 10 logarithm of x.");
Tim Peters78526162001-09-05 00:53:45 +0000378
Christian Heimes072c0f12008-01-03 23:01:04 +0000379static const double degToRad = Py_MATH_PI / 180.0;
380static const double radToDeg = 180.0 / Py_MATH_PI;
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000381
382static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +0000383math_degrees(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000384{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000385 double x = PyFloat_AsDouble(arg);
386 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000387 return NULL;
Christian Heimes072c0f12008-01-03 23:01:04 +0000388 return PyFloat_FromDouble(x * radToDeg);
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000389}
390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000391PyDoc_STRVAR(math_degrees_doc,
392"degrees(x) -> converts angle x from radians to degrees");
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000393
394static PyObject *
Thomas Wouters89f507f2006-12-13 04:49:30 +0000395math_radians(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000396{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000397 double x = PyFloat_AsDouble(arg);
398 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000399 return NULL;
400 return PyFloat_FromDouble(x * degToRad);
401}
402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000403PyDoc_STRVAR(math_radians_doc,
404"radians(x) -> converts angle x from degrees to radians");
Tim Peters78526162001-09-05 00:53:45 +0000405
Christian Heimes072c0f12008-01-03 23:01:04 +0000406static PyObject *
407math_isnan(PyObject *self, PyObject *arg)
408{
409 double x = PyFloat_AsDouble(arg);
410 if (x == -1.0 && PyErr_Occurred())
411 return NULL;
412 return PyBool_FromLong((long)Py_IS_NAN(x));
413}
414
415PyDoc_STRVAR(math_isnan_doc,
416"isnan(x) -> bool\n\
417Checks if float x is not a number (NaN)");
418
419static PyObject *
420math_isinf(PyObject *self, PyObject *arg)
421{
422 double x = PyFloat_AsDouble(arg);
423 if (x == -1.0 && PyErr_Occurred())
424 return NULL;
425 return PyBool_FromLong((long)Py_IS_INFINITY(x));
426}
427
428PyDoc_STRVAR(math_isinf_doc,
429"isinf(x) -> bool\n\
430Checks if float x is infinite (positive or negative)");
431
432
Barry Warsaw8b43b191996-12-09 22:32:36 +0000433static PyMethodDef math_methods[] = {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000434 {"acos", math_acos, METH_O, math_acos_doc},
435 {"asin", math_asin, METH_O, math_asin_doc},
436 {"atan", math_atan, METH_O, math_atan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000437 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +0000438 {"ceil", math_ceil, METH_O, math_ceil_doc},
Christian Heimesa34706f2008-01-04 03:06:10 +0000439#ifdef HAVE_COPYSIGN
Christian Heimes072c0f12008-01-03 23:01:04 +0000440 {"copysign", math_copysign, METH_VARARGS, math_copysign_doc},
441#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +0000442 {"cos", math_cos, METH_O, math_cos_doc},
443 {"cosh", math_cosh, METH_O, math_cosh_doc},
444 {"degrees", math_degrees, METH_O, math_degrees_doc},
445 {"exp", math_exp, METH_O, math_exp_doc},
446 {"fabs", math_fabs, METH_O, math_fabs_doc},
447 {"floor", math_floor, METH_O, math_floor_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000448 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +0000449 {"frexp", math_frexp, METH_O, math_frexp_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000450 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
Christian Heimes072c0f12008-01-03 23:01:04 +0000451 {"isinf", math_isinf, METH_O, math_isinf_doc},
452 {"isnan", math_isnan, METH_O, math_isnan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000453 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
454 {"log", math_log, METH_VARARGS, math_log_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +0000455 {"log10", math_log10, METH_O, math_log10_doc},
456 {"modf", math_modf, METH_O, math_modf_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000457 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +0000458 {"radians", math_radians, METH_O, math_radians_doc},
459 {"sin", math_sin, METH_O, math_sin_doc},
460 {"sinh", math_sinh, METH_O, math_sinh_doc},
461 {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
462 {"tan", math_tan, METH_O, math_tan_doc},
463 {"tanh", math_tanh, METH_O, math_tanh_doc},
Christian Heimes400adb02008-02-01 08:12:03 +0000464 {"trunc", math_trunc, METH_O, math_trunc_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465 {NULL, NULL} /* sentinel */
466};
467
Guido van Rossumc6e22901998-12-04 19:26:43 +0000468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000469PyDoc_STRVAR(module_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000470"This module is always available. It provides access to the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000471"mathematical functions defined by the C standard.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000472
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000473PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000474initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000475{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000476 PyObject *m, *d, *v;
Tim Petersfe71f812001-08-07 22:10:00 +0000477
Guido van Rossumc6e22901998-12-04 19:26:43 +0000478 m = Py_InitModule3("math", math_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000479 if (m == NULL)
480 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000481 d = PyModule_GetDict(m);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000482 if (d == NULL)
483 goto finally;
Barry Warsawfc93f751996-12-17 00:47:03 +0000484
Christian Heimes072c0f12008-01-03 23:01:04 +0000485 if (!(v = PyFloat_FromDouble(Py_MATH_PI)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000486 goto finally;
487 if (PyDict_SetItemString(d, "pi", v) < 0)
488 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000489 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000490
Christian Heimes072c0f12008-01-03 23:01:04 +0000491 if (!(v = PyFloat_FromDouble(Py_MATH_E)))
Barry Warsawfc93f751996-12-17 00:47:03 +0000492 goto finally;
Guido van Rossum4c4cbf31996-12-18 14:12:22 +0000493 if (PyDict_SetItemString(d, "e", v) < 0)
Barry Warsawfc93f751996-12-17 00:47:03 +0000494 goto finally;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000495 Py_DECREF(v);
Barry Warsawfc93f751996-12-17 00:47:03 +0000496
497 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000498 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000499}