blob: e9f0a22ac0d9e7b3ebf4144a6df922dfcf074403 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Math module -- standard C math library functions, pi and e */
2
Christian Heimes6f341092008-04-18 23:13:07 +00003/* Here are some comments from Tim Peters, extracted from the
4 discussion attached to http://bugs.python.org/issue1640. They
5 describe the general aims of the math module with respect to
6 special values, IEEE-754 floating-point exceptions, and Python
7 exceptions.
8
9These are the "spirit of 754" rules:
10
111. If the mathematical result is a real number, but of magnitude too
12large to approximate by a machine float, overflow is signaled and the
13result is an infinity (with the appropriate sign).
14
152. If the mathematical result is a real number, but of magnitude too
16small to approximate by a machine float, underflow is signaled and the
17result is a zero (with the appropriate sign).
18
193. At a singularity (a value x such that the limit of f(y) as y
20approaches x exists and is an infinity), "divide by zero" is signaled
21and the result is an infinity (with the appropriate sign). This is
22complicated a little by that the left-side and right-side limits may
23not be the same; e.g., 1/x approaches +inf or -inf as x approaches 0
24from the positive or negative directions. In that specific case, the
25sign of the zero determines the result of 1/0.
26
274. At a point where a function has no defined result in the extended
28reals (i.e., the reals plus an infinity or two), invalid operation is
29signaled and a NaN is returned.
30
31And these are what Python has historically /tried/ to do (but not
32always successfully, as platform libm behavior varies a lot):
33
34For #1, raise OverflowError.
35
36For #2, return a zero (with the appropriate sign if that happens by
37accident ;-)).
38
39For #3 and #4, raise ValueError. It may have made sense to raise
40Python's ZeroDivisionError in #3, but historically that's only been
41raised for division by zero and mod by zero.
42
43*/
44
45/*
46 In general, on an IEEE-754 platform the aim is to follow the C99
47 standard, including Annex 'F', whenever possible. Where the
48 standard recommends raising the 'divide-by-zero' or 'invalid'
49 floating-point exceptions, Python should raise a ValueError. Where
50 the standard recommends raising 'overflow', Python should raise an
51 OverflowError. In all other circumstances a value should be
52 returned.
53 */
54
Barry Warsaw8b43b191996-12-09 22:32:36 +000055#include "Python.h"
Michael W. Hudson9ef852c2005-04-06 13:05:18 +000056#include "longintrepr.h" /* just for SHIFT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057
Neal Norwitz5f95a792008-01-25 08:04:16 +000058#ifdef _OSF_SOURCE
59/* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */
60extern double copysign(double, double);
61#endif
62
Tim Peters1d120612000-10-12 06:10:25 +000063/* Call is_error when errno != 0, and where x is the result libm
64 * returned. is_error will usually set up an exception and return
65 * true (1), but may return false (0) without setting up an exception.
66 */
67static int
68is_error(double x)
Guido van Rossum8832b621991-12-16 15:44:24 +000069{
Tim Peters1d120612000-10-12 06:10:25 +000070 int result = 1; /* presumption of guilt */
Tim Peters2bf405a2000-10-12 19:42:00 +000071 assert(errno); /* non-zero errno is a precondition for calling */
Guido van Rossum8832b621991-12-16 15:44:24 +000072 if (errno == EDOM)
Barry Warsaw8b43b191996-12-09 22:32:36 +000073 PyErr_SetString(PyExc_ValueError, "math domain error");
Tim Petersa40c7932001-09-05 22:36:56 +000074
Tim Peters1d120612000-10-12 06:10:25 +000075 else if (errno == ERANGE) {
76 /* ANSI C generally requires libm functions to set ERANGE
77 * on overflow, but also generally *allows* them to set
78 * ERANGE on underflow too. There's no consistency about
Tim Petersa40c7932001-09-05 22:36:56 +000079 * the latter across platforms.
80 * Alas, C99 never requires that errno be set.
81 * Here we suppress the underflow errors (libm functions
82 * should return a zero on underflow, and +- HUGE_VAL on
83 * overflow, so testing the result for zero suffices to
84 * distinguish the cases).
Tim Peters1d120612000-10-12 06:10:25 +000085 */
86 if (x)
Tim Petersfe71f812001-08-07 22:10:00 +000087 PyErr_SetString(PyExc_OverflowError,
Tim Peters1d120612000-10-12 06:10:25 +000088 "math range error");
89 else
90 result = 0;
91 }
Guido van Rossum8832b621991-12-16 15:44:24 +000092 else
Barry Warsaw8b43b191996-12-09 22:32:36 +000093 /* Unexpected math error */
94 PyErr_SetFromErrno(PyExc_ValueError);
Tim Peters1d120612000-10-12 06:10:25 +000095 return result;
Guido van Rossum8832b621991-12-16 15:44:24 +000096}
97
Christian Heimes6f341092008-04-18 23:13:07 +000098/*
Mark Dickinson92483cd2008-04-20 21:39:04 +000099 wrapper for atan2 that deals directly with special cases before
100 delegating to the platform libm for the remaining cases. This
101 is necessary to get consistent behaviour across platforms.
102 Windows, FreeBSD and alpha Tru64 are amongst platforms that don't
103 always follow C99.
104*/
105
106static double
107m_atan2(double y, double x)
108{
109 if (Py_IS_NAN(x) || Py_IS_NAN(y))
110 return Py_NAN;
111 if (Py_IS_INFINITY(y)) {
112 if (Py_IS_INFINITY(x)) {
113 if (copysign(1., x) == 1.)
114 /* atan2(+-inf, +inf) == +-pi/4 */
115 return copysign(0.25*Py_MATH_PI, y);
116 else
117 /* atan2(+-inf, -inf) == +-pi*3/4 */
118 return copysign(0.75*Py_MATH_PI, y);
119 }
120 /* atan2(+-inf, x) == +-pi/2 for finite x */
121 return copysign(0.5*Py_MATH_PI, y);
122 }
123 if (Py_IS_INFINITY(x) || y == 0.) {
124 if (copysign(1., x) == 1.)
125 /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
126 return copysign(0., y);
127 else
128 /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
129 return copysign(Py_MATH_PI, y);
130 }
131 return atan2(y, x);
132}
133
134/*
Christian Heimes6f341092008-04-18 23:13:07 +0000135 math_1 is used to wrap a libm function f that takes a double
136 arguments and returns a double.
137
138 The error reporting follows these rules, which are designed to do
139 the right thing on C89/C99 platforms and IEEE 754/non IEEE 754
140 platforms.
141
142 - a NaN result from non-NaN inputs causes ValueError to be raised
143 - an infinite result from finite inputs causes OverflowError to be
144 raised if can_overflow is 1, or raises ValueError if can_overflow
145 is 0.
146 - if the result is finite and errno == EDOM then ValueError is
147 raised
148 - if the result is finite and nonzero and errno == ERANGE then
149 OverflowError is raised
150
151 The last rule is used to catch overflow on platforms which follow
152 C89 but for which HUGE_VAL is not an infinity.
153
154 For the majority of one-argument functions these rules are enough
155 to ensure that Python's functions behave as specified in 'Annex F'
156 of the C99 standard, with the 'invalid' and 'divide-by-zero'
157 floating-point exceptions mapping to Python's ValueError and the
158 'overflow' floating-point exception mapping to OverflowError.
159 math_1 only works for functions that don't have singularities *and*
160 the possibility of overflow; fortunately, that covers everything we
161 care about right now.
162*/
163
Barry Warsaw8b43b191996-12-09 22:32:36 +0000164static PyObject *
Christian Heimes6f341092008-04-18 23:13:07 +0000165math_1(PyObject *arg, double (*func) (double), int can_overflow)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166{
Christian Heimes6f341092008-04-18 23:13:07 +0000167 double x, r;
168 x = PyFloat_AsDouble(arg);
Neal Norwitz45e230a2006-11-19 21:26:53 +0000169 if (x == -1.0 && PyErr_Occurred())
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170 return NULL;
171 errno = 0;
Christian Heimes6f341092008-04-18 23:13:07 +0000172 PyFPE_START_PROTECT("in math_1", return 0);
173 r = (*func)(x);
174 PyFPE_END_PROTECT(r);
175 if (Py_IS_NAN(r)) {
176 if (!Py_IS_NAN(x))
177 errno = EDOM;
178 else
179 errno = 0;
180 }
181 else if (Py_IS_INFINITY(r)) {
182 if (Py_IS_FINITE(x))
183 errno = can_overflow ? ERANGE : EDOM;
184 else
185 errno = 0;
186 }
187 if (errno && is_error(r))
Tim Peters1d120612000-10-12 06:10:25 +0000188 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189 else
Christian Heimes6f341092008-04-18 23:13:07 +0000190 return PyFloat_FromDouble(r);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191}
192
Christian Heimes6f341092008-04-18 23:13:07 +0000193/*
194 math_2 is used to wrap a libm function f that takes two double
195 arguments and returns a double.
196
197 The error reporting follows these rules, which are designed to do
198 the right thing on C89/C99 platforms and IEEE 754/non IEEE 754
199 platforms.
200
201 - a NaN result from non-NaN inputs causes ValueError to be raised
202 - an infinite result from finite inputs causes OverflowError to be
203 raised.
204 - if the result is finite and errno == EDOM then ValueError is
205 raised
206 - if the result is finite and nonzero and errno == ERANGE then
207 OverflowError is raised
208
209 The last rule is used to catch overflow on platforms which follow
210 C89 but for which HUGE_VAL is not an infinity.
211
212 For most two-argument functions (copysign, fmod, hypot, atan2)
213 these rules are enough to ensure that Python's functions behave as
214 specified in 'Annex F' of the C99 standard, with the 'invalid' and
215 'divide-by-zero' floating-point exceptions mapping to Python's
216 ValueError and the 'overflow' floating-point exception mapping to
217 OverflowError.
218*/
219
Barry Warsaw8b43b191996-12-09 22:32:36 +0000220static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000221math_2(PyObject *args, double (*func) (double, double), char *funcname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000223 PyObject *ox, *oy;
Christian Heimes6f341092008-04-18 23:13:07 +0000224 double x, y, r;
Neal Norwitz45e230a2006-11-19 21:26:53 +0000225 if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy))
226 return NULL;
227 x = PyFloat_AsDouble(ox);
228 y = PyFloat_AsDouble(oy);
229 if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230 return NULL;
231 errno = 0;
Christian Heimes6f341092008-04-18 23:13:07 +0000232 PyFPE_START_PROTECT("in math_2", return 0);
233 r = (*func)(x, y);
234 PyFPE_END_PROTECT(r);
235 if (Py_IS_NAN(r)) {
236 if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
237 errno = EDOM;
238 else
239 errno = 0;
240 }
241 else if (Py_IS_INFINITY(r)) {
242 if (Py_IS_FINITE(x) && Py_IS_FINITE(y))
243 errno = ERANGE;
244 else
245 errno = 0;
246 }
247 if (errno && is_error(r))
Tim Peters1d120612000-10-12 06:10:25 +0000248 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249 else
Christian Heimes6f341092008-04-18 23:13:07 +0000250 return PyFloat_FromDouble(r);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251}
252
Christian Heimes6f341092008-04-18 23:13:07 +0000253#define FUNC1(funcname, func, can_overflow, docstring) \
Fred Drake40c48682000-07-03 18:11:56 +0000254 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
Christian Heimes6f341092008-04-18 23:13:07 +0000255 return math_1(args, func, can_overflow); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000256 }\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258
Fred Drake40c48682000-07-03 18:11:56 +0000259#define FUNC2(funcname, func, docstring) \
260 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
Neal Norwitz45e230a2006-11-19 21:26:53 +0000261 return math_2(args, func, #funcname); \
Guido van Rossumc6e22901998-12-04 19:26:43 +0000262 }\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000263 PyDoc_STRVAR(math_##funcname##_doc, docstring);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264
Christian Heimes6f341092008-04-18 23:13:07 +0000265FUNC1(acos, acos, 0,
Tim Petersfe71f812001-08-07 22:10:00 +0000266 "acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
Christian Heimes6f341092008-04-18 23:13:07 +0000267FUNC1(acosh, acosh, 0,
268 "acosh(x)\n\nReturn the hyperbolic arc cosine (measured in radians) of x.")
269FUNC1(asin, asin, 0,
Tim Petersfe71f812001-08-07 22:10:00 +0000270 "asin(x)\n\nReturn the arc sine (measured in radians) of x.")
Christian Heimes6f341092008-04-18 23:13:07 +0000271FUNC1(asinh, asinh, 0,
272 "asinh(x)\n\nReturn the hyperbolic arc sine (measured in radians) of x.")
273FUNC1(atan, atan, 0,
Tim Petersfe71f812001-08-07 22:10:00 +0000274 "atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
Mark Dickinson92483cd2008-04-20 21:39:04 +0000275FUNC2(atan2, m_atan2,
Tim Petersfe71f812001-08-07 22:10:00 +0000276 "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
277 "Unlike atan(y/x), the signs of both x and y are considered.")
Christian Heimes6f341092008-04-18 23:13:07 +0000278FUNC1(atanh, atanh, 0,
279 "atanh(x)\n\nReturn the hyperbolic arc tangent (measured in radians) of x.")
280FUNC1(ceil, ceil, 0,
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000281 "ceil(x)\n\nReturn the ceiling of x as a float.\n"
282 "This is the smallest integral value >= x.")
Christian Heimeseebb79c2008-01-03 22:32:26 +0000283FUNC2(copysign, copysign,
Christian Heimes6f341092008-04-18 23:13:07 +0000284 "copysign(x,y)\n\nReturn x with the sign of y.")
285FUNC1(cos, cos, 0,
286 "cos(x)\n\nReturn the cosine of x (measured in radians).")
287FUNC1(cosh, cosh, 1,
288 "cosh(x)\n\nReturn the hyperbolic cosine of x.")
289FUNC1(exp, exp, 1,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000290 "exp(x)\n\nReturn e raised to the power of x.")
Christian Heimes6f341092008-04-18 23:13:07 +0000291FUNC1(fabs, fabs, 0,
Tim Petersfe71f812001-08-07 22:10:00 +0000292 "fabs(x)\n\nReturn the absolute value of the float x.")
Christian Heimes6f341092008-04-18 23:13:07 +0000293FUNC1(floor, floor, 0,
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +0000294 "floor(x)\n\nReturn the floor of x as a float.\n"
295 "This is the largest integral value <= x.")
Christian Heimes6f341092008-04-18 23:13:07 +0000296FUNC1(log1p, log1p, 1,
297 "log1p(x)\n\nReturn the natural logarithm of 1+x (base e).\n\
298 The result is computed in a way which is accurate for x near zero.")
299FUNC1(sin, sin, 0,
Tim Petersfe71f812001-08-07 22:10:00 +0000300 "sin(x)\n\nReturn the sine of x (measured in radians).")
Christian Heimes6f341092008-04-18 23:13:07 +0000301FUNC1(sinh, sinh, 1,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000302 "sinh(x)\n\nReturn the hyperbolic sine of x.")
Christian Heimes6f341092008-04-18 23:13:07 +0000303FUNC1(sqrt, sqrt, 0,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000304 "sqrt(x)\n\nReturn the square root of x.")
Christian Heimes6f341092008-04-18 23:13:07 +0000305FUNC1(tan, tan, 0,
Tim Petersfe71f812001-08-07 22:10:00 +0000306 "tan(x)\n\nReturn the tangent of x (measured in radians).")
Christian Heimes6f341092008-04-18 23:13:07 +0000307FUNC1(tanh, tanh, 0,
Guido van Rossumc6e22901998-12-04 19:26:43 +0000308 "tanh(x)\n\nReturn the hyperbolic tangent of x.")
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309
Mark Dickinson99dfe922008-05-23 01:35:30 +0000310/* Precision summation function as msum() by Raymond Hettinger in
311 <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090>,
312 enhanced with the exact partials sum and roundoff from Mark
313 Dickinson's post at <http://bugs.python.org/file10357/msum4.py>.
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000314 See those links for more details, proofs and other references.
Mark Dickinson99dfe922008-05-23 01:35:30 +0000315
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000316 Note 1: IEEE 754R floating point semantics are assumed,
317 but the current implementation does not re-establish special
318 value semantics across iterations (i.e. handling -Inf + Inf).
Mark Dickinson99dfe922008-05-23 01:35:30 +0000319
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000320 Note 2: No provision is made for intermediate overflow handling;
321 therefore, sum([1e+308, 1e-308, 1e+308]) returns result 1e+308 while
322 sum([1e+308, 1e+308, 1e-308]) raises an OverflowError due to the
323 overflow of the first partial sum.
Mark Dickinson99dfe922008-05-23 01:35:30 +0000324
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000325 Note 3: Aggressively optimizing compilers can potentially eliminate the
326 residual values needed for accurate summation. For instance, the statements
327 "hi = x + y; lo = y - (hi - x);" could be mis-transformed to
328 "hi = x + y; lo = 0.0;" which defeats the computation of residuals.
Mark Dickinson99dfe922008-05-23 01:35:30 +0000329
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000330 Note 4: A similar implementation is in Modules/cmathmodule.c.
331 Be sure to update both when making changes.
Mark Dickinson99dfe922008-05-23 01:35:30 +0000332
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000333 Note 5: The signature of math.sum() differs from __builtin__.sum()
334 because the start argument doesn't make sense in the context of
335 accurate summation. Since the partials table is collapsed before
336 returning a result, sum(seq2, start=sum(seq1)) may not equal the
337 accurate result returned by sum(itertools.chain(seq1, seq2)).
Mark Dickinson99dfe922008-05-23 01:35:30 +0000338*/
339
340#define NUM_PARTIALS 32 /* initial partials array size, on stack */
341
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000342/* Extend the partials array p[] by doubling its size. */
343static int /* non-zero on error */
Mark Dickinson99dfe922008-05-23 01:35:30 +0000344_sum_realloc(double **p_ptr, Py_ssize_t n,
345 double *ps, Py_ssize_t *m_ptr)
346{
347 void *v = NULL;
348 Py_ssize_t m = *m_ptr;
349
350 m += m; /* double */
351 if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) {
352 double *p = *p_ptr;
353 if (p == ps) {
354 v = PyMem_Malloc(sizeof(double) * m);
355 if (v != NULL)
356 memcpy(v, ps, sizeof(double) * n);
357 }
358 else
359 v = PyMem_Realloc(p, sizeof(double) * m);
360 }
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000361 if (v == NULL) { /* size overflow or no memory */
Mark Dickinson99dfe922008-05-23 01:35:30 +0000362 PyErr_SetString(PyExc_MemoryError, "math sum partials");
363 return 1;
364 }
365 *p_ptr = (double*) v;
366 *m_ptr = m;
367 return 0;
368}
369
370/* Full precision summation of a sequence of floats.
371
372 def msum(iterable):
373 partials = [] # sorted, non-overlapping partial sums
374 for x in iterable:
375 i = 0
376 for y in partials:
377 if abs(x) < abs(y):
378 x, y = y, x
379 hi = x + y
380 lo = y - (hi - x)
381 if lo:
382 partials[i] = lo
383 i += 1
384 x = hi
385 partials[i:] = [x]
386 return sum_exact(partials)
387
388 Rounded x+y stored in hi with the roundoff stored in lo. Together hi+lo
389 are exactly equal to x+y. The inner loop applies hi/lo summation to each
390 partial so that the list of partial sums remains exact.
391
392 Sum_exact() adds the partial sums exactly and correctly rounds the final
393 result (using the round-half-to-even rule). The items in partials remain
394 non-zero, non-special, non-overlapping and strictly increasing in
395 magnitude, but possibly not all having the same sign.
396
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000397 Depends on IEEE 754 arithmetic guarantees and half-even rounding.
398*/
399
Mark Dickinson99dfe922008-05-23 01:35:30 +0000400static PyObject*
401math_sum(PyObject *self, PyObject *seq)
402{
403 PyObject *item, *iter, *sum = NULL;
404 Py_ssize_t i, j, n = 0, m = NUM_PARTIALS;
405 double x, y, hi, lo=0.0, ps[NUM_PARTIALS], *p = ps;
406
407 iter = PyObject_GetIter(seq);
408 if (iter == NULL)
409 return NULL;
410
411 PyFPE_START_PROTECT("sum", Py_DECREF(iter); return NULL)
412
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000413 for(;;) { /* for x in iterable */
Mark Dickinson99dfe922008-05-23 01:35:30 +0000414 assert(0 <= n && n <= m);
415 assert((m == NUM_PARTIALS && p == ps) ||
416 (m > NUM_PARTIALS && p != NULL));
417
418 item = PyIter_Next(iter);
419 if (item == NULL) {
420 if (PyErr_Occurred())
421 goto _sum_error;
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000422 break;
Mark Dickinson99dfe922008-05-23 01:35:30 +0000423 }
424 x = PyFloat_AsDouble(item);
425 Py_DECREF(item);
426 if (PyErr_Occurred())
427 goto _sum_error;
428
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000429 for (i = j = 0; j < n; j++) { /* for y in partials */
Mark Dickinson99dfe922008-05-23 01:35:30 +0000430 y = p[j];
431 hi = x + y;
432 lo = fabs(x) < fabs(y)
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000433 ? x - (hi - y)
434 : y - (hi - x);
Mark Dickinson99dfe922008-05-23 01:35:30 +0000435 if (lo != 0.0)
436 p[i++] = lo;
437 x = hi;
438 }
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000439
440 n = i; /* ps[i:] = [x] */
Mark Dickinson99dfe922008-05-23 01:35:30 +0000441 if (x != 0.0) {
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000442 /* If non-finite, reset partials, effectively
Mark Dickinson99dfe922008-05-23 01:35:30 +0000443 adding subsequent items without roundoff
444 and yielding correct non-finite results,
445 provided IEEE 754 rules are observed */
446 if (! Py_IS_FINITE(x))
447 n = 0;
448 else if (n >= m && _sum_realloc(&p, n, ps, &m))
449 goto _sum_error;
450 p[n++] = x;
451 }
452 }
Mark Dickinson99dfe922008-05-23 01:35:30 +0000453
454 if (n > 0) {
455 hi = p[--n];
456 if (Py_IS_FINITE(hi)) {
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000457 /* sum_exact(ps, hi) from the top, stop when the sum becomes inexact. */
Mark Dickinson99dfe922008-05-23 01:35:30 +0000458 while (n > 0) {
459 x = p[--n];
460 y = hi;
461 hi = x + y;
462 assert(fabs(x) < fabs(y));
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000463 lo = x - (hi - y);
Mark Dickinson99dfe922008-05-23 01:35:30 +0000464 if (lo != 0.0)
465 break;
466 }
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000467 /* Little dance to allow half-even rounding across multiple partials.
468 Needed so that sum([1e-16, 1, 1e16]) will round-up to two instead
469 of down to zero (the 1e16 makes the 1 slightly closer to two). */
Mark Dickinson99dfe922008-05-23 01:35:30 +0000470 if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) ||
471 (lo > 0.0 && p[n-1] > 0.0))) {
472 y = lo * 2.0;
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000473 x = hi + y;
Mark Dickinson99dfe922008-05-23 01:35:30 +0000474 if (y == (x - hi))
475 hi = x;
476 }
477 }
478 else { /* raise corresponding error */
479 errno = Py_IS_NAN(hi) ? EDOM : ERANGE;
480 if (is_error(hi))
481 goto _sum_error;
482 }
483 }
484 else /* default */
485 hi = 0.0;
486 sum = PyFloat_FromDouble(hi);
487
488_sum_error:
489 PyFPE_END_PROTECT(hi)
Mark Dickinson99dfe922008-05-23 01:35:30 +0000490 Py_DECREF(iter);
491 if (p != ps)
492 PyMem_Free(p);
493 return sum;
494}
495
496#undef NUM_PARTIALS
497
498PyDoc_STRVAR(math_sum_doc,
Raymond Hettinger778d5cc2008-05-23 04:32:43 +0000499"sum(iterable)\n\n\
500Return an accurate floating point sum of values in the iterable.\n\
501Assumes IEEE-754 floating point arithmetic.");
Mark Dickinson99dfe922008-05-23 01:35:30 +0000502
Barry Warsaw8b43b191996-12-09 22:32:36 +0000503static PyObject *
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000504math_trunc(PyObject *self, PyObject *number)
505{
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000506 return PyObject_CallMethod(number, "__trunc__", NULL);
507}
508
509PyDoc_STRVAR(math_trunc_doc,
510"trunc(x:Real) -> Integral\n"
511"\n"
Raymond Hettingerfe424f72008-02-02 05:24:44 +0000512"Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.");
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000513
514static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000515math_frexp(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000516{
Guido van Rossumd18ad581991-10-24 14:57:21 +0000517 int i;
Neal Norwitz45e230a2006-11-19 21:26:53 +0000518 double x = PyFloat_AsDouble(arg);
519 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000520 return NULL;
Christian Heimes6f341092008-04-18 23:13:07 +0000521 /* deal with special cases directly, to sidestep platform
522 differences */
523 if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) {
524 i = 0;
525 }
526 else {
527 PyFPE_START_PROTECT("in math_frexp", return 0);
528 x = frexp(x, &i);
529 PyFPE_END_PROTECT(x);
530 }
531 return Py_BuildValue("(di)", x, i);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000532}
533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000534PyDoc_STRVAR(math_frexp_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000535"frexp(x)\n"
536"\n"
537"Return the mantissa and exponent of x, as pair (m, e).\n"
538"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 +0000539"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 +0000540
Barry Warsaw8b43b191996-12-09 22:32:36 +0000541static PyObject *
Fred Drake40c48682000-07-03 18:11:56 +0000542math_ldexp(PyObject *self, PyObject *args)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000543{
Christian Heimes6f341092008-04-18 23:13:07 +0000544 double x, r;
Mark Dickinsonf8476c12008-05-09 17:54:23 +0000545 PyObject *oexp;
546 long exp;
547 if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp))
Guido van Rossumd18ad581991-10-24 14:57:21 +0000548 return NULL;
Mark Dickinsonf8476c12008-05-09 17:54:23 +0000549
550 if (PyLong_Check(oexp)) {
551 /* on overflow, replace exponent with either LONG_MAX
552 or LONG_MIN, depending on the sign. */
553 exp = PyLong_AsLong(oexp);
554 if (exp == -1 && PyErr_Occurred()) {
555 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
556 if (Py_SIZE(oexp) < 0) {
557 exp = LONG_MIN;
558 }
559 else {
560 exp = LONG_MAX;
561 }
562 PyErr_Clear();
563 }
564 else {
565 /* propagate any unexpected exception */
566 return NULL;
567 }
568 }
569 }
570 else if (PyInt_Check(oexp)) {
571 exp = PyInt_AS_LONG(oexp);
572 }
573 else {
574 PyErr_SetString(PyExc_TypeError,
575 "Expected an int or long as second argument "
576 "to ldexp.");
577 return NULL;
578 }
579
580 if (x == 0. || !Py_IS_FINITE(x)) {
581 /* NaNs, zeros and infinities are returned unchanged */
582 r = x;
Christian Heimes6f341092008-04-18 23:13:07 +0000583 errno = 0;
Mark Dickinsonf8476c12008-05-09 17:54:23 +0000584 } else if (exp > INT_MAX) {
585 /* overflow */
586 r = copysign(Py_HUGE_VAL, x);
587 errno = ERANGE;
588 } else if (exp < INT_MIN) {
589 /* underflow to +-0 */
590 r = copysign(0., x);
591 errno = 0;
592 } else {
593 errno = 0;
594 PyFPE_START_PROTECT("in math_ldexp", return 0);
595 r = ldexp(x, (int)exp);
596 PyFPE_END_PROTECT(r);
597 if (Py_IS_INFINITY(r))
598 errno = ERANGE;
599 }
600
Christian Heimes6f341092008-04-18 23:13:07 +0000601 if (errno && is_error(r))
Tim Peters1d120612000-10-12 06:10:25 +0000602 return NULL;
Mark Dickinsonf8476c12008-05-09 17:54:23 +0000603 return PyFloat_FromDouble(r);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000604}
605
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000606PyDoc_STRVAR(math_ldexp_doc,
607"ldexp(x, i) -> x * (2**i)");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000608
Barry Warsaw8b43b191996-12-09 22:32:36 +0000609static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000610math_modf(PyObject *self, PyObject *arg)
Guido van Rossumd18ad581991-10-24 14:57:21 +0000611{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000612 double y, x = PyFloat_AsDouble(arg);
613 if (x == -1.0 && PyErr_Occurred())
Guido van Rossumd18ad581991-10-24 14:57:21 +0000614 return NULL;
Mark Dickinsonb2f70902008-04-20 01:39:24 +0000615 /* some platforms don't do the right thing for NaNs and
616 infinities, so we take care of special cases directly. */
617 if (!Py_IS_FINITE(x)) {
618 if (Py_IS_INFINITY(x))
619 return Py_BuildValue("(dd)", copysign(0., x), x);
620 else if (Py_IS_NAN(x))
621 return Py_BuildValue("(dd)", x, x);
622 }
623
Guido van Rossumd18ad581991-10-24 14:57:21 +0000624 errno = 0;
Christian Heimes6f341092008-04-18 23:13:07 +0000625 PyFPE_START_PROTECT("in math_modf", return 0);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000626 x = modf(x, &y);
Christian Heimes6f341092008-04-18 23:13:07 +0000627 PyFPE_END_PROTECT(x);
628 return Py_BuildValue("(dd)", x, y);
Guido van Rossumd18ad581991-10-24 14:57:21 +0000629}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000631PyDoc_STRVAR(math_modf_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000632"modf(x)\n"
633"\n"
634"Return the fractional and integer parts of x. Both results carry the sign\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000635"of x. The integer part is returned as a real.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000636
Tim Peters78526162001-09-05 00:53:45 +0000637/* A decent logarithm is easy to compute even for huge longs, but libm can't
638 do that by itself -- loghelper can. func is log or log10, and name is
639 "log" or "log10". Note that overflow isn't possible: a long can contain
640 no more than INT_MAX * SHIFT bits, so has value certainly less than
641 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
642 small enough to fit in an IEEE single. log and log10 are even smaller.
643*/
644
645static PyObject*
Neal Norwitz45e230a2006-11-19 21:26:53 +0000646loghelper(PyObject* arg, double (*func)(double), char *funcname)
Tim Peters78526162001-09-05 00:53:45 +0000647{
Tim Peters78526162001-09-05 00:53:45 +0000648 /* If it is long, do it ourselves. */
649 if (PyLong_Check(arg)) {
650 double x;
651 int e;
652 x = _PyLong_AsScaledDouble(arg, &e);
653 if (x <= 0.0) {
654 PyErr_SetString(PyExc_ValueError,
655 "math domain error");
656 return NULL;
657 }
Christian Heimes543cabc2008-01-25 14:54:23 +0000658 /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~=
659 log(x) + log(2) * e * PyLong_SHIFT.
660 CAUTION: e*PyLong_SHIFT may overflow using int arithmetic,
Tim Peters78526162001-09-05 00:53:45 +0000661 so force use of double. */
Christian Heimes543cabc2008-01-25 14:54:23 +0000662 x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0);
Tim Peters78526162001-09-05 00:53:45 +0000663 return PyFloat_FromDouble(x);
664 }
665
666 /* Else let libm handle it by itself. */
Christian Heimes6f341092008-04-18 23:13:07 +0000667 return math_1(arg, func, 0);
Tim Peters78526162001-09-05 00:53:45 +0000668}
669
670static PyObject *
671math_log(PyObject *self, PyObject *args)
672{
Raymond Hettinger866964c2002-12-14 19:51:34 +0000673 PyObject *arg;
674 PyObject *base = NULL;
675 PyObject *num, *den;
676 PyObject *ans;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000677
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000678 if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
Raymond Hettinger866964c2002-12-14 19:51:34 +0000679 return NULL;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000680
Neal Norwitz45e230a2006-11-19 21:26:53 +0000681 num = loghelper(arg, log, "log");
682 if (num == NULL || base == NULL)
683 return num;
Raymond Hettinger866964c2002-12-14 19:51:34 +0000684
Neal Norwitz45e230a2006-11-19 21:26:53 +0000685 den = loghelper(base, log, "log");
Raymond Hettinger866964c2002-12-14 19:51:34 +0000686 if (den == NULL) {
687 Py_DECREF(num);
688 return NULL;
689 }
690
691 ans = PyNumber_Divide(num, den);
692 Py_DECREF(num);
693 Py_DECREF(den);
694 return ans;
Tim Peters78526162001-09-05 00:53:45 +0000695}
696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000697PyDoc_STRVAR(math_log_doc,
Raymond Hettinger866964c2002-12-14 19:51:34 +0000698"log(x[, base]) -> the logarithm of x to the given base.\n\
699If the base not specified, returns the natural logarithm (base e) of x.");
Tim Peters78526162001-09-05 00:53:45 +0000700
701static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000702math_log10(PyObject *self, PyObject *arg)
Tim Peters78526162001-09-05 00:53:45 +0000703{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000704 return loghelper(arg, log10, "log10");
Tim Peters78526162001-09-05 00:53:45 +0000705}
706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000707PyDoc_STRVAR(math_log10_doc,
708"log10(x) -> the base 10 logarithm of x.");
Tim Peters78526162001-09-05 00:53:45 +0000709
Christian Heimes6f341092008-04-18 23:13:07 +0000710static PyObject *
711math_fmod(PyObject *self, PyObject *args)
712{
713 PyObject *ox, *oy;
714 double r, x, y;
715 if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy))
716 return NULL;
717 x = PyFloat_AsDouble(ox);
718 y = PyFloat_AsDouble(oy);
719 if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
720 return NULL;
721 /* fmod(x, +/-Inf) returns x for finite x. */
722 if (Py_IS_INFINITY(y) && Py_IS_FINITE(x))
723 return PyFloat_FromDouble(x);
724 errno = 0;
725 PyFPE_START_PROTECT("in math_fmod", return 0);
726 r = fmod(x, y);
727 PyFPE_END_PROTECT(r);
728 if (Py_IS_NAN(r)) {
729 if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
730 errno = EDOM;
731 else
732 errno = 0;
733 }
734 if (errno && is_error(r))
735 return NULL;
736 else
737 return PyFloat_FromDouble(r);
738}
739
740PyDoc_STRVAR(math_fmod_doc,
741"fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
742" x % y may differ.");
743
744static PyObject *
745math_hypot(PyObject *self, PyObject *args)
746{
747 PyObject *ox, *oy;
748 double r, x, y;
749 if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy))
750 return NULL;
751 x = PyFloat_AsDouble(ox);
752 y = PyFloat_AsDouble(oy);
753 if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
754 return NULL;
755 /* hypot(x, +/-Inf) returns Inf, even if x is a NaN. */
756 if (Py_IS_INFINITY(x))
757 return PyFloat_FromDouble(fabs(x));
758 if (Py_IS_INFINITY(y))
759 return PyFloat_FromDouble(fabs(y));
760 errno = 0;
761 PyFPE_START_PROTECT("in math_hypot", return 0);
762 r = hypot(x, y);
763 PyFPE_END_PROTECT(r);
764 if (Py_IS_NAN(r)) {
765 if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
766 errno = EDOM;
767 else
768 errno = 0;
769 }
770 else if (Py_IS_INFINITY(r)) {
771 if (Py_IS_FINITE(x) && Py_IS_FINITE(y))
772 errno = ERANGE;
773 else
774 errno = 0;
775 }
776 if (errno && is_error(r))
777 return NULL;
778 else
779 return PyFloat_FromDouble(r);
780}
781
782PyDoc_STRVAR(math_hypot_doc,
783"hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).");
784
785/* pow can't use math_2, but needs its own wrapper: the problem is
786 that an infinite result can arise either as a result of overflow
787 (in which case OverflowError should be raised) or as a result of
788 e.g. 0.**-5. (for which ValueError needs to be raised.)
789*/
790
791static PyObject *
792math_pow(PyObject *self, PyObject *args)
793{
794 PyObject *ox, *oy;
795 double r, x, y;
Mark Dickinsoncec3f132008-04-20 04:13:13 +0000796 int odd_y;
Christian Heimes6f341092008-04-18 23:13:07 +0000797
798 if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy))
799 return NULL;
800 x = PyFloat_AsDouble(ox);
801 y = PyFloat_AsDouble(oy);
802 if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
803 return NULL;
Mark Dickinsona1293eb2008-04-19 19:41:52 +0000804
Mark Dickinsoncec3f132008-04-20 04:13:13 +0000805 /* deal directly with IEEE specials, to cope with problems on various
806 platforms whose semantics don't exactly match C99 */
Mark Dickinson0da94c82008-04-21 01:55:50 +0000807 r = 0.; /* silence compiler warning */
Mark Dickinsoncec3f132008-04-20 04:13:13 +0000808 if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) {
809 errno = 0;
810 if (Py_IS_NAN(x))
811 r = y == 0. ? 1. : x; /* NaN**0 = 1 */
812 else if (Py_IS_NAN(y))
813 r = x == 1. ? 1. : y; /* 1**NaN = 1 */
814 else if (Py_IS_INFINITY(x)) {
815 odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0;
816 if (y > 0.)
817 r = odd_y ? x : fabs(x);
818 else if (y == 0.)
819 r = 1.;
820 else /* y < 0. */
821 r = odd_y ? copysign(0., x) : 0.;
822 }
823 else if (Py_IS_INFINITY(y)) {
824 if (fabs(x) == 1.0)
825 r = 1.;
826 else if (y > 0. && fabs(x) > 1.0)
827 r = y;
828 else if (y < 0. && fabs(x) < 1.0) {
829 r = -y; /* result is +inf */
830 if (x == 0.) /* 0**-inf: divide-by-zero */
831 errno = EDOM;
832 }
833 else
834 r = 0.;
835 }
Mark Dickinsone941d972008-04-19 18:51:48 +0000836 }
Mark Dickinsoncec3f132008-04-20 04:13:13 +0000837 else {
838 /* let libm handle finite**finite */
839 errno = 0;
840 PyFPE_START_PROTECT("in math_pow", return 0);
841 r = pow(x, y);
842 PyFPE_END_PROTECT(r);
843 /* a NaN result should arise only from (-ve)**(finite
844 non-integer); in this case we want to raise ValueError. */
845 if (!Py_IS_FINITE(r)) {
846 if (Py_IS_NAN(r)) {
847 errno = EDOM;
848 }
849 /*
850 an infinite result here arises either from:
851 (A) (+/-0.)**negative (-> divide-by-zero)
852 (B) overflow of x**y with x and y finite
853 */
854 else if (Py_IS_INFINITY(r)) {
855 if (x == 0.)
856 errno = EDOM;
857 else
858 errno = ERANGE;
859 }
860 }
Christian Heimes6f341092008-04-18 23:13:07 +0000861 }
862
863 if (errno && is_error(r))
864 return NULL;
865 else
866 return PyFloat_FromDouble(r);
867}
868
869PyDoc_STRVAR(math_pow_doc,
870"pow(x,y)\n\nReturn x**y (x to the power of y).");
871
Christian Heimese2ca4242008-01-03 20:23:15 +0000872static const double degToRad = Py_MATH_PI / 180.0;
873static const double radToDeg = 180.0 / Py_MATH_PI;
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000874
875static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000876math_degrees(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000877{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000878 double x = PyFloat_AsDouble(arg);
879 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000880 return NULL;
Christian Heimese2ca4242008-01-03 20:23:15 +0000881 return PyFloat_FromDouble(x * radToDeg);
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000882}
883
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000884PyDoc_STRVAR(math_degrees_doc,
885"degrees(x) -> converts angle x from radians to degrees");
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000886
887static PyObject *
Neal Norwitz45e230a2006-11-19 21:26:53 +0000888math_radians(PyObject *self, PyObject *arg)
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000889{
Neal Norwitz45e230a2006-11-19 21:26:53 +0000890 double x = PyFloat_AsDouble(arg);
891 if (x == -1.0 && PyErr_Occurred())
Raymond Hettingerd6f22672002-05-13 03:56:10 +0000892 return NULL;
893 return PyFloat_FromDouble(x * degToRad);
894}
895
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000896PyDoc_STRVAR(math_radians_doc,
897"radians(x) -> converts angle x from degrees to radians");
Tim Peters78526162001-09-05 00:53:45 +0000898
Christian Heimese2ca4242008-01-03 20:23:15 +0000899static PyObject *
900math_isnan(PyObject *self, PyObject *arg)
901{
902 double x = PyFloat_AsDouble(arg);
903 if (x == -1.0 && PyErr_Occurred())
904 return NULL;
905 return PyBool_FromLong((long)Py_IS_NAN(x));
906}
907
908PyDoc_STRVAR(math_isnan_doc,
909"isnan(x) -> bool\n\
910Checks if float x is not a number (NaN)");
911
912static PyObject *
913math_isinf(PyObject *self, PyObject *arg)
914{
915 double x = PyFloat_AsDouble(arg);
916 if (x == -1.0 && PyErr_Occurred())
917 return NULL;
918 return PyBool_FromLong((long)Py_IS_INFINITY(x));
919}
920
921PyDoc_STRVAR(math_isinf_doc,
922"isinf(x) -> bool\n\
923Checks if float x is infinite (positive or negative)");
924
Barry Warsaw8b43b191996-12-09 22:32:36 +0000925static PyMethodDef math_methods[] = {
Neal Norwitz45e230a2006-11-19 21:26:53 +0000926 {"acos", math_acos, METH_O, math_acos_doc},
Christian Heimes6f341092008-04-18 23:13:07 +0000927 {"acosh", math_acosh, METH_O, math_acosh_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000928 {"asin", math_asin, METH_O, math_asin_doc},
Christian Heimes6f341092008-04-18 23:13:07 +0000929 {"asinh", math_asinh, METH_O, math_asinh_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000930 {"atan", math_atan, METH_O, math_atan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000931 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
Christian Heimes6f341092008-04-18 23:13:07 +0000932 {"atanh", math_atanh, METH_O, math_atanh_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000933 {"ceil", math_ceil, METH_O, math_ceil_doc},
Christian Heimeseebb79c2008-01-03 22:32:26 +0000934 {"copysign", math_copysign, METH_VARARGS, math_copysign_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000935 {"cos", math_cos, METH_O, math_cos_doc},
936 {"cosh", math_cosh, METH_O, math_cosh_doc},
937 {"degrees", math_degrees, METH_O, math_degrees_doc},
938 {"exp", math_exp, METH_O, math_exp_doc},
939 {"fabs", math_fabs, METH_O, math_fabs_doc},
940 {"floor", math_floor, METH_O, math_floor_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000941 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000942 {"frexp", math_frexp, METH_O, math_frexp_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000943 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
Christian Heimese2ca4242008-01-03 20:23:15 +0000944 {"isinf", math_isinf, METH_O, math_isinf_doc},
945 {"isnan", math_isnan, METH_O, math_isnan_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000946 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
947 {"log", math_log, METH_VARARGS, math_log_doc},
Christian Heimes6f341092008-04-18 23:13:07 +0000948 {"log1p", math_log1p, METH_O, math_log1p_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000949 {"log10", math_log10, METH_O, math_log10_doc},
950 {"modf", math_modf, METH_O, math_modf_doc},
Fred Drake40c48682000-07-03 18:11:56 +0000951 {"pow", math_pow, METH_VARARGS, math_pow_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000952 {"radians", math_radians, METH_O, math_radians_doc},
953 {"sin", math_sin, METH_O, math_sin_doc},
954 {"sinh", math_sinh, METH_O, math_sinh_doc},
955 {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
Mark Dickinson99dfe922008-05-23 01:35:30 +0000956 {"sum", math_sum, METH_O, math_sum_doc},
Neal Norwitz45e230a2006-11-19 21:26:53 +0000957 {"tan", math_tan, METH_O, math_tan_doc},
958 {"tanh", math_tanh, METH_O, math_tanh_doc},
Jeffrey Yasskinca2b69f2008-02-01 06:22:46 +0000959 {"trunc", math_trunc, METH_O, math_trunc_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000960 {NULL, NULL} /* sentinel */
961};
962
Guido van Rossumc6e22901998-12-04 19:26:43 +0000963
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000964PyDoc_STRVAR(module_doc,
Tim Peters63c94532001-09-04 23:17:42 +0000965"This module is always available. It provides access to the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000966"mathematical functions defined by the C standard.");
Guido van Rossumc6e22901998-12-04 19:26:43 +0000967
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000968PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000969initmath(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000970{
Christian Heimes6f341092008-04-18 23:13:07 +0000971 PyObject *m;
Tim Petersfe71f812001-08-07 22:10:00 +0000972
Guido van Rossumc6e22901998-12-04 19:26:43 +0000973 m = Py_InitModule3("math", math_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000974 if (m == NULL)
975 goto finally;
Barry Warsawfc93f751996-12-17 00:47:03 +0000976
Christian Heimes6f341092008-04-18 23:13:07 +0000977 PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI));
978 PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
Barry Warsawfc93f751996-12-17 00:47:03 +0000979
Christian Heimes6f341092008-04-18 23:13:07 +0000980 finally:
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000981 return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000982}