Mercurial > cpython-lru_cache-weakref
annotate Modules/mathmodule.c @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | b36cb4602e21 |
| children | f543863f4e43 |
| 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" |
|
57756
b206ad8cf5bf
Issue #3366: Add expm1 function to math module. Thanks Eric Smith for
Mark Dickinson <dickinsm@gmail.com>
parents:
57655
diff
changeset
|
56 #include "_math.h" |
| 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 |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
63 /* |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
64 sin(pi*x), giving accurate results for all finite x (especially x |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
65 integral or close to an integer). This is here for use in the |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
66 reflection formula for the gamma function. It conforms to IEEE |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
67 754-2008 for finite arguments, but not for infinities or nans. |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
68 */ |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
69 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
70 static const double pi = 3.141592653589793238462643383279502884197; |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
71 static const double sqrtpi = 1.772453850905516027298167483341145182798; |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
72 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
73 static double |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
74 sinpi(double x) |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
75 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
76 double y, r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
77 int n; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
78 /* this function should only ever be called for finite arguments */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
79 assert(Py_IS_FINITE(x)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
80 y = fmod(fabs(x), 2.0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
81 n = (int)round(2.0*y); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
82 assert(0 <= n && n <= 4); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
83 switch (n) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
84 case 0: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
85 r = sin(pi*y); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
86 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
87 case 1: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
88 r = cos(pi*(y-0.5)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
89 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
90 case 2: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
91 /* N.B. -sin(pi*(y-1.0)) is *not* equivalent: it would give |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
92 -0.0 instead of 0.0 when y == 1.0. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
93 r = sin(pi*(1.0-y)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
94 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
95 case 3: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
96 r = -cos(pi*(y-1.5)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
97 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
98 case 4: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
99 r = sin(pi*(y-2.0)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
100 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
101 default: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
102 assert(0); /* should never get here */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
103 r = -1.23e200; /* silence gcc warning */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
104 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
105 return copysign(1.0, x)*r; |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
106 } |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
107 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
108 /* Implementation of the real gamma function. In extensive but non-exhaustive |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
109 random tests, this function proved accurate to within <= 10 ulps across the |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
110 entire float domain. Note that accuracy may depend on the quality of the |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
111 system math functions, the pow function in particular. Special cases |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
112 follow C99 annex F. The parameters and method are tailored to platforms |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
113 whose double format is the IEEE 754 binary64 format. |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
114 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
115 Method: for x > 0.0 we use the Lanczos approximation with parameters N=13 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
116 and g=6.024680040776729583740234375; these parameters are amongst those |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
117 used by the Boost library. Following Boost (again), we re-express the |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
118 Lanczos sum as a rational function, and compute it that way. The |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
119 coefficients below were computed independently using MPFR, and have been |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
120 double-checked against the coefficients in the Boost source code. |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
121 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
122 For x < 0.0 we use the reflection formula. |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
123 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
124 There's one minor tweak that deserves explanation: Lanczos' formula for |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
125 Gamma(x) involves computing pow(x+g-0.5, x-0.5) / exp(x+g-0.5). For many x |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
126 values, x+g-0.5 can be represented exactly. However, in cases where it |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
127 can't be represented exactly the small error in x+g-0.5 can be magnified |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
128 significantly by the pow and exp calls, especially for large x. A cheap |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
129 correction is to multiply by (1 + e*g/(x+g-0.5)), where e is the error |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
130 involved in the computation of x+g-0.5 (that is, e = computed value of |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
131 x+g-0.5 - exact value of x+g-0.5). Here's the proof: |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
132 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
133 Correction factor |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
134 ----------------- |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
135 Write x+g-0.5 = y-e, where y is exactly representable as an IEEE 754 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
136 double, and e is tiny. Then: |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
137 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
138 pow(x+g-0.5,x-0.5)/exp(x+g-0.5) = pow(y-e, x-0.5)/exp(y-e) |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
139 = pow(y, x-0.5)/exp(y) * C, |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
140 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
141 where the correction_factor C is given by |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
142 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
143 C = pow(1-e/y, x-0.5) * exp(e) |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
144 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
145 Since e is tiny, pow(1-e/y, x-0.5) ~ 1-(x-0.5)*e/y, and exp(x) ~ 1+e, so: |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
146 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
147 C ~ (1-(x-0.5)*e/y) * (1+e) ~ 1 + e*(y-(x-0.5))/y |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
148 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
149 But y-(x-0.5) = g+e, and g+e ~ g. So we get C ~ 1 + e*g/y, and |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
150 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
151 pow(x+g-0.5,x-0.5)/exp(x+g-0.5) ~ pow(y, x-0.5)/exp(y) * (1 + e*g/y), |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
152 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
153 Note that for accuracy, when computing r*C it's better to do |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
154 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
155 r + e*g/y*r; |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
156 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
157 than |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
158 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
159 r * (1 + e*g/y); |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
160 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
161 since the addition in the latter throws away most of the bits of |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
162 information in e*g/y. |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
163 */ |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
164 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
165 #define LANCZOS_N 13 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
166 static const double lanczos_g = 6.024680040776729583740234375; |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
167 static const double lanczos_g_minus_half = 5.524680040776729583740234375; |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
168 static const double lanczos_num_coeffs[LANCZOS_N] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
169 23531376880.410759688572007674451636754734846804940, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
170 42919803642.649098768957899047001988850926355848959, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
171 35711959237.355668049440185451547166705960488635843, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
172 17921034426.037209699919755754458931112671403265390, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
173 6039542586.3520280050642916443072979210699388420708, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
174 1439720407.3117216736632230727949123939715485786772, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
175 248874557.86205415651146038641322942321632125127801, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
176 31426415.585400194380614231628318205362874684987640, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
177 2876370.6289353724412254090516208496135991145378768, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
178 186056.26539522349504029498971604569928220784236328, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
179 8071.6720023658162106380029022722506138218516325024, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
180 210.82427775157934587250973392071336271166969580291, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
181 2.5066282746310002701649081771338373386264310793408 |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
182 }; |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
183 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
184 /* denominator is x*(x+1)*...*(x+LANCZOS_N-2) */ |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
185 static const double lanczos_den_coeffs[LANCZOS_N] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
186 0.0, 39916800.0, 120543840.0, 150917976.0, 105258076.0, 45995730.0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
187 13339535.0, 2637558.0, 357423.0, 32670.0, 1925.0, 66.0, 1.0}; |
|
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
|
188 |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
189 /* gamma values for small positive integers, 1 though NGAMMA_INTEGRAL */ |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
190 #define NGAMMA_INTEGRAL 23 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
191 static const double gamma_integral[NGAMMA_INTEGRAL] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
192 1.0, 1.0, 2.0, 6.0, 24.0, 120.0, 720.0, 5040.0, 40320.0, 362880.0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
193 3628800.0, 39916800.0, 479001600.0, 6227020800.0, 87178291200.0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
194 1307674368000.0, 20922789888000.0, 355687428096000.0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
195 6402373705728000.0, 121645100408832000.0, 2432902008176640000.0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
196 51090942171709440000.0, 1124000727777607680000.0, |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
197 }; |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
198 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
199 /* Lanczos' sum L_g(x), for positive x */ |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
200 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
201 static double |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
202 lanczos_sum(double x) |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
203 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
204 double num = 0.0, den = 0.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
205 int i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
206 assert(x > 0.0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
207 /* evaluate the rational function lanczos_sum(x). For large |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
208 x, the obvious algorithm risks overflow, so we instead |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
209 rescale the denominator and numerator of the rational |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
210 function by x**(1-LANCZOS_N) and treat this as a |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
211 rational function in 1/x. This also reduces the error for |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
212 larger x values. The choice of cutoff point (5.0 below) is |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
213 somewhat arbitrary; in tests, smaller cutoff values than |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
214 this resulted in lower accuracy. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
215 if (x < 5.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
216 for (i = LANCZOS_N; --i >= 0; ) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
217 num = num * x + lanczos_num_coeffs[i]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
218 den = den * x + lanczos_den_coeffs[i]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
219 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
220 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
221 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
222 for (i = 0; i < LANCZOS_N; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
223 num = num / x + lanczos_num_coeffs[i]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
224 den = den / x + lanczos_den_coeffs[i]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
225 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
226 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
227 return num/den; |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
228 } |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
229 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
230 static double |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
231 m_tgamma(double x) |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
232 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
233 double absx, r, y, z, sqrtpow; |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
234 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
235 /* special cases */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
236 if (!Py_IS_FINITE(x)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
237 if (Py_IS_NAN(x) || x > 0.0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
238 return x; /* tgamma(nan) = nan, tgamma(inf) = inf */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
239 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
240 errno = EDOM; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
241 return Py_NAN; /* tgamma(-inf) = nan, invalid */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
242 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
243 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
244 if (x == 0.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
245 errno = EDOM; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
246 return 1.0/x; /* tgamma(+-0.0) = +-inf, divide-by-zero */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
247 } |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
248 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
249 /* integer arguments */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
250 if (x == floor(x)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
251 if (x < 0.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
252 errno = EDOM; /* tgamma(n) = nan, invalid for */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
253 return Py_NAN; /* negative integers n */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
254 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
255 if (x <= NGAMMA_INTEGRAL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
256 return gamma_integral[(int)x - 1]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
257 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
258 absx = fabs(x); |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
259 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
260 /* tiny arguments: tgamma(x) ~ 1/x for x near 0 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
261 if (absx < 1e-20) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
262 r = 1.0/x; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
263 if (Py_IS_INFINITY(r)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
264 errno = ERANGE; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
265 return r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
266 } |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
267 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
268 /* large arguments: assuming IEEE 754 doubles, tgamma(x) overflows for |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
269 x > 200, and underflows to +-0.0 for x < -200, not a negative |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
270 integer. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
271 if (absx > 200.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
272 if (x < 0.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
273 return 0.0/sinpi(x); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
274 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
275 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
276 errno = ERANGE; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
277 return Py_HUGE_VAL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
278 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
279 } |
|
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
|
280 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
281 y = absx + lanczos_g_minus_half; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
282 /* compute error in sum */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
283 if (absx > lanczos_g_minus_half) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
284 /* note: the correction can be foiled by an optimizing |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
285 compiler that (incorrectly) thinks that an expression like |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
286 a + b - a - b can be optimized to 0.0. This shouldn't |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
287 happen in a standards-conforming compiler. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
288 double q = y - absx; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
289 z = q - lanczos_g_minus_half; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
290 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
291 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
292 double q = y - lanczos_g_minus_half; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
293 z = q - absx; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
294 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
295 z = z * lanczos_g / y; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
296 if (x < 0.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
297 r = -pi / sinpi(absx) / absx * exp(y) / lanczos_sum(absx); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
298 r -= z * r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
299 if (absx < 140.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
300 r /= pow(y, absx - 0.5); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
301 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
302 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
303 sqrtpow = pow(y, absx / 2.0 - 0.25); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
304 r /= sqrtpow; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
305 r /= sqrtpow; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
306 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
307 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
308 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
309 r = lanczos_sum(absx) / exp(y); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
310 r += z * r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
311 if (absx < 140.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
312 r *= pow(y, absx - 0.5); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
313 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
314 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
315 sqrtpow = pow(y, absx / 2.0 - 0.25); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
316 r *= sqrtpow; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
317 r *= sqrtpow; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
318 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
319 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
320 if (Py_IS_INFINITY(r)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
321 errno = ERANGE; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
322 return r; |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
323 } |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
324 |
|
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
|
325 /* |
|
57655
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
326 lgamma: natural log of the absolute value of the Gamma function. |
|
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
327 For large arguments, Lanczos' formula works extremely well here. |
|
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
328 */ |
|
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
329 |
|
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
330 static double |
|
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
331 m_lgamma(double x) |
|
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
332 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
333 double r, absx; |
|
57655
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
334 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
335 /* special cases */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
336 if (!Py_IS_FINITE(x)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
337 if (Py_IS_NAN(x)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
338 return x; /* lgamma(nan) = nan */ |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
339 else |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
340 return Py_HUGE_VAL; /* lgamma(+-inf) = +inf */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
341 } |
|
57655
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
342 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
343 /* integer arguments */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
344 if (x == floor(x) && x <= 2.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
345 if (x <= 0.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
346 errno = EDOM; /* lgamma(n) = inf, divide-by-zero for */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
347 return Py_HUGE_VAL; /* integers n <= 0 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
348 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
349 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
350 return 0.0; /* lgamma(1) = lgamma(2) = 0.0 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
351 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
352 } |
|
57655
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
353 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
354 absx = fabs(x); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
355 /* tiny arguments: lgamma(x) ~ -log(fabs(x)) for small x */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
356 if (absx < 1e-20) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
357 return -log(absx); |
|
57655
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
358 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
359 /* Lanczos' formula */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
360 if (x > 0.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
361 /* we could save a fraction of a ulp in accuracy by having a |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
362 second set of numerator coefficients for lanczos_sum that |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
363 absorbed the exp(-lanczos_g) term, and throwing out the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
364 lanczos_g subtraction below; it's probably not worth it. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
365 r = log(lanczos_sum(x)) - lanczos_g + |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
366 (x-0.5)*(log(x+lanczos_g-0.5)-1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
367 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
368 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
369 r = log(pi) - log(fabs(sinpi(absx))) - log(absx) - |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
370 (log(lanczos_sum(absx)) - lanczos_g + |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
371 (absx-0.5)*(log(absx+lanczos_g-0.5)-1)); |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
372 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
373 if (Py_IS_INFINITY(r)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
374 errno = ERANGE; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
375 return r; |
|
57655
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
376 } |
|
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
377 |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
378 /* |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
379 Implementations of the error function erf(x) and the complementary error |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
380 function erfc(x). |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
381 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
382 Method: following 'Numerical Recipes' by Flannery, Press et. al. (2nd ed., |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
383 Cambridge University Press), we use a series approximation for erf for |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
384 small x, and a continued fraction approximation for erfc(x) for larger x; |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
385 combined with the relations erf(-x) = -erf(x) and erfc(x) = 1.0 - erf(x), |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
386 this gives us erf(x) and erfc(x) for all x. |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
387 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
388 The series expansion used is: |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
389 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
390 erf(x) = x*exp(-x*x)/sqrt(pi) * [ |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
391 2/1 + 4/3 x**2 + 8/15 x**4 + 16/105 x**6 + ...] |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
392 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
393 The coefficient of x**(2k-2) here is 4**k*factorial(k)/factorial(2*k). |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
394 This series converges well for smallish x, but slowly for larger x. |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
395 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
396 The continued fraction expansion used is: |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
397 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
398 erfc(x) = x*exp(-x*x)/sqrt(pi) * [1/(0.5 + x**2 -) 0.5/(2.5 + x**2 - ) |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
399 3.0/(4.5 + x**2 - ) 7.5/(6.5 + x**2 - ) ...] |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
400 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
401 after the first term, the general term has the form: |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
402 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
403 k*(k-0.5)/(2*k+0.5 + x**2 - ...). |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
404 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
405 This expansion converges fast for larger x, but convergence becomes |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
406 infinitely slow as x approaches 0.0. The (somewhat naive) continued |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
407 fraction evaluation algorithm used below also risks overflow for large x; |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
408 but for large x, erfc(x) == 0.0 to within machine precision. (For |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
409 example, erfc(30.0) is approximately 2.56e-393). |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
410 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
411 Parameters: use series expansion for abs(x) < ERF_SERIES_CUTOFF and |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
412 continued fraction expansion for ERF_SERIES_CUTOFF <= abs(x) < |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
413 ERFC_CONTFRAC_CUTOFF. ERFC_SERIES_TERMS and ERFC_CONTFRAC_TERMS are the |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
414 numbers of terms to use for the relevant expansions. */ |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
415 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
416 #define ERF_SERIES_CUTOFF 1.5 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
417 #define ERF_SERIES_TERMS 25 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
418 #define ERFC_CONTFRAC_CUTOFF 30.0 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
419 #define ERFC_CONTFRAC_TERMS 50 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
420 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
421 /* |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
422 Error function, via power series. |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
423 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
424 Given a finite float x, return an approximation to erf(x). |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
425 Converges reasonably fast for small x. |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
426 */ |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
427 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
428 static double |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
429 m_erf_series(double x) |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
430 { |
|
62316
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
431 double x2, acc, fk, result; |
|
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
432 int i, saved_errno; |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
433 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
434 x2 = x * x; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
435 acc = 0.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
436 fk = (double)ERF_SERIES_TERMS + 0.5; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
437 for (i = 0; i < ERF_SERIES_TERMS; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
438 acc = 2.0 + x2 * acc / fk; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
439 fk -= 1.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
440 } |
|
62316
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
441 /* Make sure the exp call doesn't affect errno; |
|
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
442 see m_erfc_contfrac for more. */ |
|
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
443 saved_errno = errno; |
|
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
444 result = acc * x * exp(-x2) / sqrtpi; |
|
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
445 errno = saved_errno; |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
446 return result; |
|
498
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
447 } |
|
56bb1067a880
Improve error handling.
Guido van Rossum <guido@python.org>
parents:
438
diff
changeset
|
448 |
|
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
|
449 /* |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
450 Complementary error function, via continued fraction expansion. |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
451 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
452 Given a positive float x, return an approximation to erfc(x). Converges |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
453 reasonably fast for x large (say, x > 2.0), and should be safe from |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
454 overflow if x and nterms are not too large. On an IEEE 754 machine, with x |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
455 <= 30.0, we're safe up to nterms = 100. For x >= 30.0, erfc(x) is smaller |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
456 than the smallest representable nonzero float. */ |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
457 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
458 static double |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
459 m_erfc_contfrac(double x) |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
460 { |
|
62316
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
461 double x2, a, da, p, p_last, q, q_last, b, result; |
|
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
462 int i, saved_errno; |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
463 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
464 if (x >= ERFC_CONTFRAC_CUTOFF) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
465 return 0.0; |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
466 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
467 x2 = x*x; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
468 a = 0.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
469 da = 0.5; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
470 p = 1.0; p_last = 0.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
471 q = da + x2; q_last = 1.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
472 for (i = 0; i < ERFC_CONTFRAC_TERMS; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
473 double temp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
474 a += da; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
475 da += 2.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
476 b = da + x2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
477 temp = p; p = b*p - a*p_last; p_last = temp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
478 temp = q; q = b*q - a*q_last; q_last = temp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
479 } |
|
62316
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
480 /* Issue #8986: On some platforms, exp sets errno on underflow to zero; |
|
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
481 save the current errno value so that we can restore it later. */ |
|
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
482 saved_errno = errno; |
|
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
483 result = p / q * x * exp(-x2) / sqrtpi; |
|
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
484 errno = saved_errno; |
|
cff0a923140b
Issue #8986: erfc was raising OverflowError on Linux for arguments in
Mark Dickinson <dickinsm@gmail.com>
parents:
61463
diff
changeset
|
485 return result; |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
486 } |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
487 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
488 /* Error function erf(x), for general x */ |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
489 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
490 static double |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
491 m_erf(double x) |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
492 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
493 double absx, cf; |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
494 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
495 if (Py_IS_NAN(x)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
496 return x; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
497 absx = fabs(x); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
498 if (absx < ERF_SERIES_CUTOFF) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
499 return m_erf_series(x); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
500 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
501 cf = m_erfc_contfrac(absx); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
502 return x > 0.0 ? 1.0 - cf : cf - 1.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
503 } |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
504 } |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
505 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
506 /* Complementary error function erfc(x), for general x. */ |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
507 |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
508 static double |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
509 m_erfc(double x) |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
510 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
511 double absx, cf; |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
512 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
513 if (Py_IS_NAN(x)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
514 return x; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
515 absx = fabs(x); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
516 if (absx < ERF_SERIES_CUTOFF) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
517 return 1.0 - m_erf_series(x); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
518 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
519 cf = m_erfc_contfrac(absx); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
520 return x > 0.0 ? cf : 2.0 - cf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
521 } |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
522 } |
|
57655
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
523 |
|
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
524 /* |
|
46507
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
525 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
|
526 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
|
527 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
|
528 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
|
529 always follow C99. |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
530 */ |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
531 |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
532 static double |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
533 m_atan2(double y, double x) |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
534 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
535 if (Py_IS_NAN(x) || Py_IS_NAN(y)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
536 return Py_NAN; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
537 if (Py_IS_INFINITY(y)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
538 if (Py_IS_INFINITY(x)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
539 if (copysign(1., x) == 1.) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
540 /* atan2(+-inf, +inf) == +-pi/4 */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
541 return copysign(0.25*Py_MATH_PI, y); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
542 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
543 /* atan2(+-inf, -inf) == +-pi*3/4 */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
544 return copysign(0.75*Py_MATH_PI, y); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
545 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
546 /* atan2(+-inf, x) == +-pi/2 for finite x */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
547 return copysign(0.5*Py_MATH_PI, y); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
548 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
549 if (Py_IS_INFINITY(x) || y == 0.) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
550 if (copysign(1., x) == 1.) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
551 /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
552 return copysign(0., y); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
553 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
554 /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
555 return copysign(Py_MATH_PI, y); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
556 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
557 return atan2(y, x); |
|
46507
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
558 } |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
559 |
|
022296032d8f
math.atan2 is misbehaving on Windows; this patch
Mark Dickinson <dickinsm@gmail.com>
parents:
46500
diff
changeset
|
560 /* |
|
50122
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
561 Various platforms (Solaris, OpenBSD) do nonstandard things for log(0), |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
562 log(-ve), log(NaN). Here are wrappers for log and log10 that deal with |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
563 special values directly, passing positive non-special values through to |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
564 the system log/log10. |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
565 */ |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
566 |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
567 static double |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
568 m_log(double x) |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
569 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
570 if (Py_IS_FINITE(x)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
571 if (x > 0.0) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
572 return log(x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
573 errno = EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
574 if (x == 0.0) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
575 return -Py_HUGE_VAL; /* log(0) = -inf */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
576 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
577 return Py_NAN; /* log(-ve) = nan */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
578 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
579 else if (Py_IS_NAN(x)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
580 return x; /* log(nan) = nan */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
581 else if (x > 0.0) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
582 return x; /* log(inf) = inf */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
583 else { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
584 errno = EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
585 return Py_NAN; /* log(-inf) = nan */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
586 } |
|
50122
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
587 } |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
588 |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
589 static double |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
590 m_log10(double x) |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
591 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
592 if (Py_IS_FINITE(x)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
593 if (x > 0.0) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
594 return log10(x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
595 errno = EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
596 if (x == 0.0) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
597 return -Py_HUGE_VAL; /* log10(0) = -inf */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
598 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
599 return Py_NAN; /* log10(-ve) = nan */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
600 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
601 else if (Py_IS_NAN(x)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
602 return x; /* log10(nan) = nan */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
603 else if (x > 0.0) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
604 return x; /* log10(inf) = inf */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
605 else { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
606 errno = EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
607 return Py_NAN; /* log10(-inf) = nan */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
608 } |
|
50122
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
609 } |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
610 |
|
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
611 |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
612 /* Call is_error when errno != 0, and where x is the result libm |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
613 * returned. is_error will usually set up an exception and return |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
614 * true (1), but may return false (0) without setting up an exception. |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
615 */ |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
616 static int |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
617 is_error(double x) |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
618 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
619 int result = 1; /* presumption of guilt */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
620 assert(errno); /* non-zero errno is a precondition for calling */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
621 if (errno == EDOM) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
622 PyErr_SetString(PyExc_ValueError, "math domain error"); |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
623 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
624 else if (errno == ERANGE) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
625 /* ANSI C generally requires libm functions to set ERANGE |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
626 * on overflow, but also generally *allows* them to set |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
627 * ERANGE on underflow too. There's no consistency about |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
628 * the latter across platforms. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
629 * Alas, C99 never requires that errno be set. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
630 * Here we suppress the underflow errors (libm functions |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
631 * should return a zero on underflow, and +- HUGE_VAL on |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
632 * overflow, so testing the result for zero suffices to |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
633 * distinguish the cases). |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
634 * |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
635 * On some platforms (Ubuntu/ia64) it seems that errno can be |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
636 * set to ERANGE for subnormal results that do *not* underflow |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
637 * to zero. So to be safe, we'll ignore ERANGE whenever the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
638 * function result is less than one in absolute value. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
639 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
640 if (fabs(x) < 1.0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
641 result = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
642 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
643 PyErr_SetString(PyExc_OverflowError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
644 "math range error"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
645 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
646 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
647 /* Unexpected math error */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
648 PyErr_SetFromErrno(PyExc_ValueError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
649 return result; |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
650 } |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
651 |
|
50122
776a6cafc21a
Merged revisions 67707 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48102
diff
changeset
|
652 /* |
|
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
|
653 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
|
654 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
|
655 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
656 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
|
657 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
|
658 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
|
659 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
660 - 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
|
661 - 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
|
662 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
|
663 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
|
664 - 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
|
665 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
|
666 - 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
|
667 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
|
668 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
669 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
|
670 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
|
671 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
672 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
|
673 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
|
674 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
|
675 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
|
676 '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
|
677 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
|
678 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
|
679 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
|
680 */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
681 |
| 4635 | 682 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
|
683 math_1(PyObject *arg, double (*func) (double), int can_overflow) |
| 6 | 684 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
685 double x, r; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
686 x = PyFloat_AsDouble(arg); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
687 if (x == -1.0 && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
688 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
689 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
690 PyFPE_START_PROTECT("in math_1", return 0); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
691 r = (*func)(x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
692 PyFPE_END_PROTECT(r); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
693 if (Py_IS_NAN(r)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
694 if (!Py_IS_NAN(x)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
695 errno = EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
696 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
697 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
698 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
699 else if (Py_IS_INFINITY(r)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
700 if (Py_IS_FINITE(x)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
701 errno = can_overflow ? ERANGE : EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
702 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
703 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
704 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
705 if (errno && is_error(r)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
706 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
707 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
708 return PyFloat_FromDouble(r); |
| 6 | 709 } |
| 710 | |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
711 /* variant of math_1, to be used when the function being wrapped is known to |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
712 set errno properly (that is, errno = EDOM for invalid or divide-by-zero, |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
713 errno = ERANGE for overflow). */ |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
714 |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
715 static PyObject * |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
716 math_1a(PyObject *arg, double (*func) (double)) |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
717 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
718 double x, r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
719 x = PyFloat_AsDouble(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
720 if (x == -1.0 && PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
721 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
722 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
723 PyFPE_START_PROTECT("in math_1a", return 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
724 r = (*func)(x); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
725 PyFPE_END_PROTECT(r); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
726 if (errno && is_error(r)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
727 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
728 return PyFloat_FromDouble(r); |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
729 } |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
730 |
|
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 /* |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
732 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
|
733 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
|
734 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
735 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
|
736 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
|
737 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
|
738 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
739 - 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
|
740 - 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
|
741 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
|
742 - 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
|
743 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
|
744 - 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
|
745 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
|
746 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
747 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
|
748 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
|
749 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
750 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
|
751 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
|
752 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
|
753 '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
|
754 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
|
755 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
|
756 */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
757 |
| 4635 | 758 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
|
759 math_2(PyObject *args, double (*func) (double, double), char *funcname) |
| 6 | 760 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
761 PyObject *ox, *oy; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
762 double x, y, r; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
763 if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
764 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
765 x = PyFloat_AsDouble(ox); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
766 y = PyFloat_AsDouble(oy); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
767 if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
768 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
769 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
770 PyFPE_START_PROTECT("in math_2", return 0); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
771 r = (*func)(x, y); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
772 PyFPE_END_PROTECT(r); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
773 if (Py_IS_NAN(r)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
774 if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
775 errno = EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
776 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
777 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
778 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
779 else if (Py_IS_INFINITY(r)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
780 if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
781 errno = ERANGE; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
782 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
783 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
784 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
785 if (errno && is_error(r)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
786 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
787 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
788 return PyFloat_FromDouble(r); |
| 6 | 789 } |
| 790 | |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
791 #define FUNC1(funcname, func, can_overflow, docstring) \ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
792 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
793 return math_1(args, func, can_overflow); \ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
794 }\ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
795 PyDoc_STRVAR(math_##funcname##_doc, docstring); |
| 6 | 796 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
797 #define FUNC1A(funcname, func, docstring) \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
798 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
799 return math_1a(args, func); \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
800 }\ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
801 PyDoc_STRVAR(math_##funcname##_doc, docstring); |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
802 |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
803 #define FUNC2(funcname, func, docstring) \ |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
804 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
805 return math_2(args, func, #funcname); \ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
806 }\ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
807 PyDoc_STRVAR(math_##funcname##_doc, docstring); |
| 6 | 808 |
|
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
|
809 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
|
810 "acos(x)\n\nReturn the arc cosine (measured in radians) of x.") |
|
57864
7f5cbc28f433
Issue #7518: Move substitute definitions of C99 math functions from
Mark Dickinson <dickinsm@gmail.com>
parents:
57807
diff
changeset
|
811 FUNC1(acosh, m_acosh, 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
|
812 "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
|
813 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
|
814 "asin(x)\n\nReturn the arc sine (measured in radians) of x.") |
|
57864
7f5cbc28f433
Issue #7518: Move substitute definitions of C99 math functions from
Mark Dickinson <dickinsm@gmail.com>
parents:
57807
diff
changeset
|
815 FUNC1(asinh, m_asinh, 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
|
816 "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
|
817 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
|
818 "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
|
819 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
|
820 "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
|
821 "Unlike atan(y/x), the signs of both x and y are considered.") |
|
57864
7f5cbc28f433
Issue #7518: Move substitute definitions of C99 math functions from
Mark Dickinson <dickinsm@gmail.com>
parents:
57807
diff
changeset
|
822 FUNC1(atanh, m_atanh, 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
|
823 "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
|
824 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
|
825 "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
|
826 "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
|
827 FUNC2(copysign, copysign, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
828 "copysign(x, y)\n\nReturn x with the sign of 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
|
829 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
|
830 "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
|
831 FUNC1(cosh, cosh, 1, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
832 "cosh(x)\n\nReturn the hyperbolic cosine of x.") |
|
57769
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
833 FUNC1A(erf, m_erf, |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
834 "erf(x)\n\nError function at x.") |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
835 FUNC1A(erfc, m_erfc, |
|
d350afc337dc
Issue #3366: Add error function and complementary error function to
Mark Dickinson <dickinsm@gmail.com>
parents:
57756
diff
changeset
|
836 "erfc(x)\n\nComplementary error function at 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
|
837 FUNC1(exp, exp, 1, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
838 "exp(x)\n\nReturn e raised to the power of x.") |
|
57756
b206ad8cf5bf
Issue #3366: Add expm1 function to math module. Thanks Eric Smith for
Mark Dickinson <dickinsm@gmail.com>
parents:
57655
diff
changeset
|
839 FUNC1(expm1, m_expm1, 1, |
|
b206ad8cf5bf
Issue #3366: Add expm1 function to math module. Thanks Eric Smith for
Mark Dickinson <dickinsm@gmail.com>
parents:
57655
diff
changeset
|
840 "expm1(x)\n\nReturn exp(x)-1.\n" |
|
b206ad8cf5bf
Issue #3366: Add expm1 function to math module. Thanks Eric Smith for
Mark Dickinson <dickinsm@gmail.com>
parents:
57655
diff
changeset
|
841 "This function avoids the loss of precision involved in the direct " |
|
b206ad8cf5bf
Issue #3366: Add expm1 function to math module. Thanks Eric Smith for
Mark Dickinson <dickinsm@gmail.com>
parents:
57655
diff
changeset
|
842 "evaluation of exp(x)-1 for small 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
|
843 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
|
844 "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
|
845 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
|
846 "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
|
847 "This is the largest integral value <= x.") |
|
56268
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
848 FUNC1A(gamma, m_tgamma, |
|
b50dd17dfe26
Issue #3366: Add gamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
50243
diff
changeset
|
849 "gamma(x)\n\nGamma function at x.") |
|
57655
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
850 FUNC1A(lgamma, m_lgamma, |
|
cb22eaee86c9
Issue #3366: Add lgamma function to math module.
Mark Dickinson <dickinsm@gmail.com>
parents:
56977
diff
changeset
|
851 "lgamma(x)\n\nNatural logarithm of absolute value of Gamma function at x.") |
|
57864
7f5cbc28f433
Issue #7518: Move substitute definitions of C99 math functions from
Mark Dickinson <dickinsm@gmail.com>
parents:
57807
diff
changeset
|
852 FUNC1(log1p, m_log1p, 1, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
853 "log1p(x)\n\nReturn the natural logarithm of 1+x (base e).\n" |
|
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
854 "The result is computed in a way which is accurate for x near zero.") |
|
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
|
855 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
|
856 "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
|
857 FUNC1(sinh, sinh, 1, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
858 "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
|
859 FUNC1(sqrt, sqrt, 0, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
860 "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
|
861 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
|
862 "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
|
863 FUNC1(tanh, tanh, 0, |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
864 "tanh(x)\n\nReturn the hyperbolic tangent of x.") |
| 6 | 865 |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
866 /* 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
|
867 <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
|
868 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
|
869 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
|
870 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
|
871 |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
872 Note 1: IEEE 754R floating point semantics are assumed, |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
873 but the current implementation does not re-establish special |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
874 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
|
875 |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
876 Note 2: No provision is made for intermediate overflow handling; |
| 47370 | 877 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
|
878 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
|
879 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
|
880 |
| 47777 | 881 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
|
882 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
|
883 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
|
884 regular doubles instead of extended long precision (80-bit) values. This |
| 47777 | 885 prevents double rounding because any addition or subtraction of two doubles |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
886 can be resolved exactly into double-sized hi and lo values. As long as the |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
887 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
|
888 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
|
889 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
|
890 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
|
891 |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
892 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
|
893 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
|
894 |
|
48102
b35ce954a392
Replace math.sum with math.fsum in a couple of comments
Mark Dickinson <dickinsm@gmail.com>
parents:
48101
diff
changeset
|
895 Note 5: The signature of math.fsum() differs from __builtin__.sum() |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
896 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
|
897 accurate summation. Since the partials table is collapsed before |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
898 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
|
899 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
|
900 */ |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
901 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
902 #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
|
903 |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
904 /* Extend the partials array p[] by doubling its size. */ |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
905 static int /* non-zero on error */ |
|
48101
8d05dda735f8
Rename math.sum to math.fsum
Mark Dickinson <dickinsm@gmail.com>
parents:
48099
diff
changeset
|
906 _fsum_realloc(double **p_ptr, Py_ssize_t n, |
|
47532
6cfa297c6555
Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds.
Raymond Hettinger <python@rcn.com>
parents:
47531
diff
changeset
|
907 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
|
908 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
909 void *v = NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
910 Py_ssize_t m = *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
|
911 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
912 m += m; /* double */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
913 if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
914 double *p = *p_ptr; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
915 if (p == ps) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
916 v = PyMem_Malloc(sizeof(double) * m); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
917 if (v != NULL) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
918 memcpy(v, ps, sizeof(double) * n); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
919 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
920 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
921 v = PyMem_Realloc(p, sizeof(double) * m); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
922 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
923 if (v == NULL) { /* size overflow or no memory */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
924 PyErr_SetString(PyExc_MemoryError, "math.fsum partials"); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
925 return 1; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
926 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
927 *p_ptr = (double*) v; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
928 *m_ptr = m; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
929 return 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
|
930 } |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
931 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
932 /* 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
|
933 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
934 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
|
935 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
|
936 for x in iterable: |
|
62494
83ca0ea9ef1f
Merged revisions 82221 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61465
diff
changeset
|
937 i = 0 |
|
83ca0ea9ef1f
Merged revisions 82221 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61465
diff
changeset
|
938 for y in partials: |
|
83ca0ea9ef1f
Merged revisions 82221 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61465
diff
changeset
|
939 if abs(x) < abs(y): |
|
83ca0ea9ef1f
Merged revisions 82221 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61465
diff
changeset
|
940 x, y = y, x |
|
83ca0ea9ef1f
Merged revisions 82221 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61465
diff
changeset
|
941 hi = x + y |
|
83ca0ea9ef1f
Merged revisions 82221 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61465
diff
changeset
|
942 lo = y - (hi - x) |
|
83ca0ea9ef1f
Merged revisions 82221 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61465
diff
changeset
|
943 if lo: |
|
83ca0ea9ef1f
Merged revisions 82221 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61465
diff
changeset
|
944 partials[i] = lo |
|
83ca0ea9ef1f
Merged revisions 82221 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61465
diff
changeset
|
945 i += 1 |
|
83ca0ea9ef1f
Merged revisions 82221 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61465
diff
changeset
|
946 x = hi |
|
83ca0ea9ef1f
Merged revisions 82221 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61465
diff
changeset
|
947 partials[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
|
948 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
|
949 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
950 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
|
951 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
|
952 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
|
953 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
954 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
|
955 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
|
956 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
|
957 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
|
958 |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
959 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
|
960 */ |
|
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
961 |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
962 static PyObject* |
|
48101
8d05dda735f8
Rename math.sum to math.fsum
Mark Dickinson <dickinsm@gmail.com>
parents:
48099
diff
changeset
|
963 math_fsum(PyObject *self, PyObject *seq) |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
964 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
965 PyObject *item, *iter, *sum = NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
966 Py_ssize_t i, j, n = 0, m = NUM_PARTIALS; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
967 double x, y, t, ps[NUM_PARTIALS], *p = ps; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
968 double xsave, special_sum = 0.0, inf_sum = 0.0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
969 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
|
970 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
971 iter = PyObject_GetIter(seq); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
972 if (iter == NULL) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
973 return NULL; |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
974 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
975 PyFPE_START_PROTECT("fsum", Py_DECREF(iter); return NULL) |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
976 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
977 for(;;) { /* for x in iterable */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
978 assert(0 <= n && n <= m); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
979 assert((m == NUM_PARTIALS && p == ps) || |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
980 (m > NUM_PARTIALS && p != NULL)); |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
981 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
982 item = PyIter_Next(iter); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
983 if (item == NULL) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
984 if (PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
985 goto _fsum_error; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
986 break; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
987 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
988 x = PyFloat_AsDouble(item); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
989 Py_DECREF(item); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
990 if (PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
991 goto _fsum_error; |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
992 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
993 xsave = x; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
994 for (i = j = 0; j < n; j++) { /* for y in partials */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
995 y = p[j]; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
996 if (fabs(x) < fabs(y)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
997 t = x; x = y; y = t; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
998 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
999 hi = x + y; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1000 yr = hi - x; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1001 lo = y - yr; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1002 if (lo != 0.0) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1003 p[i++] = lo; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1004 x = hi; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1005 } |
|
48099
b40e13bcb9ea
Fix special-value handling for math.sum.
Mark Dickinson <dickinsm@gmail.com>
parents:
47783
diff
changeset
|
1006 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1007 n = i; /* ps[i:] = [x] */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1008 if (x != 0.0) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1009 if (! Py_IS_FINITE(x)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1010 /* a nonfinite x could arise either as |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1011 a result of intermediate overflow, or |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1012 as a result of a nan or inf in the |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1013 summands */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1014 if (Py_IS_FINITE(xsave)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1015 PyErr_SetString(PyExc_OverflowError, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1016 "intermediate overflow in fsum"); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1017 goto _fsum_error; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1018 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1019 if (Py_IS_INFINITY(xsave)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1020 inf_sum += xsave; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1021 special_sum += xsave; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1022 /* reset partials */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1023 n = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1024 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1025 else if (n >= m && _fsum_realloc(&p, n, ps, &m)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1026 goto _fsum_error; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1027 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1028 p[n++] = x; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1029 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1030 } |
|
47216
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
1031 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1032 if (special_sum != 0.0) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1033 if (Py_IS_NAN(inf_sum)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1034 PyErr_SetString(PyExc_ValueError, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1035 "-inf + inf in fsum"); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1036 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1037 sum = PyFloat_FromDouble(special_sum); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1038 goto _fsum_error; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1039 } |
|
48099
b40e13bcb9ea
Fix special-value handling for math.sum.
Mark Dickinson <dickinsm@gmail.com>
parents:
47783
diff
changeset
|
1040 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1041 hi = 0.0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1042 if (n > 0) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1043 hi = p[--n]; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1044 /* sum_exact(ps, hi) from the top, stop when the sum becomes |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1045 inexact. */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1046 while (n > 0) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1047 x = hi; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1048 y = p[--n]; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1049 assert(fabs(y) < fabs(x)); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1050 hi = x + y; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1051 yr = hi - x; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1052 lo = y - yr; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1053 if (lo != 0.0) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1054 break; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1055 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1056 /* Make half-even rounding work across multiple partials. |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1057 Needed so that sum([1e-16, 1, 1e16]) will round-up the last |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1058 digit to two instead of down to zero (the 1e-16 makes the 1 |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1059 slightly closer to two). With a potential 1 ULP rounding |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1060 error fixed-up, math.fsum() can guarantee commutativity. */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1061 if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) || |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1062 (lo > 0.0 && p[n-1] > 0.0))) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1063 y = lo * 2.0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1064 x = hi + y; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1065 yr = x - hi; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1066 if (y == yr) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1067 hi = x; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1068 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1069 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1070 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
|
1071 |
|
48101
8d05dda735f8
Rename math.sum to math.fsum
Mark Dickinson <dickinsm@gmail.com>
parents:
48099
diff
changeset
|
1072 _fsum_error: |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1073 PyFPE_END_PROTECT(hi) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1074 Py_DECREF(iter); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1075 if (p != ps) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1076 PyMem_Free(p); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1077 return 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
|
1078 } |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
1079 |
|
2fffdedb10f8
Issue #2819: Add math.sum, a function that sums a sequence of floats
Mark Dickinson <dickinsm@gmail.com>
parents:
46797
diff
changeset
|
1080 #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
|
1081 |
|
48101
8d05dda735f8
Rename math.sum to math.fsum
Mark Dickinson <dickinsm@gmail.com>
parents:
48099
diff
changeset
|
1082 PyDoc_STRVAR(math_fsum_doc, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1083 "fsum(iterable)\n\n\ |
|
47220
71deee6b941e
Tweak the comments and formatting.
Raymond Hettinger <python@rcn.com>
parents:
47216
diff
changeset
|
1084 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
|
1085 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
|
1086 |
|
47529
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1087 static PyObject * |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1088 math_factorial(PyObject *self, PyObject *arg) |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1089 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1090 long i, x; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1091 PyObject *result, *iobj, *newresult; |
|
47529
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1092 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1093 if (PyFloat_Check(arg)) { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1094 PyObject *lx; |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1095 double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg); |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1096 if (!(Py_IS_FINITE(dx) && dx == floor(dx))) { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1097 PyErr_SetString(PyExc_ValueError, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1098 "factorial() only accepts integral values"); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1099 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1100 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1101 lx = PyLong_FromDouble(dx); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1102 if (lx == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1103 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1104 x = PyLong_AsLong(lx); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1105 Py_DECREF(lx); |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1106 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1107 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1108 x = PyInt_AsLong(arg); |
|
47529
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1109 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1110 if (x == -1 && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1111 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1112 if (x < 0) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1113 PyErr_SetString(PyExc_ValueError, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1114 "factorial() not defined for negative values"); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1115 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1116 } |
|
47529
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1117 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1118 result = (PyObject *)PyInt_FromLong(1); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1119 if (result == NULL) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1120 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1121 for (i=1 ; i<=x ; i++) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1122 iobj = (PyObject *)PyInt_FromLong(i); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1123 if (iobj == NULL) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1124 goto error; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1125 newresult = PyNumber_Multiply(result, iobj); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1126 Py_DECREF(iobj); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1127 if (newresult == NULL) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1128 goto error; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1129 Py_DECREF(result); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1130 result = newresult; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1131 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1132 return result; |
|
47529
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1133 |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1134 error: |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1135 Py_DECREF(result); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1136 return NULL; |
|
47529
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1137 } |
|
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1138 |
|
50257
e4675113d10e
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50122
diff
changeset
|
1139 PyDoc_STRVAR(math_factorial_doc, |
|
e4675113d10e
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50122
diff
changeset
|
1140 "factorial(x) -> Integral\n" |
|
e4675113d10e
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50122
diff
changeset
|
1141 "\n" |
|
e4675113d10e
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50122
diff
changeset
|
1142 "Find x!. Raise a ValueError if x is negative or non-integral."); |
|
47529
6428c4f1ae09
Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents:
47395
diff
changeset
|
1143 |
| 4635 | 1144 static PyObject * |
|
45079
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
1145 math_trunc(PyObject *self, PyObject *number) |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
1146 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1147 return PyObject_CallMethod(number, "__trunc__", NULL); |
|
45079
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
1148 } |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
1149 |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
1150 PyDoc_STRVAR(math_trunc_doc, |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
1151 "trunc(x:Real) -> Integral\n" |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
1152 "\n" |
| 45102 | 1153 "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
|
1154 |
|
492b302aded6
Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44947
diff
changeset
|
1155 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
|
1156 math_frexp(PyObject *self, PyObject *arg) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
1157 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1158 int i; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1159 double x = PyFloat_AsDouble(arg); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1160 if (x == -1.0 && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1161 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1162 /* deal with special cases directly, to sidestep platform |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1163 differences */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1164 if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1165 i = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1166 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1167 else { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1168 PyFPE_START_PROTECT("in math_frexp", return 0); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1169 x = frexp(x, &i); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1170 PyFPE_END_PROTECT(x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1171 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1172 return Py_BuildValue("(di)", x, i); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
1173 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
1174 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
1175 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
|
1176 "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
|
1177 "\n" |
|
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
1178 "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
|
1179 "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
|
1180 "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
|
1181 |
| 4635 | 1182 static PyObject * |
|
13326
24717405dd89
ANSI-fication, remove Py_PROTO, etc.
Fred Drake <fdrake@acm.org>
parents:
13309
diff
changeset
|
1183 math_ldexp(PyObject *self, PyObject *args) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
1184 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1185 double x, r; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1186 PyObject *oexp; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1187 long exp; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1188 int overflow; |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1189 if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1190 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
|
1191 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1192 if (PyLong_Check(oexp) || PyInt_Check(oexp)) { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1193 /* on overflow, replace exponent with either LONG_MAX |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1194 or LONG_MIN, depending on the sign. */ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1195 exp = PyLong_AsLongAndOverflow(oexp, &overflow); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1196 if (exp == -1 && PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1197 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1198 if (overflow) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1199 exp = overflow < 0 ? LONG_MIN : LONG_MAX; |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1200 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1201 else { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1202 PyErr_SetString(PyExc_TypeError, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1203 "Expected an int or long as second argument " |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1204 "to ldexp."); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1205 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1206 } |
|
46797
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
1207 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1208 if (x == 0. || !Py_IS_FINITE(x)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1209 /* NaNs, zeros and infinities are returned unchanged */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1210 r = x; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1211 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1212 } else if (exp > INT_MAX) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1213 /* overflow */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1214 r = copysign(Py_HUGE_VAL, x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1215 errno = ERANGE; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1216 } else if (exp < INT_MIN) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1217 /* underflow to +-0 */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1218 r = copysign(0., x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1219 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1220 } else { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1221 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1222 PyFPE_START_PROTECT("in math_ldexp", return 0); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1223 r = ldexp(x, (int)exp); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1224 PyFPE_END_PROTECT(r); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1225 if (Py_IS_INFINITY(r)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1226 errno = ERANGE; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1227 } |
|
46797
fd0b894bc90c
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents:
46509
diff
changeset
|
1228 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1229 if (errno && is_error(r)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1230 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1231 return PyFloat_FromDouble(r); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
1232 } |
|
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
1233 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
1234 PyDoc_STRVAR(math_ldexp_doc, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1235 "ldexp(x, i)\n\n\ |
|
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1236 Return x * (2**i)."); |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
1237 |
| 4635 | 1238 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
|
1239 math_modf(PyObject *self, PyObject *arg) |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
1240 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1241 double y, x = PyFloat_AsDouble(arg); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1242 if (x == -1.0 && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1243 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1244 /* some platforms don't do the right thing for NaNs and |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1245 infinities, so we take care of special cases directly. */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1246 if (!Py_IS_FINITE(x)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1247 if (Py_IS_INFINITY(x)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1248 return Py_BuildValue("(dd)", copysign(0., x), x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1249 else if (Py_IS_NAN(x)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1250 return Py_BuildValue("(dd)", x, x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1251 } |
|
46499
80b16595e462
FreeBSD doesn't follow C99 for modf(inf); so add explicit
Mark Dickinson <dickinsm@gmail.com>
parents:
46490
diff
changeset
|
1252 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1253 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1254 PyFPE_START_PROTECT("in math_modf", return 0); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1255 x = modf(x, &y); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1256 PyFPE_END_PROTECT(x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1257 return Py_BuildValue("(dd)", x, y); |
|
407
9b3d557665a9
Added frexp, ldexp, modf, fmod.
Guido van Rossum <guido@python.org>
parents:
320
diff
changeset
|
1258 } |
| 6 | 1259 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
1260 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
|
1261 "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
|
1262 "\n" |
|
b4521436e8f8
Mechanical fiddling to make this easier to work with in my editor.
Tim Peters <tim.peters@gmail.com>
parents:
19048
diff
changeset
|
1263 "Return the fractional and integer parts of x. Both results carry the sign\n" |
|
50257
e4675113d10e
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50122
diff
changeset
|
1264 "of x and are floats."); |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
1265 |
|
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
|
1266 /* 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
|
1267 do that by itself -- loghelper can. func is log or log10, and name is |
|
58085
27c946ff76a3
Refactor some longobject internals: PyLong_AsDouble and _PyLong_AsScaledDouble
Mark Dickinson <dickinsm@gmail.com>
parents:
57864
diff
changeset
|
1268 "log" or "log10". Note that overflow of the result isn't possible: a long |
|
27c946ff76a3
Refactor some longobject internals: PyLong_AsDouble and _PyLong_AsScaledDouble
Mark Dickinson <dickinsm@gmail.com>
parents:
57864
diff
changeset
|
1269 can contain no more than INT_MAX * SHIFT bits, so has value certainly less |
|
27c946ff76a3
Refactor some longobject internals: PyLong_AsDouble and _PyLong_AsScaledDouble
Mark Dickinson <dickinsm@gmail.com>
parents:
57864
diff
changeset
|
1270 than 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is |
|
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
|
1271 small enough to fit in an IEEE single. log and log10 are even smaller. |
|
58085
27c946ff76a3
Refactor some longobject internals: PyLong_AsDouble and _PyLong_AsScaledDouble
Mark Dickinson <dickinsm@gmail.com>
parents:
57864
diff
changeset
|
1272 However, intermediate overflow is possible for a long if the number of bits |
|
27c946ff76a3
Refactor some longobject internals: PyLong_AsDouble and _PyLong_AsScaledDouble
Mark Dickinson <dickinsm@gmail.com>
parents:
57864
diff
changeset
|
1273 in that long is larger than PY_SSIZE_T_MAX. */ |
|
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
|
1274 |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
1275 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
|
1276 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
|
1277 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1278 /* If it is long, do it ourselves. */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1279 if (PyLong_Check(arg)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1280 double x; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1281 Py_ssize_t e; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1282 x = _PyLong_Frexp((PyLongObject *)arg, &e); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1283 if (x == -1.0 && PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1284 return NULL; |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1285 if (x <= 0.0) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1286 PyErr_SetString(PyExc_ValueError, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1287 "math domain error"); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1288 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1289 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1290 /* Special case for log(1), to make sure we get an |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1291 exact result there. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1292 if (e == 1 && x == 0.5) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1293 return PyFloat_FromDouble(0.0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1294 /* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1295 x = func(x) + func(2.0) * e; |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1296 return PyFloat_FromDouble(x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1297 } |
|
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
|
1298 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1299 /* Else let libm handle it by itself. */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1300 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
|
1301 } |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
1302 |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
1303 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
|
1304 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
|
1305 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1306 PyObject *arg; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1307 PyObject *base = NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1308 PyObject *num, *den; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1309 PyObject *ans; |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
1310 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1311 if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1312 return NULL; |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
1313 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1314 num = loghelper(arg, m_log, "log"); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1315 if (num == NULL || base == NULL) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1316 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
|
1317 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1318 den = loghelper(base, m_log, "log"); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1319 if (den == NULL) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1320 Py_DECREF(num); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1321 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1322 } |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
1323 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1324 ans = PyNumber_Divide(num, den); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1325 Py_DECREF(num); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1326 Py_DECREF(den); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1327 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
|
1328 } |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
1329 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
1330 PyDoc_STRVAR(math_log_doc, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1331 "log(x[, base])\n\n\ |
|
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1332 Return the logarithm of x to the given base.\n\ |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24471
diff
changeset
|
1333 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
|
1334 |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
1335 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
|
1336 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
|
1337 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1338 return loghelper(arg, m_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
|
1339 } |
|
9433dd3c197e
Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents:
19712
diff
changeset
|
1340 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
1341 PyDoc_STRVAR(math_log10_doc, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1342 "log10(x)\n\nReturn 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
|
1343 |
|
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
|
1344 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
|
1345 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
|
1346 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1347 PyObject *ox, *oy; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1348 double r, x, y; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1349 if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1350 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1351 x = PyFloat_AsDouble(ox); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1352 y = PyFloat_AsDouble(oy); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1353 if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1354 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1355 /* fmod(x, +/-Inf) returns x for finite x. */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1356 if (Py_IS_INFINITY(y) && Py_IS_FINITE(x)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1357 return PyFloat_FromDouble(x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1358 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1359 PyFPE_START_PROTECT("in math_fmod", return 0); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1360 r = fmod(x, y); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1361 PyFPE_END_PROTECT(r); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1362 if (Py_IS_NAN(r)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1363 if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1364 errno = EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1365 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1366 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1367 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1368 if (errno && is_error(r)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1369 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1370 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1371 return PyFloat_FromDouble(r); |
|
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
|
1372 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1373 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1374 PyDoc_STRVAR(math_fmod_doc, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1375 "fmod(x, y)\n\nReturn fmod(x, y), according to platform C." |
|
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
|
1376 " 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
|
1377 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1378 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
|
1379 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
|
1380 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1381 PyObject *ox, *oy; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1382 double r, x, y; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1383 if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1384 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1385 x = PyFloat_AsDouble(ox); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1386 y = PyFloat_AsDouble(oy); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1387 if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1388 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1389 /* hypot(x, +/-Inf) returns Inf, even if x is a NaN. */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1390 if (Py_IS_INFINITY(x)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1391 return PyFloat_FromDouble(fabs(x)); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1392 if (Py_IS_INFINITY(y)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1393 return PyFloat_FromDouble(fabs(y)); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1394 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1395 PyFPE_START_PROTECT("in math_hypot", return 0); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1396 r = hypot(x, y); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1397 PyFPE_END_PROTECT(r); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1398 if (Py_IS_NAN(r)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1399 if (!Py_IS_NAN(x) && !Py_IS_NAN(y)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1400 errno = EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1401 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1402 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1403 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1404 else if (Py_IS_INFINITY(r)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1405 if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1406 errno = ERANGE; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1407 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1408 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1409 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1410 if (errno && is_error(r)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1411 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1412 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1413 return PyFloat_FromDouble(r); |
|
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
|
1414 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1415 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1416 PyDoc_STRVAR(math_hypot_doc, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1417 "hypot(x, y)\n\nReturn the Euclidean distance, sqrt(x*x + y*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
|
1418 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1419 /* 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
|
1420 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
|
1421 (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
|
1422 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
|
1423 */ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1424 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1425 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
|
1426 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
|
1427 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1428 PyObject *ox, *oy; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1429 double r, x, y; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1430 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
|
1431 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1432 if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1433 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1434 x = PyFloat_AsDouble(ox); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1435 y = PyFloat_AsDouble(oy); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1436 if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1437 return NULL; |
|
46490
648b4d01cccf
Additional special-case handling for math.pow.
Mark Dickinson <dickinsm@gmail.com>
parents:
46486
diff
changeset
|
1438 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1439 /* deal directly with IEEE specials, to cope with problems on various |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1440 platforms whose semantics don't exactly match C99 */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1441 r = 0.; /* silence compiler warning */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1442 if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1443 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1444 if (Py_IS_NAN(x)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1445 r = y == 0. ? 1. : x; /* NaN**0 = 1 */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1446 else if (Py_IS_NAN(y)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1447 r = x == 1. ? 1. : y; /* 1**NaN = 1 */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1448 else if (Py_IS_INFINITY(x)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1449 odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1450 if (y > 0.) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1451 r = odd_y ? x : fabs(x); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1452 else if (y == 0.) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1453 r = 1.; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1454 else /* y < 0. */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1455 r = odd_y ? copysign(0., x) : 0.; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1456 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1457 else if (Py_IS_INFINITY(y)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1458 if (fabs(x) == 1.0) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1459 r = 1.; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1460 else if (y > 0. && fabs(x) > 1.0) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1461 r = y; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1462 else if (y < 0. && fabs(x) < 1.0) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1463 r = -y; /* result is +inf */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1464 if (x == 0.) /* 0**-inf: divide-by-zero */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1465 errno = EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1466 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1467 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1468 r = 0.; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1469 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1470 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1471 else { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1472 /* let libm handle finite**finite */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1473 errno = 0; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1474 PyFPE_START_PROTECT("in math_pow", return 0); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1475 r = pow(x, y); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1476 PyFPE_END_PROTECT(r); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1477 /* a NaN result should arise only from (-ve)**(finite |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1478 non-integer); in this case we want to raise ValueError. */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1479 if (!Py_IS_FINITE(r)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1480 if (Py_IS_NAN(r)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1481 errno = EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1482 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1483 /* |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1484 an infinite result here arises either from: |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1485 (A) (+/-0.)**negative (-> divide-by-zero) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1486 (B) overflow of x**y with x and y finite |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1487 */ |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1488 else if (Py_IS_INFINITY(r)) { |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1489 if (x == 0.) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1490 errno = EDOM; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1491 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1492 errno = ERANGE; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1493 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1494 } |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1495 } |
|
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
|
1496 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1497 if (errno && is_error(r)) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1498 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1499 else |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1500 return PyFloat_FromDouble(r); |
|
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
|
1501 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1502 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
45102
diff
changeset
|
1503 PyDoc_STRVAR(math_pow_doc, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1504 "pow(x, y)\n\nReturn x**y (x to the power of 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
|
1505 |
|
44437
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1506 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
|
1507 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
|
1508 |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
1509 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
|
1510 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
|
1511 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1512 double x = PyFloat_AsDouble(arg); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1513 if (x == -1.0 && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1514 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1515 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
|
1516 } |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
1517 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
1518 PyDoc_STRVAR(math_degrees_doc, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1519 "degrees(x)\n\n\ |
|
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1520 Convert 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
|
1521 |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
1522 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
|
1523 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
|
1524 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1525 double x = PyFloat_AsDouble(arg); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1526 if (x == -1.0 && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1527 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1528 return PyFloat_FromDouble(x * degToRad); |
|
23427
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
1529 } |
|
a4d675b8b53c
Added degrees() and radians() to mathmodule. Closes patch 552452 and
Raymond Hettinger <python@rcn.com>
parents:
19758
diff
changeset
|
1530 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
1531 PyDoc_STRVAR(math_radians_doc, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1532 "radians(x)\n\n\ |
|
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1533 Convert 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
|
1534 |
|
44437
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1535 static PyObject * |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1536 math_isnan(PyObject *self, PyObject *arg) |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1537 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1538 double x = PyFloat_AsDouble(arg); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1539 if (x == -1.0 && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1540 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1541 return PyBool_FromLong((long)Py_IS_NAN(x)); |
|
44437
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1542 } |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1543 |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1544 PyDoc_STRVAR(math_isnan_doc, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1545 "isnan(x) -> bool\n\n\ |
|
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1546 Check if float x is not a number (NaN)."); |
|
44437
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1547 |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1548 static PyObject * |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1549 math_isinf(PyObject *self, PyObject *arg) |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1550 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1551 double x = PyFloat_AsDouble(arg); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1552 if (x == -1.0 && PyErr_Occurred()) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1553 return NULL; |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1554 return PyBool_FromLong((long)Py_IS_INFINITY(x)); |
|
44437
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1555 } |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1556 |
|
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1557 PyDoc_STRVAR(math_isinf_doc, |
|
59829
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1558 "isinf(x) -> bool\n\n\ |
|
edc92bce4d6e
Merged revisions 75952-75953,75955,76105,76143,76223,76259,76326,76376-76377 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
1559 Check if float x is infinite (positive or negative)."); |
|
44437
968bda81e6c8
Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents:
44432
diff
changeset
|
1560 |
| 4635 | 1561 static PyMethodDef math_methods[] = { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1562 {"acos", math_acos, METH_O, math_acos_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1563 {"acosh", math_acosh, METH_O, math_acosh_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1564 {"asin", math_asin, METH_O, math_asin_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1565 {"asinh", math_asinh, METH_O, math_asinh_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1566 {"atan", math_atan, METH_O, math_atan_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1567 {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1568 {"atanh", math_atanh, METH_O, math_atanh_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1569 {"ceil", math_ceil, METH_O, math_ceil_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1570 {"copysign", math_copysign, METH_VARARGS, math_copysign_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1571 {"cos", math_cos, METH_O, math_cos_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1572 {"cosh", math_cosh, METH_O, math_cosh_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1573 {"degrees", math_degrees, METH_O, math_degrees_doc}, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1574 {"erf", math_erf, METH_O, math_erf_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1575 {"erfc", math_erfc, METH_O, math_erfc_doc}, |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1576 {"exp", math_exp, METH_O, math_exp_doc}, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1577 {"expm1", math_expm1, METH_O, math_expm1_doc}, |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1578 {"fabs", math_fabs, METH_O, math_fabs_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1579 {"factorial", math_factorial, METH_O, math_factorial_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1580 {"floor", math_floor, METH_O, math_floor_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1581 {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1582 {"frexp", math_frexp, METH_O, math_frexp_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1583 {"fsum", math_fsum, METH_O, math_fsum_doc}, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1584 {"gamma", math_gamma, METH_O, math_gamma_doc}, |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1585 {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1586 {"isinf", math_isinf, METH_O, math_isinf_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1587 {"isnan", math_isnan, METH_O, math_isnan_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1588 {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58124
diff
changeset
|
1589 {"lgamma", math_lgamma, METH_O, math_lgamma_doc}, |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1590 {"log", math_log, METH_VARARGS, math_log_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1591 {"log1p", math_log1p, METH_O, math_log1p_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1592 {"log10", math_log10, METH_O, math_log10_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1593 {"modf", math_modf, METH_O, math_modf_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1594 {"pow", math_pow, METH_VARARGS, math_pow_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1595 {"radians", math_radians, METH_O, math_radians_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1596 {"sin", math_sin, METH_O, math_sin_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1597 {"sinh", math_sinh, METH_O, math_sinh_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1598 {"sqrt", math_sqrt, METH_O, math_sqrt_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1599 {"tan", math_tan, METH_O, math_tan_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1600 {"tanh", math_tanh, METH_O, math_tanh_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1601 {"trunc", math_trunc, METH_O, math_trunc_doc}, |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1602 {NULL, NULL} /* sentinel */ |
| 6 | 1603 }; |
| 1604 | |
|
9593
f22f769bf3d9
Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
1605 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23427
diff
changeset
|
1606 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
|
1607 "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
|
1608 "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
|
1609 |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
23832
diff
changeset
|
1610 PyMODINIT_FUNC |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13331
diff
changeset
|
1611 initmath(void) |
| 6 | 1612 { |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1613 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
|
1614 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1615 m = Py_InitModule3("math", math_methods, module_doc); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1616 if (m == NULL) |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1617 goto finally; |
|
4752
7367fb107108
Better error checking in initmath().
Barry Warsaw <barry@python.org>
parents:
4635
diff
changeset
|
1618 |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1619 PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); |
|
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1620 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
|
1621 |
|
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
|
1622 finally: |
|
61465
e38335d4df91
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59829
diff
changeset
|
1623 return; |
| 6 | 1624 } |
