Mercurial > cpython-lru_cache-weakref
annotate Modules/mathmodule.c @ 47927:76325c19af84 legacy-trunk
Document the fact that urllib2 spans multiple modules with new names in Python
3.0.
| author | Brett Cannon <bcannon@gmail.com> |
|---|---|
| date | Fri, 11 Jul 2008 00:12:52 +0000 |
| parents | a7186fee7596 |
| children | b40e13bcb9ea |
| rev | line source |
|---|---|
| 6 | 1 /* Math module -- standard C math library functions, pi and e */ |
| 2 | |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
3 /* Here are some comments from Tim Peters, extracted from the |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
4 discussion attached to http://bugs.python.org/issue1640. They |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
5 describe the general aims of the math module with respect to |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
6 special values, IEEE-754 floating-point exceptions, and Python |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
7 exceptions. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
8 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
9 These are the "spirit of 754" rules: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
10 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
11 1. If the mathematical result is a real number, but of magnitude too |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
12 large to approximate by a machine float, overflow is signaled and the |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
13 result is an infinity (with the appropriate sign). |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
14 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
15 2. If the mathematical result is a real number, but of magnitude too |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
16 small to approximate by a machine float, underflow is signaled and the |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
17 result is a zero (with the appropriate sign). |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
18 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
19 3. At a singularity (a value x such that the limit of f(y) as y |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
20 approaches x exists and is an infinity), "divide by zero" is signaled |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
21 and the result is an infinity (with the appropriate sign). This is |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
22 complicated a little by that the left-side and right-side limits may |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
23 not be the same; e.g., 1/x approaches +inf or -inf as x approaches 0 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
24 from the positive or negative directions. In that specific case, the |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
25 sign of the zero determines the result of 1/0. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
26 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
27 4. At a point where a function has no defined result in the extended |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
28 reals (i.e., the reals plus an infinity or two), invalid operation is |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
29 signaled and a NaN is returned. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
30 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
31 And these are what Python has historically /tried/ to do (but not |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
32 always successfully, as platform libm behavior varies a lot): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
33 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
34 For #1, raise OverflowError. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
35 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
36 For #2, return a zero (with the appropriate sign if that happens by |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
37 accident ;-)). |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
38 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
39 For #3 and #4, raise ValueError. It may have made sense to raise |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
40 Python's ZeroDivisionError in #3, but historically that's only been |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
41 raised for division by zero and mod by zero. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
42 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
43 */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
44 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
45 /* |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
46 In general, on an IEEE-754 platform the aim is to follow the C99 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
47 standard, including Annex 'F', whenever possible. Where the |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
48 standard recommends raising the 'divide-by-zero' or 'invalid' |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
49 floating-point exceptions, Python should raise a ValueError. Where |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
50 the standard recommends raising 'overflow', Python should raise an |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
51 OverflowError. In all other circumstances a value should be |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
52 returned. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
53 */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
54 |
| 4635 | 55 #include "Python.h" |
|
34872
0143d990d60c
Make that a C, not a C++, comment.
Michael W. Hudson <mwh@python.net>
parents:
34871
diff
changeset
|
56 #include "longintrepr.h" /* just for SHIFT */ |
| 6 | 57 |
|
44940
12099143724f
Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents:
44488
diff
changeset
|
58 #ifdef _OSF_SOURCE |
|
12099143724f
Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents:
44488
diff
changeset
|
59 /* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */ |
|
12099143724f
Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents:
44488
diff
changeset
|
60 extern double copysign(double, double); |
|
12099143724f
Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents:
44488
diff
changeset
|
61 #endif |
|
12099143724f
Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
Neal Norwitz <nnorwitz@gmail.com>
parents:
44488
diff
changeset
|
62 |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
63 /* Call is_error when errno != 0, and where x is the result libm |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
64 * returned. is_error will usually set up an exception and return |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
65 * true (1), but may return false (0) without setting up an exception. |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
66 */ |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
67 static int |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
68 is_error(double x) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
69 { |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
70 int result = 1; /* presumption of guilt */ |
|
15181
2ed358446f2f
Repaired a comment and asserted a precondition.
Tim Peters <tim.peters@gmail.com>
parents:
15170
diff
changeset
|
71 assert(errno); /* non-zero errno is a precondition for calling */ |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
72 if (errno == EDOM) |
| 4635 | 73 PyErr_SetString(PyExc_ValueError, "math domain error"); |
|
19750
35b6a1d0b1e0
Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents:
19730
diff
changeset
|
74 |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
75 else if (errno == ERANGE) { |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
76 /* ANSI C generally requires libm functions to set ERANGE |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
77 * on overflow, but also generally *allows* them to set |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
78 * ERANGE on underflow too. There's no consistency about |
|
19750
35b6a1d0b1e0
Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents:
19730
diff
changeset
|
79 * the latter across platforms. |
|
35b6a1d0b1e0
Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents:
19730
diff
changeset
|
80 * Alas, C99 never requires that errno be set. |
|
35b6a1d0b1e0
Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents:
19730
diff
changeset
|
81 * Here we suppress the underflow errors (libm functions |
|
35b6a1d0b1e0
Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents:
19730
diff
changeset
|
82 * should return a zero on underflow, and +- HUGE_VAL on |
|
35b6a1d0b1e0
Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents:
19730
diff
changeset
|
83 * overflow, so testing the result for zero suffices to |
|
35b6a1d0b1e0
Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents:
19730
diff
changeset
|
84 * distinguish the cases). |
|
47752
ca074232b879
Issue 3118: make test_math pass on Ubuntu/ia64. exp(-745.0) was raising
Mark Dickinson <dickinsm@gmail.com>
parents:
47532
diff
changeset
|
85 * |
|
ca074232b879
Issue 3118: make test_math pass on Ubuntu/ia64. exp(-745.0) was raising
Mark Dickinson <dickinsm@gmail.com>
parents:
47532
diff
changeset
|
86 * On some platforms (Ubuntu/ia64) it seems that errno can be |
|
ca074232b879
Issue 3118: make test_math pass on Ubuntu/ia64. exp(-745.0) was raising
Mark Dickinson <dickinsm@gmail.com>
parents:
47532
diff
changeset
|
87 * set to ERANGE for subnormal results that do *not* underflow |
|
ca074232b879
Issue 3118: make test_math pass on Ubuntu/ia64. exp(-745.0) was raising
Mark Dickinson <dickinsm@gmail.com>
parents:
47532
diff
changeset
|
88 * to zero. So to be safe, we'll ignore ERANGE whenever the |
|
ca074232b879
Issue 3118: make test_math pass on Ubuntu/ia64. exp(-745.0) was raising
Mark Dickinson <dickinsm@gmail.com>
parents:
47532
diff
changeset
|
89 * function result is less than one in absolute value. |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
90 */ |
|
47752
ca074232b879
Issue 3118: make test_math pass on Ubuntu/ia64. exp(-745.0) was raising
Mark Dickinson <dickinsm@gmail.com>
parents:
47532
diff
changeset
|
91 if (fabs(x) < 1.0) |
|
ca074232b879
Issue 3118: make test_math pass on Ubuntu/ia64. exp(-745.0) was raising
Mark Dickinson <dickinsm@gmail.com>
parents:
47532
diff
changeset
|
92 result = 0; |
|
ca074232b879
Issue 3118: make test_math pass on Ubuntu/ia64. exp(-745.0) was raising
Mark Dickinson <dickinsm@gmail.com>
parents:
47532
diff
changeset
|
93 else |
|
19048
e282d6b2b758
Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents:
18066
diff
changeset
|
94 PyErr_SetString(PyExc_OverflowError, |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
95 "math range error"); |
|
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
96 } |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
97 else |
| 4635 | 98 /* Unexpected math error */ |
| 99 PyErr_SetFromErrno(PyExc_ValueError); | |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
100 return result; |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
101 } |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
102 |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
103 /* |
|
46507
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
104 wrapper for atan2 that deals directly with special cases before |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
105 delegating to the platform libm for the remaining cases. This |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
106 is necessary to get consistent behaviour across platforms. |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
107 Windows, FreeBSD and alpha Tru64 are amongst platforms that don't |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
108 always follow C99. |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
109 */ |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
110 |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
111 static double |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
112 m_atan2(double y, double x) |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
113 { |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
114 if (Py_IS_NAN(x) || Py_IS_NAN(y)) |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
115 return Py_NAN; |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
116 if (Py_IS_INFINITY(y)) { |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
117 if (Py_IS_INFINITY(x)) { |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
118 if (copysign(1., x) == 1.) |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
119 /* atan2(+-inf, +inf) == +-pi/4 */ |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
120 return copysign(0.25*Py_MATH_PI, y); |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
121 else |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
122 /* atan2(+-inf, -inf) == +-pi*3/4 */ |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
123 return copysign(0.75*Py_MATH_PI, y); |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
124 } |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
125 /* atan2(+-inf, x) == +-pi/2 for finite x */ |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
126 return copysign(0.5*Py_MATH_PI, y); |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
127 } |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
128 if (Py_IS_INFINITY(x) || y == 0.) { |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
129 if (copysign(1., x) == 1.) |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
130 /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */ |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
131 return copysign(0., y); |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
132 else |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
133 /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */ |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
134 return copysign(Py_MATH_PI, y); |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
135 } |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
136 return atan2(y, x); |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
137 } |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
138 |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
139 /* |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
140 math_1 is used to wrap a libm function f that takes a double |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
141 arguments and returns a double. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
142 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
143 The error reporting follows these rules, which are designed to do |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
144 the right thing on C89/C99 platforms and IEEE 754/non IEEE 754 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
145 platforms. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
146 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
147 - a NaN result from non-NaN inputs causes ValueError to be raised |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
148 - an infinite result from finite inputs causes OverflowError to be |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
149 raised if can_overflow is 1, or raises ValueError if can_overflow |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
150 is 0. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
151 - if the result is finite and errno == EDOM then ValueError is |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
152 raised |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
153 - if the result is finite and nonzero and errno == ERANGE then |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
154 OverflowError is raised |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
155 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
156 The last rule is used to catch overflow on platforms which follow |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
157 C89 but for which HUGE_VAL is not an infinity. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
158 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
159 For the majority of one-argument functions these rules are enough |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
160 to ensure that Python's functions behave as specified in 'Annex F' |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
161 of the C99 standard, with the 'invalid' and 'divide-by-zero' |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
162 floating-point exceptions mapping to Python's ValueError and the |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
163 'overflow' floating-point exception mapping to OverflowError. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
164 math_1 only works for functions that don't have singularities *and* |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
165 the possibility of overflow; fortunately, that covers everything we |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
166 care about right now. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
167 */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
168 |
| 4635 | 169 static PyObject * |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
170 math_1(PyObject *arg, double (*func) (double), int can_overflow) |
| 6 | 171 { |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
172 double x, r; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
173 x = PyFloat_AsDouble(arg); |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
174 if (x == -1.0 && PyErr_Occurred()) |
| 6 | 175 return NULL; |
| 176 errno = 0; | |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
177 PyFPE_START_PROTECT("in math_1", return 0); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
178 r = (*func)(x); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
179 PyFPE_END_PROTECT(r); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
180 if (Py_IS_NAN(r)) { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
181 if (!Py_IS_NAN(x)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
182 errno = EDOM; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
183 else |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
184 errno = 0; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
185 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
186 else if (Py_IS_INFINITY(r)) { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
187 if (Py_IS_FINITE(x)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
188 errno = can_overflow ? ERANGE : EDOM; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
189 else |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
190 errno = 0; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
191 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
192 if (errno && is_error(r)) |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
193 return NULL; |
| 6 | 194 else |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
195 return PyFloat_FromDouble(r); |
| 6 | 196 } |
| 197 | |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
198 /* |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
199 math_2 is used to wrap a libm function f that takes two double |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
200 arguments and returns a double. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
201 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
202 The error reporting follows these rules, which are designed to do |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
203 the right thing on C89/C99 platforms and IEEE 754/non IEEE 754 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
204 platforms. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
205 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
206 - a NaN result from non-NaN inputs causes ValueError to be raised |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
207 - an infinite result from finite inputs causes OverflowError to be |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
208 raised. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
209 - if the result is finite and errno == EDOM then ValueError is |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
210 raised |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
211 - if the result is finite and nonzero and errno == ERANGE then |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
212 OverflowError is raised |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
213 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
214 The last rule is used to catch overflow on platforms which follow |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
215 C89 but for which HUGE_VAL is not an infinity. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
216 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
217 For most two-argument functions (copysign, fmod, hypot, atan2) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
218 these rules are enough to ensure that Python's functions behave as |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
219 specified in 'Annex F' of the C99 standard, with the 'invalid' and |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
220 'divide-by-zero' floating-point exceptions mapping to Python's |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
221 ValueError and the 'overflow' floating-point exception mapping to |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
222 OverflowError. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
223 */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
224 |
| 4635 | 225 static PyObject * |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
226 math_2(PyObject *args, double (*func) (double, double), char *funcname) |
| 6 | 227 { |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
228 PyObject *ox, *oy; |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
229 double x, y, r; |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
230 if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy)) |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
231 return NULL; |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
232 x = PyFloat_AsDouble(ox); |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
233 y = PyFloat_AsDouble(oy); |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
234 if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
| 6 | 235 return NULL; |
| 236 errno = 0; | |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
237 PyFPE_START_PROTECT("in math_2", return 0); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
238 r = (*func)(x, y); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
239 PyFPE_END_PROTECT(r); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
240 if (Py_IS_NAN(r)) { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
241 if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
242 errno = EDOM; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
243 else |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
244 errno = 0; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
245 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
246 else if (Py_IS_INFINITY(r)) { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
247 if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
248 errno = ERANGE; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
249 else |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
250 errno = 0; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
251 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
252 if (errno && is_error(r)) |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
253 return NULL; |
| 6 | 254 else |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
255 return PyFloat_FromDouble(r); |
| 6 | 256 } |
| 257 | |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
258 #define FUNC1(funcname, func, can_overflow, docstring) \ |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
259 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
260 return math_1(args, func, can_overflow); \ |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
261 }\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
262 PyDoc_STRVAR(math_##funcname##_doc, docstring); |
| 6 | 263 |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
264 #define FUNC2(funcname, func, docstring) \ |
|
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
265 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
266 return math_2(args, func, #funcname); \ |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
267 }\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
268 PyDoc_STRVAR(math_##funcname##_doc, docstring); |
| 6 | 269 |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
270 FUNC1(acos, acos, 0, |
|
19048
e282d6b2b758
Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents:
18066
diff
changeset
|
271 "acos(x)\n\nReturn the arc cosine (measured in radians) of x.") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
272 FUNC1(acosh, acosh, 0, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
273 "acosh(x)\n\nReturn the hyperbolic arc cosine (measured in radians) of x.") |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
274 FUNC1(asin, asin, 0, |
|
19048
e282d6b2b758
Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents:
18066
diff
changeset
|
275 "asin(x)\n\nReturn the arc sine (measured in radians) of x.") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
276 FUNC1(asinh, asinh, 0, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
277 "asinh(x)\n\nReturn the hyperbolic arc sine (measured in radians) of x.") |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
278 FUNC1(atan, atan, 0, |
|
19048
e282d6b2b758
Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents:
18066
diff
changeset
|
279 "atan(x)\n\nReturn the arc tangent (measured in radians) of x.") |
|
46507
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
280 FUNC2(atan2, m_atan2, |
|
19048
e282d6b2b758
Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents:
18066
diff
changeset
|
281 "atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n" |
|
e282d6b2b758
Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents:
18066
diff
changeset
|
282 "Unlike atan(y/x), the signs of both x and y are considered.") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
283 FUNC1(atanh, atanh, 0, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
284 "atanh(x)\n\nReturn the hyperbolic arc tangent (measured in radians) of x.") |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
285 FUNC1(ceil, ceil, 0, |
|
44488
94eeaeeb3ce9
Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44453
diff
changeset
|
286 "ceil(x)\n\nReturn the ceiling of x as a float.\n" |
|
94eeaeeb3ce9
Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44453
diff
changeset
|
287 "This is the smallest integral value >= x.") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
288 FUNC2(copysign, copysign, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
289 "copysign(x,y)\n\nReturn x with the sign of y.") |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
290 FUNC1(cos, cos, 0, |
|
19048
e282d6b2b758
Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents:
18066
diff
changeset
|
291 "cos(x)\n\nReturn the cosine of x (measured in radians).") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
292 FUNC1(cosh, cosh, 1, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
293 "cosh(x)\n\nReturn the hyperbolic cosine of x.") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
294 FUNC1(exp, exp, 1, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
295 "exp(x)\n\nReturn e raised to the power of x.") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
296 FUNC1(fabs, fabs, 0, |
|
19048
e282d6b2b758
Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents:
18066
diff
changeset
|
297 "fabs(x)\n\nReturn the absolute value of the float x.") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
298 FUNC1(floor, floor, 0, |
|
44488
94eeaeeb3ce9
Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44453
diff
changeset
|
299 "floor(x)\n\nReturn the floor of x as a float.\n" |
|
94eeaeeb3ce9
Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44453
diff
changeset
|
300 "This is the largest integral value <= x.") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
301 FUNC1(log1p, log1p, 1, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
302 "log1p(x)\n\nReturn the natural logarithm of 1+x (base e).\n\ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
303 The result is computed in a way which is accurate for x near zero.") |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
304 FUNC1(sin, sin, 0, |
|
19048
e282d6b2b758
Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents:
18066
diff
changeset
|
305 "sin(x)\n\nReturn the sine of x (measured in radians).") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
306 FUNC1(sinh, sinh, 1, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
307 "sinh(x)\n\nReturn the hyperbolic sine of x.") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
308 FUNC1(sqrt, sqrt, 0, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
309 "sqrt(x)\n\nReturn the square root of x.") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
310 FUNC1(tan, tan, 0, |
|
19048
e282d6b2b758
Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents:
18066
diff
changeset
|
311 "tan(x)\n\nReturn the tangent of x (measured in radians).") |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
312 FUNC1(tanh, tanh, 0, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
313 "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
| 6 | 314 |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
315 /* Precision summation function as msum() by Raymond Hettinger in |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
316 <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090>, |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
317 enhanced with the exact partials sum and roundoff from Mark |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
318 Dickinson's post at <http://bugs.python.org/file10357/msum4.py>. |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
319 See those links for more details, proofs and other references. |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
320 |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
321 Note 1: IEEE 754R floating point semantics are assumed, |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
322 but the current implementation does not re-establish special |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
323 value semantics across iterations (i.e. handling -Inf + Inf). |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
324 |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
325 Note 2: No provision is made for intermediate overflow handling; |
| 47370 | 326 therefore, sum([1e+308, 1e-308, 1e+308]) returns 1e+308 while |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
327 sum([1e+308, 1e+308, 1e-308]) raises an OverflowError due to the |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
328 overflow of the first partial sum. |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
329 |
| 47777 | 330 Note 3: The intermediate values lo, yr, and hi are declared volatile so |
|
47783
a7186fee7596
Fix another typo in math_sum comment
Mark Dickinson <dickinsm@gmail.com>
parents:
47777
diff
changeset
|
331 aggressive compilers won't algebraically reduce lo to always be exactly 0.0. |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
332 Also, the volatile declaration forces the values to be stored in memory as |
|
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
333 regular doubles instead of extended long precision (80-bit) values. This |
| 47777 | 334 prevents double rounding because any addition or subtraction of two doubles |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
335 can be resolved exactly into double-sized hi and lo values. As long as the |
|
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
336 hi value gets forced into a double before yr and lo are computed, the extra |
|
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
337 bits in downstream extended precision operations (x87 for example) will be |
|
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
338 exactly zero and therefore can be losslessly stored back into a double, |
|
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
339 thereby preventing double rounding. |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
340 |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
341 Note 4: A similar implementation is in Modules/cmathmodule.c. |
|
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
342 Be sure to update both when making changes. |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
343 |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
344 Note 5: The signature of math.sum() differs from __builtin__.sum() |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
345 because the start argument doesn't make sense in the context of |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
346 accurate summation. Since the partials table is collapsed before |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
347 returning a result, sum(seq2, start=sum(seq1)) may not equal the |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
348 accurate result returned by sum(itertools.chain(seq1, seq2)). |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
349 */ |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
350 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
351 #define NUM_PARTIALS 32 /* initial partials array size, on stack */ |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
352 |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
353 /* Extend the partials array p[] by doubling its size. */ |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
354 static int /* non-zero on error */ |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
355 _sum_realloc(double **p_ptr, Py_ssize_t n, |
|
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
356 double *ps, Py_ssize_t *m_ptr) |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
357 { |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
358 void *v = NULL; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
359 Py_ssize_t m = *m_ptr; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
360 |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
361 m += m; /* double */ |
|
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
362 if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) { |
|
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
363 double *p = *p_ptr; |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
364 if (p == ps) { |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
365 v = PyMem_Malloc(sizeof(double) * m); |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
366 if (v != NULL) |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
367 memcpy(v, ps, sizeof(double) * n); |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
368 } |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
369 else |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
370 v = PyMem_Realloc(p, sizeof(double) * m); |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
371 } |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
372 if (v == NULL) { /* size overflow or no memory */ |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
373 PyErr_SetString(PyExc_MemoryError, "math sum partials"); |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
374 return 1; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
375 } |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
376 *p_ptr = (double*) v; |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
377 *m_ptr = m; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
378 return 0; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
379 } |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
380 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
381 /* Full precision summation of a sequence of floats. |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
382 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
383 def msum(iterable): |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
384 partials = [] # sorted, non-overlapping partial sums |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
385 for x in iterable: |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
386 i = 0 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
387 for y in partials: |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
388 if abs(x) < abs(y): |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
389 x, y = y, x |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
390 hi = x + y |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
391 lo = y - (hi - x) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
392 if lo: |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
393 partials[i] = lo |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
394 i += 1 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
395 x = hi |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
396 partials[i:] = [x] |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
397 return sum_exact(partials) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
398 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
399 Rounded x+y stored in hi with the roundoff stored in lo. Together hi+lo |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
400 are exactly equal to x+y. The inner loop applies hi/lo summation to each |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
401 partial so that the list of partial sums remains exact. |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
402 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
403 Sum_exact() adds the partial sums exactly and correctly rounds the final |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
404 result (using the round-half-to-even rule). The items in partials remain |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
405 non-zero, non-special, non-overlapping and strictly increasing in |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
406 magnitude, but possibly not all having the same sign. |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
407 |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
408 Depends on IEEE 754 arithmetic guarantees and half-even rounding. |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
409 */ |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
410 |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
411 static PyObject* |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
412 math_sum(PyObject *self, PyObject *seq) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
413 { |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
414 PyObject *item, *iter, *sum = NULL; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
415 Py_ssize_t i, j, n = 0, m = NUM_PARTIALS; |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
416 double x, y, t, ps[NUM_PARTIALS], *p = ps; |
|
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
417 volatile double hi, yr, lo; |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
418 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
419 iter = PyObject_GetIter(seq); |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
420 if (iter == NULL) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
421 return NULL; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
422 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
423 PyFPE_START_PROTECT("sum", Py_DECREF(iter); return NULL) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
424 |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
425 for(;;) { /* for x in iterable */ |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
426 assert(0 <= n && n <= m); |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
427 assert((m == NUM_PARTIALS && p == ps) || |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
428 (m > NUM_PARTIALS && p != NULL)); |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
429 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
430 item = PyIter_Next(iter); |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
431 if (item == NULL) { |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
432 if (PyErr_Occurred()) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
433 goto _sum_error; |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
434 break; |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
435 } |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
436 x = PyFloat_AsDouble(item); |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
437 Py_DECREF(item); |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
438 if (PyErr_Occurred()) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
439 goto _sum_error; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
440 |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
441 for (i = j = 0; j < n; j++) { /* for y in partials */ |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
442 y = p[j]; |
|
47395
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
443 if (fabs(x) < fabs(y)) { |
|
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
444 t = x; x = y; y = t; |
|
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
445 } |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
446 hi = x + y; |
|
47395
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
447 yr = hi - x; |
|
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
448 lo = y - yr; |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
449 if (lo != 0.0) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
450 p[i++] = lo; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
451 x = hi; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
452 } |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
453 |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
454 n = i; /* ps[i:] = [x] */ |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
455 if (x != 0.0) { |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
456 /* If non-finite, reset partials, effectively |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
457 adding subsequent items without roundoff |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
458 and yielding correct non-finite results, |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
459 provided IEEE 754 rules are observed */ |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
460 if (! Py_IS_FINITE(x)) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
461 n = 0; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
462 else if (n >= m && _sum_realloc(&p, n, ps, &m)) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
463 goto _sum_error; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
464 p[n++] = x; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
465 } |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
466 } |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
467 |
|
47395
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
468 hi = 0.0; |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
469 if (n > 0) { |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
470 hi = p[--n]; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
471 if (Py_IS_FINITE(hi)) { |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
472 /* sum_exact(ps, hi) from the top, stop when the sum becomes inexact. */ |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
473 while (n > 0) { |
|
47395
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
474 x = hi; |
|
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
475 y = p[--n]; |
|
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
476 assert(fabs(y) < fabs(x)); |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
477 hi = x + y; |
|
47395
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
478 yr = hi - x; |
|
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
479 lo = y - yr; |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
480 if (lo != 0.0) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
481 break; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
482 } |
|
47395
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
483 /* Make half-even rounding work across multiple partials. Needed |
|
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
484 so that sum([1e-16, 1, 1e16]) will round-up the last digit to |
|
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
485 two instead of down to zero (the 1e-16 makes the 1 slightly |
|
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
486 closer to two). With a potential 1 ULP rounding error fixed-up, |
|
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
487 math.sum() can guarantee commutativity. */ |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
488 if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) || |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
489 (lo > 0.0 && p[n-1] > 0.0))) { |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
490 y = lo * 2.0; |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
491 x = hi + y; |
|
47395
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
492 yr = x - hi; |
|
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
493 if (y == yr) |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
494 hi = x; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
495 } |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
496 } |
|
47395
bba29f462bcc
* Mark intermedidate computes values (hi, lo, yr) as volatile.
Raymond Hettinger <python@rcn.com>
parents:
47370
diff
changeset
|
497 else { /* raise exception corresponding to a special value */ |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
498 errno = Py_IS_NAN(hi) ? EDOM : ERANGE; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
499 if (is_error(hi)) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
500 goto _sum_error; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
501 } |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
502 } |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
503 sum = PyFloat_FromDouble(hi); |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
504 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
505 _sum_error: |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
506 PyFPE_END_PROTECT(hi) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
507 Py_DECREF(iter); |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
508 if (p != ps) |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
509 PyMem_Free(p); |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
510 return sum; |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
511 } |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
512 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
513 #undef NUM_PARTIALS |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
514 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
515 PyDoc_STRVAR(math_sum_doc, |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
516 "sum(iterable)\n\n\ |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
517 Return an accurate floating point sum of values in the iterable.\n\ |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
518 Assumes IEEE-754 floating point arithmetic."); |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
519 |
|
47529
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
520 static PyObject * |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
521 math_factorial(PyObject *self, PyObject *arg) |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
522 { |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
523 long i, x; |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
524 PyObject *result, *iobj, *newresult; |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
525 |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
526 if (PyFloat_Check(arg)) { |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
527 double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg); |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
528 if (dx != floor(dx)) { |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
529 PyErr_SetString(PyExc_ValueError, |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
530 "factorial() only accepts integral values"); |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
531 return NULL; |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
532 } |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
533 } |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
534 |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
535 x = PyInt_AsLong(arg); |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
536 if (x == -1 && PyErr_Occurred()) |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
537 return NULL; |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
538 if (x < 0) { |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
539 PyErr_SetString(PyExc_ValueError, |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
540 "factorial() not defined for negative values"); |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
541 return NULL; |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
542 } |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
543 |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
544 result = (PyObject *)PyInt_FromLong(1); |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
545 if (result == NULL) |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
546 return NULL; |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
547 for (i=1 ; i<=x ; i++) { |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
548 iobj = (PyObject *)PyInt_FromLong(i); |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
549 if (iobj == NULL) |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
550 goto error; |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
551 newresult = PyNumber_Multiply(result, iobj); |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
552 Py_DECREF(iobj); |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
553 if (newresult == NULL) |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
554 goto error; |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
555 Py_DECREF(result); |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
556 result = newresult; |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
557 } |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
558 return result; |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
559 |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
560 error: |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
561 Py_DECREF(result); |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
562 return NULL; |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
563 } |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
564 |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
565 PyDoc_STRVAR(math_factorial_doc, "Return n!"); |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
566 |
| 4635 | 567 static PyObject * |
|
45079
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
568 math_trunc(PyObject *self, PyObject *number) |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
569 { |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
570 return PyObject_CallMethod(number, "__trunc__", NULL); |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
571 } |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
572 |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
573 PyDoc_STRVAR(math_trunc_doc, |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
574 "trunc(x:Real) -> Integral\n" |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
575 "\n" |
| 45102 | 576 "Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method."); |
|
45079
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
577 |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
578 static PyObject * |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
579 math_frexp(PyObject *self, PyObject *arg) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
580 { |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
581 int i; |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
582 double x = PyFloat_AsDouble(arg); |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
583 if (x == -1.0 && PyErr_Occurred()) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
584 return NULL; |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
585 /* deal with special cases directly, to sidestep platform |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
586 differences */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
587 if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
588 i = 0; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
589 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
590 else { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
591 PyFPE_START_PROTECT("in math_frexp", return 0); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
592 x = frexp(x, &i); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
593 PyFPE_END_PROTECT(x); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
594 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
595 return Py_BuildValue("(di)", x, i); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
596 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
597 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
598 PyDoc_STRVAR(math_frexp_doc, |
|
19712
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
599 "frexp(x)\n" |
|
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
600 "\n" |
|
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
601 "Return the mantissa and exponent of x, as pair (m, e).\n" |
|
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
602 "m is a float and e is an int, such that x = m * 2.**e.\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
603 "If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0."); |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
604 |
| 4635 | 605 static PyObject * |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
606 math_ldexp(PyObject *self, PyObject *args) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
607 { |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
608 double x, r; |
|
46797
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
609 PyObject *oexp; |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
610 long exp; |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
611 if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp)) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
612 return NULL; |
|
46797
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
613 |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
614 if (PyLong_Check(oexp)) { |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
615 /* on overflow, replace exponent with either LONG_MAX |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
616 or LONG_MIN, depending on the sign. */ |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
617 exp = PyLong_AsLong(oexp); |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
618 if (exp == -1 && PyErr_Occurred()) { |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
619 if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
620 if (Py_SIZE(oexp) < 0) { |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
621 exp = LONG_MIN; |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
622 } |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
623 else { |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
624 exp = LONG_MAX; |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
625 } |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
626 PyErr_Clear(); |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
627 } |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
628 else { |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
629 /* propagate any unexpected exception */ |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
630 return NULL; |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
631 } |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
632 } |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
633 } |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
634 else if (PyInt_Check(oexp)) { |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
635 exp = PyInt_AS_LONG(oexp); |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
636 } |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
637 else { |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
638 PyErr_SetString(PyExc_TypeError, |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
639 "Expected an int or long as second argument " |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
640 "to ldexp."); |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
641 return NULL; |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
642 } |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
643 |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
644 if (x == 0. || !Py_IS_FINITE(x)) { |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
645 /* NaNs, zeros and infinities are returned unchanged */ |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
646 r = x; |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
647 errno = 0; |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
648 } else if (exp > INT_MAX) { |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
649 /* overflow */ |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
650 r = copysign(Py_HUGE_VAL, x); |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
651 errno = ERANGE; |
|
46797
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
652 } else if (exp < INT_MIN) { |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
653 /* underflow to +-0 */ |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
654 r = copysign(0., x); |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
655 errno = 0; |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
656 } else { |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
657 errno = 0; |
|
46797
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
658 PyFPE_START_PROTECT("in math_ldexp", return 0); |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
659 r = ldexp(x, (int)exp); |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
660 PyFPE_END_PROTECT(r); |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
661 if (Py_IS_INFINITY(r)) |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
662 errno = ERANGE; |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
663 } |
|
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
664 |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
665 if (errno && is_error(r)) |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
14616
diff
changeset
|
666 return NULL; |
|
46797
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
667 return PyFloat_FromDouble(r); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
668 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
669 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
670 PyDoc_STRVAR(math_ldexp_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
671 "ldexp(x, i) -> x * (2**i)"); |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
672 |
| 4635 | 673 static PyObject * |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
674 math_modf(PyObject *self, PyObject *arg) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
675 { |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
676 double y, x = PyFloat_AsDouble(arg); |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
677 if (x == -1.0 && PyErr_Occurred()) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
678 return NULL; |
|
46499
80b16595e462
FreeBSD doesn't follow C99 for modf(inf); so add explicit
Mark Dickinson <dickinsm@gmail.com>
parents:
46490
diff
changeset
|
679 /* some platforms don't do the right thing for NaNs and |
|
80b16595e462
FreeBSD doesn't follow C99 for modf(inf); so add explicit
Mark Dickinson <dickinsm@gmail.com>
parents:
46490
diff
changeset
|
680 infinities, so we take care of special cases directly. */ |
|
80b16595e462
FreeBSD doesn't follow C99 for modf(inf); so add explicit
Mark Dickinson <dickinsm@gmail.com>
parents:
46490
diff
changeset
|
681 if (!Py_IS_FINITE(x)) { |
|
80b16595e462
FreeBSD doesn't follow C99 for modf(inf); so add explicit
Mark Dickinson <dickinsm@gmail.com>
parents:
46490
diff
changeset
|
682 if (Py_IS_INFINITY(x)) |
|
80b16595e462
FreeBSD doesn't follow C99 for modf(inf); so add explicit
Mark Dickinson <dickinsm@gmail.com>
parents:
46490
diff
changeset
|
683 return Py_BuildValue("(dd)", copysign(0., x), x); |
|
80b16595e462
FreeBSD doesn't follow C99 for modf(inf); so add explicit
Mark Dickinson <dickinsm@gmail.com>
parents:
46490
diff
changeset
|
684 else if (Py_IS_NAN(x)) |
|
80b16595e462
FreeBSD doesn't follow C99 for modf(inf); so add explicit
Mark Dickinson <dickinsm@gmail.com>
parents:
46490
diff
changeset
|
685 return Py_BuildValue("(dd)", x, x); |
|
80b16595e462
FreeBSD doesn't follow C99 for modf(inf); so add explicit
Mark Dickinson <dickinsm@gmail.com>
parents:
46490
diff
changeset
|
686 } |
|
80b16595e462
FreeBSD doesn't follow C99 for modf(inf); so add explicit
Mark Dickinson <dickinsm@gmail.com>
parents:
46490
diff
changeset
|
687 |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
688 errno = 0; |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
689 PyFPE_START_PROTECT("in math_modf", return 0); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
690 x = modf(x, &y); |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
691 PyFPE_END_PROTECT(x); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
692 return Py_BuildValue("(dd)", x, y); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
693 } |
| 6 | 694 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
695 PyDoc_STRVAR(math_modf_doc, |
|
19712
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
696 "modf(x)\n" |
|
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
697 "\n" |
|
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
698 "Return the fractional and integer parts of x. Both results carry the sign\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
699 "of x. The integer part is returned as a real."); |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
700 |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
701 /* A decent logarithm is easy to compute even for huge longs, but libm can't |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
702 do that by itself -- loghelper can. func is log or log10, and name is |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
703 "log" or "log10". Note that overflow isn't possible: a long can contain |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
704 no more than INT_MAX * SHIFT bits, so has value certainly less than |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
705 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
706 small enough to fit in an IEEE single. log and log10 are even smaller. |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
707 */ |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
708 |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
709 static PyObject* |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
710 loghelper(PyObject* arg, double (*func)(double), char *funcname) |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
711 { |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
712 /* If it is long, do it ourselves. */ |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
713 if (PyLong_Check(arg)) { |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
714 double x; |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
715 int e; |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
716 x = _PyLong_AsScaledDouble(arg, &e); |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
717 if (x <= 0.0) { |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
718 PyErr_SetString(PyExc_ValueError, |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
719 "math domain error"); |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
720 return NULL; |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
721 } |
|
44947
8e435bdd4bc8
setup.py doesn't pick up changes to a header file
Christian Heimes <christian@cheimes.de>
parents:
44940
diff
changeset
|
722 /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~= |
|
8e435bdd4bc8
setup.py doesn't pick up changes to a header file
Christian Heimes <christian@cheimes.de>
parents:
44940
diff
changeset
|
723 log(x) + log(2) * e * PyLong_SHIFT. |
|
8e435bdd4bc8
setup.py doesn't pick up changes to a header file
Christian Heimes <christian@cheimes.de>
parents:
44940
diff
changeset
|
724 CAUTION: e*PyLong_SHIFT may overflow using int arithmetic, |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
725 so force use of double. */ |
|
44947
8e435bdd4bc8
setup.py doesn't pick up changes to a header file
Christian Heimes <christian@cheimes.de>
parents:
44940
diff
changeset
|
726 x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0); |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
727 return PyFloat_FromDouble(x); |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
728 } |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
729 |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
730 /* Else let libm handle it by itself. */ |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
731 return math_1(arg, func, 0); |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
732 } |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
733 |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
734 static PyObject * |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
735 math_log(PyObject *self, PyObject *args) |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
736 { |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
737 PyObject *arg; |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
738 PyObject *base = NULL; |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
739 PyObject *num, *den; |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
740 PyObject *ans; |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
741 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26661
diff
changeset
|
742 if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base)) |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
743 return NULL; |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
744 |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
745 num = loghelper(arg, log, "log"); |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
746 if (num == NULL || base == NULL) |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
747 return num; |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
748 |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
749 den = loghelper(base, log, "log"); |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
750 if (den == NULL) { |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
751 Py_DECREF(num); |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
752 return NULL; |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
753 } |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
754 |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
755 ans = PyNumber_Divide(num, den); |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
756 Py_DECREF(num); |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
757 Py_DECREF(den); |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
758 return ans; |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
759 } |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
760 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
761 PyDoc_STRVAR(math_log_doc, |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
762 "log(x[, base]) -> the logarithm of x to the given base.\n\ |
|
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
763 If the base not specified, returns the natural logarithm (base e) of x."); |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
764 |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
765 static PyObject * |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
766 math_log10(PyObject *self, PyObject *arg) |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
767 { |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
768 return loghelper(arg, log10, "log10"); |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
769 } |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
770 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
771 PyDoc_STRVAR(math_log10_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
772 "log10(x) -> the base 10 logarithm of x."); |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
773 |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
774 static PyObject * |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
775 math_fmod(PyObject *self, PyObject *args) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
776 { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
777 PyObject *ox, *oy; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
778 double r, x, y; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
779 if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
780 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
781 x = PyFloat_AsDouble(ox); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
782 y = PyFloat_AsDouble(oy); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
783 if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
784 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
785 /* fmod(x, +/-Inf) returns x for finite x. */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
786 if (Py_IS_INFINITY(y) && Py_IS_FINITE(x)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
787 return PyFloat_FromDouble(x); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
788 errno = 0; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
789 PyFPE_START_PROTECT("in math_fmod", return 0); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
790 r = fmod(x, y); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
791 PyFPE_END_PROTECT(r); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
792 if (Py_IS_NAN(r)) { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
793 if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
794 errno = EDOM; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
795 else |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
796 errno = 0; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
797 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
798 if (errno && is_error(r)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
799 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
800 else |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
801 return PyFloat_FromDouble(r); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
802 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
803 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
804 PyDoc_STRVAR(math_fmod_doc, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
805 "fmod(x,y)\n\nReturn fmod(x, y), according to platform C." |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
806 " x % y may differ."); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
807 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
808 static PyObject * |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
809 math_hypot(PyObject *self, PyObject *args) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
810 { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
811 PyObject *ox, *oy; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
812 double r, x, y; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
813 if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
814 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
815 x = PyFloat_AsDouble(ox); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
816 y = PyFloat_AsDouble(oy); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
817 if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
818 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
819 /* hypot(x, +/-Inf) returns Inf, even if x is a NaN. */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
820 if (Py_IS_INFINITY(x)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
821 return PyFloat_FromDouble(fabs(x)); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
822 if (Py_IS_INFINITY(y)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
823 return PyFloat_FromDouble(fabs(y)); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
824 errno = 0; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
825 PyFPE_START_PROTECT("in math_hypot", return 0); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
826 r = hypot(x, y); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
827 PyFPE_END_PROTECT(r); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
828 if (Py_IS_NAN(r)) { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
829 if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
830 errno = EDOM; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
831 else |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
832 errno = 0; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
833 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
834 else if (Py_IS_INFINITY(r)) { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
835 if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
836 errno = ERANGE; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
837 else |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
838 errno = 0; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
839 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
840 if (errno && is_error(r)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
841 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
842 else |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
843 return PyFloat_FromDouble(r); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
844 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
845 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
846 PyDoc_STRVAR(math_hypot_doc, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
847 "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y)."); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
848 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
849 /* pow can't use math_2, but needs its own wrapper: the problem is |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
850 that an infinite result can arise either as a result of overflow |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
851 (in which case OverflowError should be raised) or as a result of |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
852 e.g. 0.**-5. (for which ValueError needs to be raised.) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
853 */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
854 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
855 static PyObject * |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
856 math_pow(PyObject *self, PyObject *args) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
857 { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
858 PyObject *ox, *oy; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
859 double r, x, y; |
|
46500
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
860 int odd_y; |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
861 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
862 if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
863 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
864 x = PyFloat_AsDouble(ox); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
865 y = PyFloat_AsDouble(oy); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
866 if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
867 return NULL; |
|
46490
648b4d01cccf
Additional special-case handling for math.pow.
Mark Dickinson <dickinsm@gmail.com>
parents:
46486
diff
changeset
|
868 |
|
46500
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
869 /* deal directly with IEEE specials, to cope with problems on various |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
870 platforms whose semantics don't exactly match C99 */ |
|
46509
052fd8f1ca37
Silence 'r may be used uninitialized' compiler warning.
Mark Dickinson <dickinsm@gmail.com>
parents:
46507
diff
changeset
|
871 r = 0.; /* silence compiler warning */ |
|
46500
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
872 if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) { |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
873 errno = 0; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
874 if (Py_IS_NAN(x)) |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
875 r = y == 0. ? 1. : x; /* NaN**0 = 1 */ |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
876 else if (Py_IS_NAN(y)) |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
877 r = x == 1. ? 1. : y; /* 1**NaN = 1 */ |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
878 else if (Py_IS_INFINITY(x)) { |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
879 odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
880 if (y > 0.) |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
881 r = odd_y ? x : fabs(x); |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
882 else if (y == 0.) |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
883 r = 1.; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
884 else /* y < 0. */ |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
885 r = odd_y ? copysign(0., x) : 0.; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
886 } |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
887 else if (Py_IS_INFINITY(y)) { |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
888 if (fabs(x) == 1.0) |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
889 r = 1.; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
890 else if (y > 0. && fabs(x) > 1.0) |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
891 r = y; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
892 else if (y < 0. && fabs(x) < 1.0) { |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
893 r = -y; /* result is +inf */ |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
894 if (x == 0.) /* 0**-inf: divide-by-zero */ |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
895 errno = EDOM; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
896 } |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
897 else |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
898 r = 0.; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
899 } |
|
46486
82d85a05923b
Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents:
46473
diff
changeset
|
900 } |
|
46500
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
901 else { |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
902 /* let libm handle finite**finite */ |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
903 errno = 0; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
904 PyFPE_START_PROTECT("in math_pow", return 0); |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
905 r = pow(x, y); |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
906 PyFPE_END_PROTECT(r); |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
907 /* a NaN result should arise only from (-ve)**(finite |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
908 non-integer); in this case we want to raise ValueError. */ |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
909 if (!Py_IS_FINITE(r)) { |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
910 if (Py_IS_NAN(r)) { |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
911 errno = EDOM; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
912 } |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
913 /* |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
914 an infinite result here arises either from: |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
915 (A) (+/-0.)**negative (-> divide-by-zero) |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
916 (B) overflow of x**y with x and y finite |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
917 */ |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
918 else if (Py_IS_INFINITY(r)) { |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
919 if (x == 0.) |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
920 errno = EDOM; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
921 else |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
922 errno = ERANGE; |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
923 } |
|
a20def0101de
Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents:
46499
diff
changeset
|
924 } |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
925 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
926 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
927 if (errno && is_error(r)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
928 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
929 else |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
930 return PyFloat_FromDouble(r); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
931 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
932 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
933 PyDoc_STRVAR(math_pow_doc, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
934 "pow(x,y)\n\nReturn x**y (x to the power of y)."); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
935 |
|
44437
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
936 static const double degToRad = Py_MATH_PI / 180.0; |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
937 static const double radToDeg = 180.0 / Py_MATH_PI; |
|
23427
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
938 |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
939 static PyObject * |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
940 math_degrees(PyObject *self, PyObject *arg) |
|
23427
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
941 { |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
942 double x = PyFloat_AsDouble(arg); |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
943 if (x == -1.0 && PyErr_Occurred()) |
|
23427
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
944 return NULL; |
|
44437
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
945 return PyFloat_FromDouble(x * radToDeg); |
|
23427
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
946 } |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
947 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
948 PyDoc_STRVAR(math_degrees_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
949 "degrees(x) -> converts angle x from radians to degrees"); |
|
23427
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
950 |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
951 static PyObject * |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
952 math_radians(PyObject *self, PyObject *arg) |
|
23427
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
953 { |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
954 double x = PyFloat_AsDouble(arg); |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
955 if (x == -1.0 && PyErr_Occurred()) |
|
23427
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
956 return NULL; |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
957 return PyFloat_FromDouble(x * degToRad); |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
958 } |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
959 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
960 PyDoc_STRVAR(math_radians_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
961 "radians(x) -> converts angle x from degrees to radians"); |
|
19713
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
962 |
|
44437
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
963 static PyObject * |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
964 math_isnan(PyObject *self, PyObject *arg) |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
965 { |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
966 double x = PyFloat_AsDouble(arg); |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
967 if (x == -1.0 && PyErr_Occurred()) |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
968 return NULL; |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
969 return PyBool_FromLong((long)Py_IS_NAN(x)); |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
970 } |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
971 |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
972 PyDoc_STRVAR(math_isnan_doc, |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
973 "isnan(x) -> bool\n\ |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
974 Checks if float x is not a number (NaN)"); |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
975 |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
976 static PyObject * |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
977 math_isinf(PyObject *self, PyObject *arg) |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
978 { |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
979 double x = PyFloat_AsDouble(arg); |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
980 if (x == -1.0 && PyErr_Occurred()) |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
981 return NULL; |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
982 return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
983 } |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
984 |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
985 PyDoc_STRVAR(math_isinf_doc, |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
986 "isinf(x) -> bool\n\ |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
987 Checks if float x is infinite (positive or negative)"); |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
988 |
| 4635 | 989 static PyMethodDef math_methods[] = { |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
990 {"acos", math_acos, METH_O, math_acos_doc}, |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
991 {"acosh", math_acosh, METH_O, math_acosh_doc}, |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
992 {"asin", math_asin, METH_O, math_asin_doc}, |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
993 {"asinh", math_asinh, METH_O, math_asinh_doc}, |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
994 {"atan", math_atan, METH_O, math_atan_doc}, |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
995 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
996 {"atanh", math_atanh, METH_O, math_atanh_doc}, |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
997 {"ceil", math_ceil, METH_O, math_ceil_doc}, |
|
44440
6208b17d0dbe
Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents:
44437
diff
changeset
|
998 {"copysign", math_copysign, METH_VARARGS, math_copysign_doc}, |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
999 {"cos", math_cos, METH_O, math_cos_doc}, |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1000 {"cosh", math_cosh, METH_O, math_cosh_doc}, |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1001 {"degrees", math_degrees, METH_O, math_degrees_doc}, |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1002 {"exp", math_exp, METH_O, math_exp_doc}, |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1003 {"fabs", math_fabs, METH_O, math_fabs_doc}, |
|
47529
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1004 {"factorial", math_factorial, METH_O, math_factorial_doc}, |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1005 {"floor", math_floor, METH_O, math_floor_doc}, |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
1006 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1007 {"frexp", math_frexp, METH_O, math_frexp_doc}, |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
1008 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
|
44437
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1009 {"isinf", math_isinf, METH_O, math_isinf_doc}, |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1010 {"isnan", math_isnan, METH_O, math_isnan_doc}, |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
1011 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
|
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
1012 {"log", math_log, METH_VARARGS, math_log_doc}, |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1013 {"log1p", math_log1p, METH_O, math_log1p_doc}, |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1014 {"log10", math_log10, METH_O, math_log10_doc}, |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1015 {"modf", math_modf, METH_O, math_modf_doc}, |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
1016 {"pow", math_pow, METH_VARARGS, math_pow_doc}, |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1017 {"radians", math_radians, METH_O, math_radians_doc}, |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1018 {"sin", math_sin, METH_O, math_sin_doc}, |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1019 {"sinh", math_sinh, METH_O, math_sinh_doc}, |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1020 {"sqrt", math_sqrt, METH_O, math_sqrt_doc}, |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
1021 {"sum", math_sum, METH_O, math_sum_doc}, |
|
40362
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1022 {"tan", math_tan, METH_O, math_tan_doc}, |
|
271133981e63
Speed up function calls into the math module by using METH_O.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38491
diff
changeset
|
1023 {"tanh", math_tanh, METH_O, math_tanh_doc}, |
|
45079
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
1024 {"trunc", math_trunc, METH_O, math_trunc_doc}, |
| 6 | 1025 {NULL, NULL} /* sentinel */ |
| 1026 }; | |
| 1027 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
1028 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
1029 PyDoc_STRVAR(module_doc, |
|
19712
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
1030 "This module is always available. It provides access to the\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
1031 "mathematical functions defined by the C standard."); |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
1032 |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
23832
diff
changeset
|
1033 PyMODINIT_FUNC |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13331
diff
changeset
|
1034 initmath(void) |
| 6 | 1035 { |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1036 PyObject *m; |
|
19048
e282d6b2b758
Part of SF patch [#431848] mathmodule.c: doc strings & conversion, from
Tim Peters <tim.peters@gmail.com>
parents:
18066
diff
changeset
|
1037 |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
1038 m = Py_InitModule3("math", math_methods, module_doc); |
|
36324
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
34872
diff
changeset
|
1039 if (m == NULL) |
|
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
34872
diff
changeset
|
1040 goto finally; |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
1041 |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1042 PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1043 PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
1044 |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1045 finally: |
|
14346
5397fdf88ec2
Do the absolute minimal amount of modifications to eradicate
Barry Warsaw <barry@python.org>
parents:
13912
diff
changeset
|
1046 return; |
| 6 | 1047 } |
