You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3400 lines
99 KiB

36 years ago
30 years ago
36 years ago
30 years ago
36 years ago
14 years ago
36 years ago
30 years ago
36 years ago
36 years ago
36 years ago
Merged revisions 75149,75260-75263,75265-75267,75292,75300,75376,75405,75429-75433,75437,75445,75501,75551,75572,75589-75591,75657,75742,75868,75952-75957,76057,76105,76139,76143,76162,76223 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r75149 | gregory.p.smith | 2009-09-29 16:56:31 -0500 (Tue, 29 Sep 2009) | 3 lines Mention issue6972 in extractall docs about overwriting things outside of the supplied path. ........ r75260 | andrew.kuchling | 2009-10-05 16:24:20 -0500 (Mon, 05 Oct 2009) | 1 line Wording fix ........ r75261 | andrew.kuchling | 2009-10-05 16:24:35 -0500 (Mon, 05 Oct 2009) | 1 line Fix narkup ........ r75262 | andrew.kuchling | 2009-10-05 16:25:03 -0500 (Mon, 05 Oct 2009) | 1 line Document 'skip' parameter to constructor ........ r75263 | andrew.kuchling | 2009-10-05 16:25:35 -0500 (Mon, 05 Oct 2009) | 1 line Note side benefit of socket.create_connection() ........ r75265 | andrew.kuchling | 2009-10-05 17:31:11 -0500 (Mon, 05 Oct 2009) | 1 line Reword sentence ........ r75266 | andrew.kuchling | 2009-10-05 17:32:48 -0500 (Mon, 05 Oct 2009) | 1 line Use standard comma punctuation; reword some sentences in the docs ........ r75267 | andrew.kuchling | 2009-10-05 17:42:56 -0500 (Mon, 05 Oct 2009) | 1 line Backport r73983: Document the thousands separator. ........ r75292 | benjamin.peterson | 2009-10-08 22:11:36 -0500 (Thu, 08 Oct 2009) | 1 line death to old CVS keyword ........ r75300 | benjamin.peterson | 2009-10-09 16:48:14 -0500 (Fri, 09 Oct 2009) | 1 line fix some coding style ........ r75376 | benjamin.peterson | 2009-10-11 20:26:07 -0500 (Sun, 11 Oct 2009) | 1 line platform we don't care about ........ r75405 | neil.schemenauer | 2009-10-14 12:17:14 -0500 (Wed, 14 Oct 2009) | 4 lines Issue #1754094: Improve the stack depth calculation in the compiler. There should be no other effect than a small decrease in memory use. Patch by Christopher Tur Lesniewski-Laas. ........ r75429 | benjamin.peterson | 2009-10-14 20:47:28 -0500 (Wed, 14 Oct 2009) | 1 line pep8ify if blocks ........ r75430 | benjamin.peterson | 2009-10-14 20:49:37 -0500 (Wed, 14 Oct 2009) | 1 line use floor division and add a test that exercises the tabsize codepath ........ r75431 | benjamin.peterson | 2009-10-14 20:56:25 -0500 (Wed, 14 Oct 2009) | 1 line change test to what I intended ........ r75432 | benjamin.peterson | 2009-10-14 22:05:39 -0500 (Wed, 14 Oct 2009) | 1 line some cleanups ........ r75433 | benjamin.peterson | 2009-10-14 22:06:55 -0500 (Wed, 14 Oct 2009) | 1 line make inspect.isabstract() always return a boolean; add a test for it, too #7069 ........ r75437 | benjamin.peterson | 2009-10-15 10:44:46 -0500 (Thu, 15 Oct 2009) | 1 line only clear a module's __dict__ if the module is the only one with a reference to it #7140 ........ r75445 | vinay.sajip | 2009-10-16 09:06:44 -0500 (Fri, 16 Oct 2009) | 1 line Issue #7120: logging: Removed import of multiprocessing which is causing crash in GAE. ........ r75501 | antoine.pitrou | 2009-10-18 13:37:11 -0500 (Sun, 18 Oct 2009) | 3 lines Add a comment about unreachable code, and fix a typo ........ r75551 | benjamin.peterson | 2009-10-19 22:14:10 -0500 (Mon, 19 Oct 2009) | 1 line use property api ........ r75572 | benjamin.peterson | 2009-10-20 16:55:17 -0500 (Tue, 20 Oct 2009) | 1 line clarify buffer arg #7178 ........ r75589 | benjamin.peterson | 2009-10-21 21:26:47 -0500 (Wed, 21 Oct 2009) | 1 line whitespace ........ r75590 | benjamin.peterson | 2009-10-21 21:36:47 -0500 (Wed, 21 Oct 2009) | 1 line rewrite to be nice to other implementations ........ r75591 | benjamin.peterson | 2009-10-21 21:50:38 -0500 (Wed, 21 Oct 2009) | 4 lines rewrite for style, clarify, and comments Also, use the hasattr() like scheme of allowing BaseException exceptions through. ........ r75657 | antoine.pitrou | 2009-10-24 07:41:27 -0500 (Sat, 24 Oct 2009) | 3 lines Fix compilation error in debug mode. ........ r75742 | benjamin.peterson | 2009-10-26 17:51:16 -0500 (Mon, 26 Oct 2009) | 1 line use 'is' instead of id() ........ r75868 | benjamin.peterson | 2009-10-27 15:59:18 -0500 (Tue, 27 Oct 2009) | 1 line test expect base classes ........ r75952 | georg.brandl | 2009-10-29 15:38:32 -0500 (Thu, 29 Oct 2009) | 1 line Use the correct function name in docstring. ........ r75953 | georg.brandl | 2009-10-29 15:39:50 -0500 (Thu, 29 Oct 2009) | 1 line Remove mention of the old -X command line switch. ........ r75954 | georg.brandl | 2009-10-29 15:53:00 -0500 (Thu, 29 Oct 2009) | 1 line Use constants instead of magic integers for test result. Do not re-run with --verbose3 for environment changing tests. ........ r75955 | georg.brandl | 2009-10-29 15:54:03 -0500 (Thu, 29 Oct 2009) | 1 line Use a single style for all the docstrings in the math module. ........ r75956 | georg.brandl | 2009-10-29 16:16:34 -0500 (Thu, 29 Oct 2009) | 1 line I do not think the "railroad" program mentioned is still available. ........ r75957 | georg.brandl | 2009-10-29 16:44:56 -0500 (Thu, 29 Oct 2009) | 1 line Fix constant name. ........ r76057 | benjamin.peterson | 2009-11-02 09:06:45 -0600 (Mon, 02 Nov 2009) | 1 line prevent a rather unlikely segfault ........ r76105 | georg.brandl | 2009-11-04 01:38:12 -0600 (Wed, 04 Nov 2009) | 1 line #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs. ........ r76139 | benjamin.peterson | 2009-11-06 19:04:38 -0600 (Fri, 06 Nov 2009) | 1 line spelling ........ r76143 | georg.brandl | 2009-11-07 02:26:07 -0600 (Sat, 07 Nov 2009) | 1 line #7271: fix typo. ........ r76162 | benjamin.peterson | 2009-11-08 22:10:53 -0600 (Sun, 08 Nov 2009) | 1 line discuss how to use -p ........ r76223 | georg.brandl | 2009-11-12 02:29:46 -0600 (Thu, 12 Nov 2009) | 1 line Give the profile module a module directive. ........
16 years ago
36 years ago
Merged revisions 63542-63544,63546,63553,63563-63564,63567,63569,63576 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63542 | mark.dickinson | 2008-05-22 20:35:30 -0500 (Thu, 22 May 2008) | 5 lines Issue #2819: Add math.sum, a function that sums a sequence of floats efficiently but with no intermediate loss of precision. Based on Raymond Hettinger's ASPN recipe. Thanks Jean Brouwers for the patch. ........ r63543 | mark.dickinson | 2008-05-22 21:36:48 -0500 (Thu, 22 May 2008) | 2 lines Add tests for math.sum (Issue #2819) ........ r63544 | mark.dickinson | 2008-05-22 22:30:01 -0500 (Thu, 22 May 2008) | 2 lines Better error reporting in test_math.py ........ r63546 | raymond.hettinger | 2008-05-22 23:32:43 -0500 (Thu, 22 May 2008) | 1 line Tweak the comments and formatting. ........ r63553 | mark.dickinson | 2008-05-23 07:07:36 -0500 (Fri, 23 May 2008) | 3 lines Skip math.sum tests on non IEEE 754 platforms, and on IEEE 754 platforms that exhibit the problem described in issue #2937. ........ r63563 | martin.v.loewis | 2008-05-23 10:18:28 -0500 (Fri, 23 May 2008) | 3 lines Issue #1390: Raise ValueError in toxml when an invalid comment would otherwise be produced. ........ r63564 | raymond.hettinger | 2008-05-23 12:21:44 -0500 (Fri, 23 May 2008) | 1 line Issue 2909: show how to name unpacked fields. ........ r63567 | raymond.hettinger | 2008-05-23 12:34:34 -0500 (Fri, 23 May 2008) | 1 line Fix typo ........ r63569 | martin.v.loewis | 2008-05-23 14:33:13 -0500 (Fri, 23 May 2008) | 3 lines Mention that the leaking of variables from list comprehensions is fixed in 3.0. ........ r63576 | martin.v.loewis | 2008-05-24 04:36:45 -0500 (Sat, 24 May 2008) | 3 lines Don't try to get the window size if it was never set before. Fixes the test failure on Solaris. ........
18 years ago
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63562 | martin.v.loewis | 2008-05-23 17:06:50 +0200 (Fri, 23 May 2008) | 2 lines Patch #1722225: Support QNX 6. ........ r63570 | trent.nelson | 2008-05-23 22:33:14 +0200 (Fri, 23 May 2008) | 1 line Introduce a user macro named $(externalsDir), which should point to the root directory of where all the external sources should live. Developers can change this value if their external sources live elsewhere. The default of '..\..' matches the current status quo. ........ r63728 | gregory.p.smith | 2008-05-26 23:16:34 +0200 (Mon, 26 May 2008) | 4 lines Fix issue2589: there was a potential integer overflow leading to memory corruption on esoteric platforms and incorrect behavior on normal platforms. ........ r63734 | gregory.p.smith | 2008-05-27 00:07:28 +0200 (Tue, 27 May 2008) | 3 lines Fix issue2588: Do not execute str[size-1] = '\0' when a 0 size is passed in. (The assert won't prevent this in non-debug builds). ........ r63784 | raymond.hettinger | 2008-05-29 10:38:23 +0200 (Thu, 29 May 2008) | 1 line Fix two typos. ........ r63788 | facundo.batista | 2008-05-29 18:39:26 +0200 (Thu, 29 May 2008) | 6 lines Fixed the semantic of timeout for socket.create_connection and all the upper level libraries that use it, including urllib2. Added and fixed some tests, and changed docs correspondingly. Thanks to John J Lee for the patch and the pusing, :) ........ r63802 | mark.dickinson | 2008-05-30 04:46:53 +0200 (Fri, 30 May 2008) | 2 lines Fix typo in testSum ........ r63817 | raymond.hettinger | 2008-05-30 20:20:50 +0200 (Fri, 30 May 2008) | 8 lines * Mark intermedidate computes values (hi, lo, yr) as volatile. * Expand comments. * Swap variable names in the sum_exact code so that x and y are consistently chosen as the larger and smaller magnitude values respectively. ........ r63827 | raymond.hettinger | 2008-05-31 05:24:31 +0200 (Sat, 31 May 2008) | 1 line Implement heapq in terms of less-than (to match list.sort()). ........ r63839 | gerhard.haering | 2008-05-31 23:33:27 +0200 (Sat, 31 May 2008) | 2 lines Fixed rowcount for SELECT statements. They're -1 now (again), for better DB-API 2.0 compliance. ........ r63887 | gregory.p.smith | 2008-06-02 06:05:52 +0200 (Mon, 02 Jun 2008) | 4 lines Fix issue 2782: be less strict about the format string type in strftime. Accept unicode and anything else ParseTuple "s#" can deal with. This matches the time.strftime behavior. ........ r63975 | neal.norwitz | 2008-06-06 06:47:01 +0200 (Fri, 06 Jun 2008) | 3 lines Aldo Cortesi confirmed this is still needed for OpenBSD 4.2 and 4.3. (I didn't regen configure, since I don't have a working autoconf.) ........ r63998 | raymond.hettinger | 2008-06-06 23:47:51 +0200 (Fri, 06 Jun 2008) | 1 line Issue 3501: Make heapq support both __le__ and __lt__. ........
18 years ago
Merged revisions 63542-63544,63546,63553,63563-63564,63567,63569,63576 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63542 | mark.dickinson | 2008-05-22 20:35:30 -0500 (Thu, 22 May 2008) | 5 lines Issue #2819: Add math.sum, a function that sums a sequence of floats efficiently but with no intermediate loss of precision. Based on Raymond Hettinger's ASPN recipe. Thanks Jean Brouwers for the patch. ........ r63543 | mark.dickinson | 2008-05-22 21:36:48 -0500 (Thu, 22 May 2008) | 2 lines Add tests for math.sum (Issue #2819) ........ r63544 | mark.dickinson | 2008-05-22 22:30:01 -0500 (Thu, 22 May 2008) | 2 lines Better error reporting in test_math.py ........ r63546 | raymond.hettinger | 2008-05-22 23:32:43 -0500 (Thu, 22 May 2008) | 1 line Tweak the comments and formatting. ........ r63553 | mark.dickinson | 2008-05-23 07:07:36 -0500 (Fri, 23 May 2008) | 3 lines Skip math.sum tests on non IEEE 754 platforms, and on IEEE 754 platforms that exhibit the problem described in issue #2937. ........ r63563 | martin.v.loewis | 2008-05-23 10:18:28 -0500 (Fri, 23 May 2008) | 3 lines Issue #1390: Raise ValueError in toxml when an invalid comment would otherwise be produced. ........ r63564 | raymond.hettinger | 2008-05-23 12:21:44 -0500 (Fri, 23 May 2008) | 1 line Issue 2909: show how to name unpacked fields. ........ r63567 | raymond.hettinger | 2008-05-23 12:34:34 -0500 (Fri, 23 May 2008) | 1 line Fix typo ........ r63569 | martin.v.loewis | 2008-05-23 14:33:13 -0500 (Fri, 23 May 2008) | 3 lines Mention that the leaking of variables from list comprehensions is fixed in 3.0. ........ r63576 | martin.v.loewis | 2008-05-24 04:36:45 -0500 (Sat, 24 May 2008) | 3 lines Don't try to get the window size if it was never set before. Fixes the test failure on Solaris. ........
18 years ago
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64365 | raymond.hettinger | 2008-06-17 19:56:57 -0500 (Tue, 17 Jun 2008) | 1 line Fix double decref. ........ r64370 | mark.dickinson | 2008-06-18 04:20:17 -0500 (Wed, 18 Jun 2008) | 2 lines Typo fix ........ r64406 | andrew.kuchling | 2008-06-19 09:02:30 -0500 (Thu, 19 Jun 2008) | 1 line Only include update_lines_cols() function when it's actually going to be used ........ r64408 | amaury.forgeotdarc | 2008-06-19 14:57:39 -0500 (Thu, 19 Jun 2008) | 2 lines test_macos can be skipped on non-mac platforms. ........ r64409 | andrew.kuchling | 2008-06-19 15:33:31 -0500 (Thu, 19 Jun 2008) | 1 line Put threading in front of thread ........ r64412 | amaury.forgeotdarc | 2008-06-19 16:17:12 -0500 (Thu, 19 Jun 2008) | 3 lines In test_site, correctly escape backslashes in path names. This allows the test to pass when the username begins with a lowercase 't'... ........ r64416 | vinay.sajip | 2008-06-19 17:40:17 -0500 (Thu, 19 Jun 2008) | 2 lines Bug #3136: fileConfig()'s disabling of old loggers is now conditional via an optional disable_existing_loggers parameter, but the default value is such that the old behaviour is preserved. Thanks to Leandro Lucarella for the patch. ........ r64417 | vinay.sajip | 2008-06-19 17:41:08 -0500 (Thu, 19 Jun 2008) | 1 line Updated with fix for #3136. ........ r64420 | andrew.kuchling | 2008-06-19 21:05:57 -0500 (Thu, 19 Jun 2008) | 1 line Various items ........ r64421 | andrew.kuchling | 2008-06-19 21:11:42 -0500 (Thu, 19 Jun 2008) | 1 line Fix comment typos ........ r64425 | andrew.kuchling | 2008-06-20 06:39:54 -0500 (Fri, 20 Jun 2008) | 1 line Various items ........ r64426 | mark.dickinson | 2008-06-20 09:53:43 -0500 (Fri, 20 Jun 2008) | 4 lines Issue #3004: Minor fix to slice.indices(). slice(-10).indices(9) now returns (0, 0, 1) instead of (0, -1, 1), and slice(None, 10, -1).indices(10) returns (9, 9, -1) instead of (9, 10, -1). ........ r64427 | mark.dickinson | 2008-06-20 10:17:41 -0500 (Fri, 20 Jun 2008) | 2 lines Fix outdated count of the number of new math module functions. ........ r64428 | mark.dickinson | 2008-06-20 10:26:19 -0500 (Fri, 20 Jun 2008) | 2 lines Fix another typo in math_sum comment ........
18 years ago
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63562 | martin.v.loewis | 2008-05-23 17:06:50 +0200 (Fri, 23 May 2008) | 2 lines Patch #1722225: Support QNX 6. ........ r63570 | trent.nelson | 2008-05-23 22:33:14 +0200 (Fri, 23 May 2008) | 1 line Introduce a user macro named $(externalsDir), which should point to the root directory of where all the external sources should live. Developers can change this value if their external sources live elsewhere. The default of '..\..' matches the current status quo. ........ r63728 | gregory.p.smith | 2008-05-26 23:16:34 +0200 (Mon, 26 May 2008) | 4 lines Fix issue2589: there was a potential integer overflow leading to memory corruption on esoteric platforms and incorrect behavior on normal platforms. ........ r63734 | gregory.p.smith | 2008-05-27 00:07:28 +0200 (Tue, 27 May 2008) | 3 lines Fix issue2588: Do not execute str[size-1] = '\0' when a 0 size is passed in. (The assert won't prevent this in non-debug builds). ........ r63784 | raymond.hettinger | 2008-05-29 10:38:23 +0200 (Thu, 29 May 2008) | 1 line Fix two typos. ........ r63788 | facundo.batista | 2008-05-29 18:39:26 +0200 (Thu, 29 May 2008) | 6 lines Fixed the semantic of timeout for socket.create_connection and all the upper level libraries that use it, including urllib2. Added and fixed some tests, and changed docs correspondingly. Thanks to John J Lee for the patch and the pusing, :) ........ r63802 | mark.dickinson | 2008-05-30 04:46:53 +0200 (Fri, 30 May 2008) | 2 lines Fix typo in testSum ........ r63817 | raymond.hettinger | 2008-05-30 20:20:50 +0200 (Fri, 30 May 2008) | 8 lines * Mark intermedidate computes values (hi, lo, yr) as volatile. * Expand comments. * Swap variable names in the sum_exact code so that x and y are consistently chosen as the larger and smaller magnitude values respectively. ........ r63827 | raymond.hettinger | 2008-05-31 05:24:31 +0200 (Sat, 31 May 2008) | 1 line Implement heapq in terms of less-than (to match list.sort()). ........ r63839 | gerhard.haering | 2008-05-31 23:33:27 +0200 (Sat, 31 May 2008) | 2 lines Fixed rowcount for SELECT statements. They're -1 now (again), for better DB-API 2.0 compliance. ........ r63887 | gregory.p.smith | 2008-06-02 06:05:52 +0200 (Mon, 02 Jun 2008) | 4 lines Fix issue 2782: be less strict about the format string type in strftime. Accept unicode and anything else ParseTuple "s#" can deal with. This matches the time.strftime behavior. ........ r63975 | neal.norwitz | 2008-06-06 06:47:01 +0200 (Fri, 06 Jun 2008) | 3 lines Aldo Cortesi confirmed this is still needed for OpenBSD 4.2 and 4.3. (I didn't regen configure, since I don't have a working autoconf.) ........ r63998 | raymond.hettinger | 2008-06-06 23:47:51 +0200 (Fri, 06 Jun 2008) | 1 line Issue 3501: Make heapq support both __le__ and __lt__. ........
18 years ago
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64365 | raymond.hettinger | 2008-06-17 19:56:57 -0500 (Tue, 17 Jun 2008) | 1 line Fix double decref. ........ r64370 | mark.dickinson | 2008-06-18 04:20:17 -0500 (Wed, 18 Jun 2008) | 2 lines Typo fix ........ r64406 | andrew.kuchling | 2008-06-19 09:02:30 -0500 (Thu, 19 Jun 2008) | 1 line Only include update_lines_cols() function when it's actually going to be used ........ r64408 | amaury.forgeotdarc | 2008-06-19 14:57:39 -0500 (Thu, 19 Jun 2008) | 2 lines test_macos can be skipped on non-mac platforms. ........ r64409 | andrew.kuchling | 2008-06-19 15:33:31 -0500 (Thu, 19 Jun 2008) | 1 line Put threading in front of thread ........ r64412 | amaury.forgeotdarc | 2008-06-19 16:17:12 -0500 (Thu, 19 Jun 2008) | 3 lines In test_site, correctly escape backslashes in path names. This allows the test to pass when the username begins with a lowercase 't'... ........ r64416 | vinay.sajip | 2008-06-19 17:40:17 -0500 (Thu, 19 Jun 2008) | 2 lines Bug #3136: fileConfig()'s disabling of old loggers is now conditional via an optional disable_existing_loggers parameter, but the default value is such that the old behaviour is preserved. Thanks to Leandro Lucarella for the patch. ........ r64417 | vinay.sajip | 2008-06-19 17:41:08 -0500 (Thu, 19 Jun 2008) | 1 line Updated with fix for #3136. ........ r64420 | andrew.kuchling | 2008-06-19 21:05:57 -0500 (Thu, 19 Jun 2008) | 1 line Various items ........ r64421 | andrew.kuchling | 2008-06-19 21:11:42 -0500 (Thu, 19 Jun 2008) | 1 line Fix comment typos ........ r64425 | andrew.kuchling | 2008-06-20 06:39:54 -0500 (Fri, 20 Jun 2008) | 1 line Various items ........ r64426 | mark.dickinson | 2008-06-20 09:53:43 -0500 (Fri, 20 Jun 2008) | 4 lines Issue #3004: Minor fix to slice.indices(). slice(-10).indices(9) now returns (0, 0, 1) instead of (0, -1, 1), and slice(None, 10, -1).indices(10) returns (9, 9, -1) instead of (9, 10, -1). ........ r64427 | mark.dickinson | 2008-06-20 10:17:41 -0500 (Fri, 20 Jun 2008) | 2 lines Fix outdated count of the number of new math module functions. ........ r64428 | mark.dickinson | 2008-06-20 10:26:19 -0500 (Fri, 20 Jun 2008) | 2 lines Fix another typo in math_sum comment ........
18 years ago
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63562 | martin.v.loewis | 2008-05-23 17:06:50 +0200 (Fri, 23 May 2008) | 2 lines Patch #1722225: Support QNX 6. ........ r63570 | trent.nelson | 2008-05-23 22:33:14 +0200 (Fri, 23 May 2008) | 1 line Introduce a user macro named $(externalsDir), which should point to the root directory of where all the external sources should live. Developers can change this value if their external sources live elsewhere. The default of '..\..' matches the current status quo. ........ r63728 | gregory.p.smith | 2008-05-26 23:16:34 +0200 (Mon, 26 May 2008) | 4 lines Fix issue2589: there was a potential integer overflow leading to memory corruption on esoteric platforms and incorrect behavior on normal platforms. ........ r63734 | gregory.p.smith | 2008-05-27 00:07:28 +0200 (Tue, 27 May 2008) | 3 lines Fix issue2588: Do not execute str[size-1] = '\0' when a 0 size is passed in. (The assert won't prevent this in non-debug builds). ........ r63784 | raymond.hettinger | 2008-05-29 10:38:23 +0200 (Thu, 29 May 2008) | 1 line Fix two typos. ........ r63788 | facundo.batista | 2008-05-29 18:39:26 +0200 (Thu, 29 May 2008) | 6 lines Fixed the semantic of timeout for socket.create_connection and all the upper level libraries that use it, including urllib2. Added and fixed some tests, and changed docs correspondingly. Thanks to John J Lee for the patch and the pusing, :) ........ r63802 | mark.dickinson | 2008-05-30 04:46:53 +0200 (Fri, 30 May 2008) | 2 lines Fix typo in testSum ........ r63817 | raymond.hettinger | 2008-05-30 20:20:50 +0200 (Fri, 30 May 2008) | 8 lines * Mark intermedidate computes values (hi, lo, yr) as volatile. * Expand comments. * Swap variable names in the sum_exact code so that x and y are consistently chosen as the larger and smaller magnitude values respectively. ........ r63827 | raymond.hettinger | 2008-05-31 05:24:31 +0200 (Sat, 31 May 2008) | 1 line Implement heapq in terms of less-than (to match list.sort()). ........ r63839 | gerhard.haering | 2008-05-31 23:33:27 +0200 (Sat, 31 May 2008) | 2 lines Fixed rowcount for SELECT statements. They're -1 now (again), for better DB-API 2.0 compliance. ........ r63887 | gregory.p.smith | 2008-06-02 06:05:52 +0200 (Mon, 02 Jun 2008) | 4 lines Fix issue 2782: be less strict about the format string type in strftime. Accept unicode and anything else ParseTuple "s#" can deal with. This matches the time.strftime behavior. ........ r63975 | neal.norwitz | 2008-06-06 06:47:01 +0200 (Fri, 06 Jun 2008) | 3 lines Aldo Cortesi confirmed this is still needed for OpenBSD 4.2 and 4.3. (I didn't regen configure, since I don't have a working autoconf.) ........ r63998 | raymond.hettinger | 2008-06-06 23:47:51 +0200 (Fri, 06 Jun 2008) | 1 line Issue 3501: Make heapq support both __le__ and __lt__. ........
18 years ago
Merged revisions 63542-63544,63546,63553,63563-63564,63567,63569,63576 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63542 | mark.dickinson | 2008-05-22 20:35:30 -0500 (Thu, 22 May 2008) | 5 lines Issue #2819: Add math.sum, a function that sums a sequence of floats efficiently but with no intermediate loss of precision. Based on Raymond Hettinger's ASPN recipe. Thanks Jean Brouwers for the patch. ........ r63543 | mark.dickinson | 2008-05-22 21:36:48 -0500 (Thu, 22 May 2008) | 2 lines Add tests for math.sum (Issue #2819) ........ r63544 | mark.dickinson | 2008-05-22 22:30:01 -0500 (Thu, 22 May 2008) | 2 lines Better error reporting in test_math.py ........ r63546 | raymond.hettinger | 2008-05-22 23:32:43 -0500 (Thu, 22 May 2008) | 1 line Tweak the comments and formatting. ........ r63553 | mark.dickinson | 2008-05-23 07:07:36 -0500 (Fri, 23 May 2008) | 3 lines Skip math.sum tests on non IEEE 754 platforms, and on IEEE 754 platforms that exhibit the problem described in issue #2937. ........ r63563 | martin.v.loewis | 2008-05-23 10:18:28 -0500 (Fri, 23 May 2008) | 3 lines Issue #1390: Raise ValueError in toxml when an invalid comment would otherwise be produced. ........ r63564 | raymond.hettinger | 2008-05-23 12:21:44 -0500 (Fri, 23 May 2008) | 1 line Issue 2909: show how to name unpacked fields. ........ r63567 | raymond.hettinger | 2008-05-23 12:34:34 -0500 (Fri, 23 May 2008) | 1 line Fix typo ........ r63569 | martin.v.loewis | 2008-05-23 14:33:13 -0500 (Fri, 23 May 2008) | 3 lines Mention that the leaking of variables from list comprehensions is fixed in 3.0. ........ r63576 | martin.v.loewis | 2008-05-24 04:36:45 -0500 (Sat, 24 May 2008) | 3 lines Don't try to get the window size if it was never set before. Fixes the test failure on Solaris. ........
18 years ago
Merged revisions 63542-63544,63546,63553,63563-63564,63567,63569,63576 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63542 | mark.dickinson | 2008-05-22 20:35:30 -0500 (Thu, 22 May 2008) | 5 lines Issue #2819: Add math.sum, a function that sums a sequence of floats efficiently but with no intermediate loss of precision. Based on Raymond Hettinger's ASPN recipe. Thanks Jean Brouwers for the patch. ........ r63543 | mark.dickinson | 2008-05-22 21:36:48 -0500 (Thu, 22 May 2008) | 2 lines Add tests for math.sum (Issue #2819) ........ r63544 | mark.dickinson | 2008-05-22 22:30:01 -0500 (Thu, 22 May 2008) | 2 lines Better error reporting in test_math.py ........ r63546 | raymond.hettinger | 2008-05-22 23:32:43 -0500 (Thu, 22 May 2008) | 1 line Tweak the comments and formatting. ........ r63553 | mark.dickinson | 2008-05-23 07:07:36 -0500 (Fri, 23 May 2008) | 3 lines Skip math.sum tests on non IEEE 754 platforms, and on IEEE 754 platforms that exhibit the problem described in issue #2937. ........ r63563 | martin.v.loewis | 2008-05-23 10:18:28 -0500 (Fri, 23 May 2008) | 3 lines Issue #1390: Raise ValueError in toxml when an invalid comment would otherwise be produced. ........ r63564 | raymond.hettinger | 2008-05-23 12:21:44 -0500 (Fri, 23 May 2008) | 1 line Issue 2909: show how to name unpacked fields. ........ r63567 | raymond.hettinger | 2008-05-23 12:34:34 -0500 (Fri, 23 May 2008) | 1 line Fix typo ........ r63569 | martin.v.loewis | 2008-05-23 14:33:13 -0500 (Fri, 23 May 2008) | 3 lines Mention that the leaking of variables from list comprehensions is fixed in 3.0. ........ r63576 | martin.v.loewis | 2008-05-24 04:36:45 -0500 (Sat, 24 May 2008) | 3 lines Don't try to get the window size if it was never set before. Fixes the test failure on Solaris. ........
18 years ago
Merged revisions 65258,65292,65299,65308-65309,65315,65326 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r65258 | mark.dickinson | 2008-07-27 08:15:29 +0100 (Sun, 27 Jul 2008) | 4 lines Remove math.sum tests related to overflow, special values, and behaviour near the extremes of the floating-point range. (The behaviour of math.sum should be regarded as undefined in these cases.) ........ r65292 | mark.dickinson | 2008-07-29 19:45:38 +0100 (Tue, 29 Jul 2008) | 4 lines More modifications to tests for math.sum: replace the Python version of msum by a version using a different algorithm, and use the new float.fromhex method to specify test results exactly. ........ r65299 | mark.dickinson | 2008-07-30 13:01:41 +0100 (Wed, 30 Jul 2008) | 5 lines Fix special-value handling for math.sum. Also minor cleanups to the code: fix tabbing, remove trailing whitespace, and reformat to fit into 80 columns. ........ r65308 | mark.dickinson | 2008-07-30 17:20:10 +0100 (Wed, 30 Jul 2008) | 2 lines Rename math.sum to math.fsum ........ r65309 | mark.dickinson | 2008-07-30 17:25:16 +0100 (Wed, 30 Jul 2008) | 3 lines Replace math.sum with math.fsum in a couple of comments that were missed by r65308 ........ r65315 | mark.dickinson | 2008-07-30 21:23:15 +0100 (Wed, 30 Jul 2008) | 2 lines Add note about problems with math.fsum on x86 hardware. ........ r65326 | mark.dickinson | 2008-07-31 15:48:32 +0100 (Thu, 31 Jul 2008) | 2 lines Rename testSum to testFsum and move it to proper place in test_math.py ........
18 years ago
Merged revisions 63542-63544,63546,63553,63563-63564,63567,63569,63576 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63542 | mark.dickinson | 2008-05-22 20:35:30 -0500 (Thu, 22 May 2008) | 5 lines Issue #2819: Add math.sum, a function that sums a sequence of floats efficiently but with no intermediate loss of precision. Based on Raymond Hettinger's ASPN recipe. Thanks Jean Brouwers for the patch. ........ r63543 | mark.dickinson | 2008-05-22 21:36:48 -0500 (Thu, 22 May 2008) | 2 lines Add tests for math.sum (Issue #2819) ........ r63544 | mark.dickinson | 2008-05-22 22:30:01 -0500 (Thu, 22 May 2008) | 2 lines Better error reporting in test_math.py ........ r63546 | raymond.hettinger | 2008-05-22 23:32:43 -0500 (Thu, 22 May 2008) | 1 line Tweak the comments and formatting. ........ r63553 | mark.dickinson | 2008-05-23 07:07:36 -0500 (Fri, 23 May 2008) | 3 lines Skip math.sum tests on non IEEE 754 platforms, and on IEEE 754 platforms that exhibit the problem described in issue #2937. ........ r63563 | martin.v.loewis | 2008-05-23 10:18:28 -0500 (Fri, 23 May 2008) | 3 lines Issue #1390: Raise ValueError in toxml when an invalid comment would otherwise be produced. ........ r63564 | raymond.hettinger | 2008-05-23 12:21:44 -0500 (Fri, 23 May 2008) | 1 line Issue 2909: show how to name unpacked fields. ........ r63567 | raymond.hettinger | 2008-05-23 12:34:34 -0500 (Fri, 23 May 2008) | 1 line Fix typo ........ r63569 | martin.v.loewis | 2008-05-23 14:33:13 -0500 (Fri, 23 May 2008) | 3 lines Mention that the leaking of variables from list comprehensions is fixed in 3.0. ........ r63576 | martin.v.loewis | 2008-05-24 04:36:45 -0500 (Sat, 24 May 2008) | 3 lines Don't try to get the window size if it was never set before. Fixes the test failure on Solaris. ........
18 years ago
Merged revisions 63542-63544,63546,63553,63563-63564,63567,63569,63576 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63542 | mark.dickinson | 2008-05-22 20:35:30 -0500 (Thu, 22 May 2008) | 5 lines Issue #2819: Add math.sum, a function that sums a sequence of floats efficiently but with no intermediate loss of precision. Based on Raymond Hettinger's ASPN recipe. Thanks Jean Brouwers for the patch. ........ r63543 | mark.dickinson | 2008-05-22 21:36:48 -0500 (Thu, 22 May 2008) | 2 lines Add tests for math.sum (Issue #2819) ........ r63544 | mark.dickinson | 2008-05-22 22:30:01 -0500 (Thu, 22 May 2008) | 2 lines Better error reporting in test_math.py ........ r63546 | raymond.hettinger | 2008-05-22 23:32:43 -0500 (Thu, 22 May 2008) | 1 line Tweak the comments and formatting. ........ r63553 | mark.dickinson | 2008-05-23 07:07:36 -0500 (Fri, 23 May 2008) | 3 lines Skip math.sum tests on non IEEE 754 platforms, and on IEEE 754 platforms that exhibit the problem described in issue #2937. ........ r63563 | martin.v.loewis | 2008-05-23 10:18:28 -0500 (Fri, 23 May 2008) | 3 lines Issue #1390: Raise ValueError in toxml when an invalid comment would otherwise be produced. ........ r63564 | raymond.hettinger | 2008-05-23 12:21:44 -0500 (Fri, 23 May 2008) | 1 line Issue 2909: show how to name unpacked fields. ........ r63567 | raymond.hettinger | 2008-05-23 12:34:34 -0500 (Fri, 23 May 2008) | 1 line Fix typo ........ r63569 | martin.v.loewis | 2008-05-23 14:33:13 -0500 (Fri, 23 May 2008) | 3 lines Mention that the leaking of variables from list comprehensions is fixed in 3.0. ........ r63576 | martin.v.loewis | 2008-05-24 04:36:45 -0500 (Sat, 24 May 2008) | 3 lines Don't try to get the window size if it was never set before. Fixes the test failure on Solaris. ........
18 years ago
Merged revisions 63542-63544,63546,63553,63563-63564,63567,63569,63576 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63542 | mark.dickinson | 2008-05-22 20:35:30 -0500 (Thu, 22 May 2008) | 5 lines Issue #2819: Add math.sum, a function that sums a sequence of floats efficiently but with no intermediate loss of precision. Based on Raymond Hettinger's ASPN recipe. Thanks Jean Brouwers for the patch. ........ r63543 | mark.dickinson | 2008-05-22 21:36:48 -0500 (Thu, 22 May 2008) | 2 lines Add tests for math.sum (Issue #2819) ........ r63544 | mark.dickinson | 2008-05-22 22:30:01 -0500 (Thu, 22 May 2008) | 2 lines Better error reporting in test_math.py ........ r63546 | raymond.hettinger | 2008-05-22 23:32:43 -0500 (Thu, 22 May 2008) | 1 line Tweak the comments and formatting. ........ r63553 | mark.dickinson | 2008-05-23 07:07:36 -0500 (Fri, 23 May 2008) | 3 lines Skip math.sum tests on non IEEE 754 platforms, and on IEEE 754 platforms that exhibit the problem described in issue #2937. ........ r63563 | martin.v.loewis | 2008-05-23 10:18:28 -0500 (Fri, 23 May 2008) | 3 lines Issue #1390: Raise ValueError in toxml when an invalid comment would otherwise be produced. ........ r63564 | raymond.hettinger | 2008-05-23 12:21:44 -0500 (Fri, 23 May 2008) | 1 line Issue 2909: show how to name unpacked fields. ........ r63567 | raymond.hettinger | 2008-05-23 12:34:34 -0500 (Fri, 23 May 2008) | 1 line Fix typo ........ r63569 | martin.v.loewis | 2008-05-23 14:33:13 -0500 (Fri, 23 May 2008) | 3 lines Mention that the leaking of variables from list comprehensions is fixed in 3.0. ........ r63576 | martin.v.loewis | 2008-05-24 04:36:45 -0500 (Sat, 24 May 2008) | 3 lines Don't try to get the window size if it was never set before. Fixes the test failure on Solaris. ........
18 years ago
Merged revisions 63542-63544,63546,63553,63563-63564,63567,63569,63576 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63542 | mark.dickinson | 2008-05-22 20:35:30 -0500 (Thu, 22 May 2008) | 5 lines Issue #2819: Add math.sum, a function that sums a sequence of floats efficiently but with no intermediate loss of precision. Based on Raymond Hettinger's ASPN recipe. Thanks Jean Brouwers for the patch. ........ r63543 | mark.dickinson | 2008-05-22 21:36:48 -0500 (Thu, 22 May 2008) | 2 lines Add tests for math.sum (Issue #2819) ........ r63544 | mark.dickinson | 2008-05-22 22:30:01 -0500 (Thu, 22 May 2008) | 2 lines Better error reporting in test_math.py ........ r63546 | raymond.hettinger | 2008-05-22 23:32:43 -0500 (Thu, 22 May 2008) | 1 line Tweak the comments and formatting. ........ r63553 | mark.dickinson | 2008-05-23 07:07:36 -0500 (Fri, 23 May 2008) | 3 lines Skip math.sum tests on non IEEE 754 platforms, and on IEEE 754 platforms that exhibit the problem described in issue #2937. ........ r63563 | martin.v.loewis | 2008-05-23 10:18:28 -0500 (Fri, 23 May 2008) | 3 lines Issue #1390: Raise ValueError in toxml when an invalid comment would otherwise be produced. ........ r63564 | raymond.hettinger | 2008-05-23 12:21:44 -0500 (Fri, 23 May 2008) | 1 line Issue 2909: show how to name unpacked fields. ........ r63567 | raymond.hettinger | 2008-05-23 12:34:34 -0500 (Fri, 23 May 2008) | 1 line Fix typo ........ r63569 | martin.v.loewis | 2008-05-23 14:33:13 -0500 (Fri, 23 May 2008) | 3 lines Mention that the leaking of variables from list comprehensions is fixed in 3.0. ........ r63576 | martin.v.loewis | 2008-05-24 04:36:45 -0500 (Sat, 24 May 2008) | 3 lines Don't try to get the window size if it was never set before. Fixes the test failure on Solaris. ........
18 years ago
Merged revisions 63542-63544,63546,63553,63563-63564,63567,63569,63576 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63542 | mark.dickinson | 2008-05-22 20:35:30 -0500 (Thu, 22 May 2008) | 5 lines Issue #2819: Add math.sum, a function that sums a sequence of floats efficiently but with no intermediate loss of precision. Based on Raymond Hettinger's ASPN recipe. Thanks Jean Brouwers for the patch. ........ r63543 | mark.dickinson | 2008-05-22 21:36:48 -0500 (Thu, 22 May 2008) | 2 lines Add tests for math.sum (Issue #2819) ........ r63544 | mark.dickinson | 2008-05-22 22:30:01 -0500 (Thu, 22 May 2008) | 2 lines Better error reporting in test_math.py ........ r63546 | raymond.hettinger | 2008-05-22 23:32:43 -0500 (Thu, 22 May 2008) | 1 line Tweak the comments and formatting. ........ r63553 | mark.dickinson | 2008-05-23 07:07:36 -0500 (Fri, 23 May 2008) | 3 lines Skip math.sum tests on non IEEE 754 platforms, and on IEEE 754 platforms that exhibit the problem described in issue #2937. ........ r63563 | martin.v.loewis | 2008-05-23 10:18:28 -0500 (Fri, 23 May 2008) | 3 lines Issue #1390: Raise ValueError in toxml when an invalid comment would otherwise be produced. ........ r63564 | raymond.hettinger | 2008-05-23 12:21:44 -0500 (Fri, 23 May 2008) | 1 line Issue 2909: show how to name unpacked fields. ........ r63567 | raymond.hettinger | 2008-05-23 12:34:34 -0500 (Fri, 23 May 2008) | 1 line Fix typo ........ r63569 | martin.v.loewis | 2008-05-23 14:33:13 -0500 (Fri, 23 May 2008) | 3 lines Mention that the leaking of variables from list comprehensions is fixed in 3.0. ........ r63576 | martin.v.loewis | 2008-05-24 04:36:45 -0500 (Sat, 24 May 2008) | 3 lines Don't try to get the window size if it was never set before. Fixes the test failure on Solaris. ........
18 years ago
Merged revisions 63542-63544,63546,63553,63563-63564,63567,63569,63576 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63542 | mark.dickinson | 2008-05-22 20:35:30 -0500 (Thu, 22 May 2008) | 5 lines Issue #2819: Add math.sum, a function that sums a sequence of floats efficiently but with no intermediate loss of precision. Based on Raymond Hettinger's ASPN recipe. Thanks Jean Brouwers for the patch. ........ r63543 | mark.dickinson | 2008-05-22 21:36:48 -0500 (Thu, 22 May 2008) | 2 lines Add tests for math.sum (Issue #2819) ........ r63544 | mark.dickinson | 2008-05-22 22:30:01 -0500 (Thu, 22 May 2008) | 2 lines Better error reporting in test_math.py ........ r63546 | raymond.hettinger | 2008-05-22 23:32:43 -0500 (Thu, 22 May 2008) | 1 line Tweak the comments and formatting. ........ r63553 | mark.dickinson | 2008-05-23 07:07:36 -0500 (Fri, 23 May 2008) | 3 lines Skip math.sum tests on non IEEE 754 platforms, and on IEEE 754 platforms that exhibit the problem described in issue #2937. ........ r63563 | martin.v.loewis | 2008-05-23 10:18:28 -0500 (Fri, 23 May 2008) | 3 lines Issue #1390: Raise ValueError in toxml when an invalid comment would otherwise be produced. ........ r63564 | raymond.hettinger | 2008-05-23 12:21:44 -0500 (Fri, 23 May 2008) | 1 line Issue 2909: show how to name unpacked fields. ........ r63567 | raymond.hettinger | 2008-05-23 12:34:34 -0500 (Fri, 23 May 2008) | 1 line Fix typo ........ r63569 | martin.v.loewis | 2008-05-23 14:33:13 -0500 (Fri, 23 May 2008) | 3 lines Mention that the leaking of variables from list comprehensions is fixed in 3.0. ........ r63576 | martin.v.loewis | 2008-05-24 04:36:45 -0500 (Sat, 24 May 2008) | 3 lines Don't try to get the window size if it was never set before. Fixes the test failure on Solaris. ........
18 years ago
Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64002 | travis.oliphant | 2008-06-07 00:33:21 +0200 (Sat, 07 Jun 2008) | 1 line Add long double check support to configure test. ........ r64003 | travis.oliphant | 2008-06-07 00:39:47 +0200 (Sat, 07 Jun 2008) | 1 line Remove locking part of new buffer protocol. ........ r64012 | facundo.batista | 2008-06-07 15:36:36 +0200 (Sat, 07 Jun 2008) | 4 lines Finished bug #2451. Fixed the retrying part to make it more robust. ........ r64036 | georg.brandl | 2008-06-08 10:54:40 +0200 (Sun, 08 Jun 2008) | 2 lines #3028: tokenize passes the physical line. ........ r64037 | georg.brandl | 2008-06-08 10:59:38 +0200 (Sun, 08 Jun 2008) | 2 lines Argh, I read it wrong. Reverted 64036 and added a clarifying remark. ........ r64047 | raymond.hettinger | 2008-06-09 03:28:30 +0200 (Mon, 09 Jun 2008) | 1 line Issue3065: Fixed pickling of named tuples. Added tests. ........ r64050 | raymond.hettinger | 2008-06-09 08:54:45 +0200 (Mon, 09 Jun 2008) | 1 line Issue #2138: Add math.factorial(). ........ r64051 | raymond.hettinger | 2008-06-09 10:33:37 +0200 (Mon, 09 Jun 2008) | 1 line Let set.union() and set.update() accept multiple inputs. ........ r64052 | raymond.hettinger | 2008-06-09 11:29:17 +0200 (Mon, 09 Jun 2008) | 1 line Address double-rounding scenarios by setting all variables to long doubles. ........ r64054 | raymond.hettinger | 2008-06-09 13:24:47 +0200 (Mon, 09 Jun 2008) | 1 line Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds. ........ r64055 | raymond.hettinger | 2008-06-09 15:07:27 +0200 (Mon, 09 Jun 2008) | 1 line Let set.intersection() and set.intersection_update() take multiple input arguments. ........ r64066 | robert.schuppenies | 2008-06-10 12:10:31 +0200 (Tue, 10 Jun 2008) | 2 lines Issue 3048: Fixed sys.getsizeof for unicode objects. ........ r64071 | thomas.heller | 2008-06-10 16:07:12 +0200 (Tue, 10 Jun 2008) | 3 lines NEWS entry for: Add an optional 'offset' parameter to byref, defaulting to zero. ........
18 years ago
Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64002 | travis.oliphant | 2008-06-07 00:33:21 +0200 (Sat, 07 Jun 2008) | 1 line Add long double check support to configure test. ........ r64003 | travis.oliphant | 2008-06-07 00:39:47 +0200 (Sat, 07 Jun 2008) | 1 line Remove locking part of new buffer protocol. ........ r64012 | facundo.batista | 2008-06-07 15:36:36 +0200 (Sat, 07 Jun 2008) | 4 lines Finished bug #2451. Fixed the retrying part to make it more robust. ........ r64036 | georg.brandl | 2008-06-08 10:54:40 +0200 (Sun, 08 Jun 2008) | 2 lines #3028: tokenize passes the physical line. ........ r64037 | georg.brandl | 2008-06-08 10:59:38 +0200 (Sun, 08 Jun 2008) | 2 lines Argh, I read it wrong. Reverted 64036 and added a clarifying remark. ........ r64047 | raymond.hettinger | 2008-06-09 03:28:30 +0200 (Mon, 09 Jun 2008) | 1 line Issue3065: Fixed pickling of named tuples. Added tests. ........ r64050 | raymond.hettinger | 2008-06-09 08:54:45 +0200 (Mon, 09 Jun 2008) | 1 line Issue #2138: Add math.factorial(). ........ r64051 | raymond.hettinger | 2008-06-09 10:33:37 +0200 (Mon, 09 Jun 2008) | 1 line Let set.union() and set.update() accept multiple inputs. ........ r64052 | raymond.hettinger | 2008-06-09 11:29:17 +0200 (Mon, 09 Jun 2008) | 1 line Address double-rounding scenarios by setting all variables to long doubles. ........ r64054 | raymond.hettinger | 2008-06-09 13:24:47 +0200 (Mon, 09 Jun 2008) | 1 line Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds. ........ r64055 | raymond.hettinger | 2008-06-09 15:07:27 +0200 (Mon, 09 Jun 2008) | 1 line Let set.intersection() and set.intersection_update() take multiple input arguments. ........ r64066 | robert.schuppenies | 2008-06-10 12:10:31 +0200 (Tue, 10 Jun 2008) | 2 lines Issue 3048: Fixed sys.getsizeof for unicode objects. ........ r64071 | thomas.heller | 2008-06-10 16:07:12 +0200 (Tue, 10 Jun 2008) | 3 lines NEWS entry for: Add an optional 'offset' parameter to byref, defaulting to zero. ........
18 years ago
Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64002 | travis.oliphant | 2008-06-07 00:33:21 +0200 (Sat, 07 Jun 2008) | 1 line Add long double check support to configure test. ........ r64003 | travis.oliphant | 2008-06-07 00:39:47 +0200 (Sat, 07 Jun 2008) | 1 line Remove locking part of new buffer protocol. ........ r64012 | facundo.batista | 2008-06-07 15:36:36 +0200 (Sat, 07 Jun 2008) | 4 lines Finished bug #2451. Fixed the retrying part to make it more robust. ........ r64036 | georg.brandl | 2008-06-08 10:54:40 +0200 (Sun, 08 Jun 2008) | 2 lines #3028: tokenize passes the physical line. ........ r64037 | georg.brandl | 2008-06-08 10:59:38 +0200 (Sun, 08 Jun 2008) | 2 lines Argh, I read it wrong. Reverted 64036 and added a clarifying remark. ........ r64047 | raymond.hettinger | 2008-06-09 03:28:30 +0200 (Mon, 09 Jun 2008) | 1 line Issue3065: Fixed pickling of named tuples. Added tests. ........ r64050 | raymond.hettinger | 2008-06-09 08:54:45 +0200 (Mon, 09 Jun 2008) | 1 line Issue #2138: Add math.factorial(). ........ r64051 | raymond.hettinger | 2008-06-09 10:33:37 +0200 (Mon, 09 Jun 2008) | 1 line Let set.union() and set.update() accept multiple inputs. ........ r64052 | raymond.hettinger | 2008-06-09 11:29:17 +0200 (Mon, 09 Jun 2008) | 1 line Address double-rounding scenarios by setting all variables to long doubles. ........ r64054 | raymond.hettinger | 2008-06-09 13:24:47 +0200 (Mon, 09 Jun 2008) | 1 line Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds. ........ r64055 | raymond.hettinger | 2008-06-09 15:07:27 +0200 (Mon, 09 Jun 2008) | 1 line Let set.intersection() and set.intersection_update() take multiple input arguments. ........ r64066 | robert.schuppenies | 2008-06-10 12:10:31 +0200 (Tue, 10 Jun 2008) | 2 lines Issue 3048: Fixed sys.getsizeof for unicode objects. ........ r64071 | thomas.heller | 2008-06-10 16:07:12 +0200 (Tue, 10 Jun 2008) | 3 lines NEWS entry for: Add an optional 'offset' parameter to byref, defaulting to zero. ........
18 years ago
Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64002 | travis.oliphant | 2008-06-07 00:33:21 +0200 (Sat, 07 Jun 2008) | 1 line Add long double check support to configure test. ........ r64003 | travis.oliphant | 2008-06-07 00:39:47 +0200 (Sat, 07 Jun 2008) | 1 line Remove locking part of new buffer protocol. ........ r64012 | facundo.batista | 2008-06-07 15:36:36 +0200 (Sat, 07 Jun 2008) | 4 lines Finished bug #2451. Fixed the retrying part to make it more robust. ........ r64036 | georg.brandl | 2008-06-08 10:54:40 +0200 (Sun, 08 Jun 2008) | 2 lines #3028: tokenize passes the physical line. ........ r64037 | georg.brandl | 2008-06-08 10:59:38 +0200 (Sun, 08 Jun 2008) | 2 lines Argh, I read it wrong. Reverted 64036 and added a clarifying remark. ........ r64047 | raymond.hettinger | 2008-06-09 03:28:30 +0200 (Mon, 09 Jun 2008) | 1 line Issue3065: Fixed pickling of named tuples. Added tests. ........ r64050 | raymond.hettinger | 2008-06-09 08:54:45 +0200 (Mon, 09 Jun 2008) | 1 line Issue #2138: Add math.factorial(). ........ r64051 | raymond.hettinger | 2008-06-09 10:33:37 +0200 (Mon, 09 Jun 2008) | 1 line Let set.union() and set.update() accept multiple inputs. ........ r64052 | raymond.hettinger | 2008-06-09 11:29:17 +0200 (Mon, 09 Jun 2008) | 1 line Address double-rounding scenarios by setting all variables to long doubles. ........ r64054 | raymond.hettinger | 2008-06-09 13:24:47 +0200 (Mon, 09 Jun 2008) | 1 line Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds. ........ r64055 | raymond.hettinger | 2008-06-09 15:07:27 +0200 (Mon, 09 Jun 2008) | 1 line Let set.intersection() and set.intersection_update() take multiple input arguments. ........ r64066 | robert.schuppenies | 2008-06-10 12:10:31 +0200 (Tue, 10 Jun 2008) | 2 lines Issue 3048: Fixed sys.getsizeof for unicode objects. ........ r64071 | thomas.heller | 2008-06-10 16:07:12 +0200 (Tue, 10 Jun 2008) | 3 lines NEWS entry for: Add an optional 'offset' parameter to byref, defaulting to zero. ........
18 years ago
30 years ago
30 years ago
30 years ago
36 years ago
Merged revisions 59666-59679 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59666 | christian.heimes | 2008-01-02 19:28:32 +0100 (Wed, 02 Jan 2008) | 1 line Made vs9to8 Unix compatible ........ r59669 | guido.van.rossum | 2008-01-02 20:00:46 +0100 (Wed, 02 Jan 2008) | 2 lines Patch #1696. Don't attempt to close None in dry-run mode. ........ r59671 | jeffrey.yasskin | 2008-01-03 03:21:52 +0100 (Thu, 03 Jan 2008) | 6 lines Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361, r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new documentation. The only significant difference is that round(x) returns a float to preserve backward-compatibility. See http://bugs.python.org/issue1689. ........ r59672 | christian.heimes | 2008-01-03 16:41:30 +0100 (Thu, 03 Jan 2008) | 1 line Issue #1726: Remove Python/atof.c from PCBuild/pythoncore.vcproj ........ r59675 | guido.van.rossum | 2008-01-03 20:12:44 +0100 (Thu, 03 Jan 2008) | 4 lines Issue #1700, reported by Nguyen Quan Son, fix by Fredruk Lundh: Regular Expression inline flags not handled correctly for some unicode characters. (Forward port from 2.5.2.) ........ r59676 | christian.heimes | 2008-01-03 21:23:15 +0100 (Thu, 03 Jan 2008) | 1 line Added math.isinf() and math.isnan() ........ r59677 | christian.heimes | 2008-01-03 22:14:48 +0100 (Thu, 03 Jan 2008) | 1 line Some build bots don't compile mathmodule. There is an issue with the long definition of pi and euler ........ r59678 | christian.heimes | 2008-01-03 23:16:32 +0100 (Thu, 03 Jan 2008) | 2 lines Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0 Added a new API function PyImport_ImportModuleNoBlock. It solves the problem with dead locks when mixing threads and imports ........ r59679 | christian.heimes | 2008-01-03 23:32:26 +0100 (Thu, 03 Jan 2008) | 1 line Added copysign(x, y) function to the math module ........
18 years ago
Merged revisions 59666-59679 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59666 | christian.heimes | 2008-01-02 19:28:32 +0100 (Wed, 02 Jan 2008) | 1 line Made vs9to8 Unix compatible ........ r59669 | guido.van.rossum | 2008-01-02 20:00:46 +0100 (Wed, 02 Jan 2008) | 2 lines Patch #1696. Don't attempt to close None in dry-run mode. ........ r59671 | jeffrey.yasskin | 2008-01-03 03:21:52 +0100 (Thu, 03 Jan 2008) | 6 lines Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361, r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new documentation. The only significant difference is that round(x) returns a float to preserve backward-compatibility. See http://bugs.python.org/issue1689. ........ r59672 | christian.heimes | 2008-01-03 16:41:30 +0100 (Thu, 03 Jan 2008) | 1 line Issue #1726: Remove Python/atof.c from PCBuild/pythoncore.vcproj ........ r59675 | guido.van.rossum | 2008-01-03 20:12:44 +0100 (Thu, 03 Jan 2008) | 4 lines Issue #1700, reported by Nguyen Quan Son, fix by Fredruk Lundh: Regular Expression inline flags not handled correctly for some unicode characters. (Forward port from 2.5.2.) ........ r59676 | christian.heimes | 2008-01-03 21:23:15 +0100 (Thu, 03 Jan 2008) | 1 line Added math.isinf() and math.isnan() ........ r59677 | christian.heimes | 2008-01-03 22:14:48 +0100 (Thu, 03 Jan 2008) | 1 line Some build bots don't compile mathmodule. There is an issue with the long definition of pi and euler ........ r59678 | christian.heimes | 2008-01-03 23:16:32 +0100 (Thu, 03 Jan 2008) | 2 lines Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0 Added a new API function PyImport_ImportModuleNoBlock. It solves the problem with dead locks when mixing threads and imports ........ r59679 | christian.heimes | 2008-01-03 23:32:26 +0100 (Thu, 03 Jan 2008) | 1 line Added copysign(x, y) function to the math module ........
18 years ago
Merged revisions 59666-59679 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59666 | christian.heimes | 2008-01-02 19:28:32 +0100 (Wed, 02 Jan 2008) | 1 line Made vs9to8 Unix compatible ........ r59669 | guido.van.rossum | 2008-01-02 20:00:46 +0100 (Wed, 02 Jan 2008) | 2 lines Patch #1696. Don't attempt to close None in dry-run mode. ........ r59671 | jeffrey.yasskin | 2008-01-03 03:21:52 +0100 (Thu, 03 Jan 2008) | 6 lines Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361, r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new documentation. The only significant difference is that round(x) returns a float to preserve backward-compatibility. See http://bugs.python.org/issue1689. ........ r59672 | christian.heimes | 2008-01-03 16:41:30 +0100 (Thu, 03 Jan 2008) | 1 line Issue #1726: Remove Python/atof.c from PCBuild/pythoncore.vcproj ........ r59675 | guido.van.rossum | 2008-01-03 20:12:44 +0100 (Thu, 03 Jan 2008) | 4 lines Issue #1700, reported by Nguyen Quan Son, fix by Fredruk Lundh: Regular Expression inline flags not handled correctly for some unicode characters. (Forward port from 2.5.2.) ........ r59676 | christian.heimes | 2008-01-03 21:23:15 +0100 (Thu, 03 Jan 2008) | 1 line Added math.isinf() and math.isnan() ........ r59677 | christian.heimes | 2008-01-03 22:14:48 +0100 (Thu, 03 Jan 2008) | 1 line Some build bots don't compile mathmodule. There is an issue with the long definition of pi and euler ........ r59678 | christian.heimes | 2008-01-03 23:16:32 +0100 (Thu, 03 Jan 2008) | 2 lines Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0 Added a new API function PyImport_ImportModuleNoBlock. It solves the problem with dead locks when mixing threads and imports ........ r59679 | christian.heimes | 2008-01-03 23:32:26 +0100 (Thu, 03 Jan 2008) | 1 line Added copysign(x, y) function to the math module ........
18 years ago
Merged revisions 59666-59679 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59666 | christian.heimes | 2008-01-02 19:28:32 +0100 (Wed, 02 Jan 2008) | 1 line Made vs9to8 Unix compatible ........ r59669 | guido.van.rossum | 2008-01-02 20:00:46 +0100 (Wed, 02 Jan 2008) | 2 lines Patch #1696. Don't attempt to close None in dry-run mode. ........ r59671 | jeffrey.yasskin | 2008-01-03 03:21:52 +0100 (Thu, 03 Jan 2008) | 6 lines Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361, r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new documentation. The only significant difference is that round(x) returns a float to preserve backward-compatibility. See http://bugs.python.org/issue1689. ........ r59672 | christian.heimes | 2008-01-03 16:41:30 +0100 (Thu, 03 Jan 2008) | 1 line Issue #1726: Remove Python/atof.c from PCBuild/pythoncore.vcproj ........ r59675 | guido.van.rossum | 2008-01-03 20:12:44 +0100 (Thu, 03 Jan 2008) | 4 lines Issue #1700, reported by Nguyen Quan Son, fix by Fredruk Lundh: Regular Expression inline flags not handled correctly for some unicode characters. (Forward port from 2.5.2.) ........ r59676 | christian.heimes | 2008-01-03 21:23:15 +0100 (Thu, 03 Jan 2008) | 1 line Added math.isinf() and math.isnan() ........ r59677 | christian.heimes | 2008-01-03 22:14:48 +0100 (Thu, 03 Jan 2008) | 1 line Some build bots don't compile mathmodule. There is an issue with the long definition of pi and euler ........ r59678 | christian.heimes | 2008-01-03 23:16:32 +0100 (Thu, 03 Jan 2008) | 2 lines Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0 Added a new API function PyImport_ImportModuleNoBlock. It solves the problem with dead locks when mixing threads and imports ........ r59679 | christian.heimes | 2008-01-03 23:32:26 +0100 (Thu, 03 Jan 2008) | 1 line Added copysign(x, y) function to the math module ........
18 years ago
Merged revisions 59666-59679 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59666 | christian.heimes | 2008-01-02 19:28:32 +0100 (Wed, 02 Jan 2008) | 1 line Made vs9to8 Unix compatible ........ r59669 | guido.van.rossum | 2008-01-02 20:00:46 +0100 (Wed, 02 Jan 2008) | 2 lines Patch #1696. Don't attempt to close None in dry-run mode. ........ r59671 | jeffrey.yasskin | 2008-01-03 03:21:52 +0100 (Thu, 03 Jan 2008) | 6 lines Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361, r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new documentation. The only significant difference is that round(x) returns a float to preserve backward-compatibility. See http://bugs.python.org/issue1689. ........ r59672 | christian.heimes | 2008-01-03 16:41:30 +0100 (Thu, 03 Jan 2008) | 1 line Issue #1726: Remove Python/atof.c from PCBuild/pythoncore.vcproj ........ r59675 | guido.van.rossum | 2008-01-03 20:12:44 +0100 (Thu, 03 Jan 2008) | 4 lines Issue #1700, reported by Nguyen Quan Son, fix by Fredruk Lundh: Regular Expression inline flags not handled correctly for some unicode characters. (Forward port from 2.5.2.) ........ r59676 | christian.heimes | 2008-01-03 21:23:15 +0100 (Thu, 03 Jan 2008) | 1 line Added math.isinf() and math.isnan() ........ r59677 | christian.heimes | 2008-01-03 22:14:48 +0100 (Thu, 03 Jan 2008) | 1 line Some build bots don't compile mathmodule. There is an issue with the long definition of pi and euler ........ r59678 | christian.heimes | 2008-01-03 23:16:32 +0100 (Thu, 03 Jan 2008) | 2 lines Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0 Added a new API function PyImport_ImportModuleNoBlock. It solves the problem with dead locks when mixing threads and imports ........ r59679 | christian.heimes | 2008-01-03 23:32:26 +0100 (Thu, 03 Jan 2008) | 1 line Added copysign(x, y) function to the math module ........
18 years ago
Merged revisions 59666-59679 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59666 | christian.heimes | 2008-01-02 19:28:32 +0100 (Wed, 02 Jan 2008) | 1 line Made vs9to8 Unix compatible ........ r59669 | guido.van.rossum | 2008-01-02 20:00:46 +0100 (Wed, 02 Jan 2008) | 2 lines Patch #1696. Don't attempt to close None in dry-run mode. ........ r59671 | jeffrey.yasskin | 2008-01-03 03:21:52 +0100 (Thu, 03 Jan 2008) | 6 lines Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361, r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new documentation. The only significant difference is that round(x) returns a float to preserve backward-compatibility. See http://bugs.python.org/issue1689. ........ r59672 | christian.heimes | 2008-01-03 16:41:30 +0100 (Thu, 03 Jan 2008) | 1 line Issue #1726: Remove Python/atof.c from PCBuild/pythoncore.vcproj ........ r59675 | guido.van.rossum | 2008-01-03 20:12:44 +0100 (Thu, 03 Jan 2008) | 4 lines Issue #1700, reported by Nguyen Quan Son, fix by Fredruk Lundh: Regular Expression inline flags not handled correctly for some unicode characters. (Forward port from 2.5.2.) ........ r59676 | christian.heimes | 2008-01-03 21:23:15 +0100 (Thu, 03 Jan 2008) | 1 line Added math.isinf() and math.isnan() ........ r59677 | christian.heimes | 2008-01-03 22:14:48 +0100 (Thu, 03 Jan 2008) | 1 line Some build bots don't compile mathmodule. There is an issue with the long definition of pi and euler ........ r59678 | christian.heimes | 2008-01-03 23:16:32 +0100 (Thu, 03 Jan 2008) | 2 lines Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0 Added a new API function PyImport_ImportModuleNoBlock. It solves the problem with dead locks when mixing threads and imports ........ r59679 | christian.heimes | 2008-01-03 23:32:26 +0100 (Thu, 03 Jan 2008) | 1 line Added copysign(x, y) function to the math module ........
18 years ago
Merged revisions 59666-59679 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59666 | christian.heimes | 2008-01-02 19:28:32 +0100 (Wed, 02 Jan 2008) | 1 line Made vs9to8 Unix compatible ........ r59669 | guido.van.rossum | 2008-01-02 20:00:46 +0100 (Wed, 02 Jan 2008) | 2 lines Patch #1696. Don't attempt to close None in dry-run mode. ........ r59671 | jeffrey.yasskin | 2008-01-03 03:21:52 +0100 (Thu, 03 Jan 2008) | 6 lines Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361, r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new documentation. The only significant difference is that round(x) returns a float to preserve backward-compatibility. See http://bugs.python.org/issue1689. ........ r59672 | christian.heimes | 2008-01-03 16:41:30 +0100 (Thu, 03 Jan 2008) | 1 line Issue #1726: Remove Python/atof.c from PCBuild/pythoncore.vcproj ........ r59675 | guido.van.rossum | 2008-01-03 20:12:44 +0100 (Thu, 03 Jan 2008) | 4 lines Issue #1700, reported by Nguyen Quan Son, fix by Fredruk Lundh: Regular Expression inline flags not handled correctly for some unicode characters. (Forward port from 2.5.2.) ........ r59676 | christian.heimes | 2008-01-03 21:23:15 +0100 (Thu, 03 Jan 2008) | 1 line Added math.isinf() and math.isnan() ........ r59677 | christian.heimes | 2008-01-03 22:14:48 +0100 (Thu, 03 Jan 2008) | 1 line Some build bots don't compile mathmodule. There is an issue with the long definition of pi and euler ........ r59678 | christian.heimes | 2008-01-03 23:16:32 +0100 (Thu, 03 Jan 2008) | 2 lines Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0 Added a new API function PyImport_ImportModuleNoBlock. It solves the problem with dead locks when mixing threads and imports ........ r59679 | christian.heimes | 2008-01-03 23:32:26 +0100 (Thu, 03 Jan 2008) | 1 line Added copysign(x, y) function to the math module ........
18 years ago
30 years ago
36 years ago
36 years ago
36 years ago
  1. /* Math module -- standard C math library functions, pi and e */
  2. /* Here are some comments from Tim Peters, extracted from the
  3. discussion attached to http://bugs.python.org/issue1640. They
  4. describe the general aims of the math module with respect to
  5. special values, IEEE-754 floating-point exceptions, and Python
  6. exceptions.
  7. These are the "spirit of 754" rules:
  8. 1. If the mathematical result is a real number, but of magnitude too
  9. large to approximate by a machine float, overflow is signaled and the
  10. result is an infinity (with the appropriate sign).
  11. 2. If the mathematical result is a real number, but of magnitude too
  12. small to approximate by a machine float, underflow is signaled and the
  13. result is a zero (with the appropriate sign).
  14. 3. At a singularity (a value x such that the limit of f(y) as y
  15. approaches x exists and is an infinity), "divide by zero" is signaled
  16. and the result is an infinity (with the appropriate sign). This is
  17. complicated a little by that the left-side and right-side limits may
  18. not be the same; e.g., 1/x approaches +inf or -inf as x approaches 0
  19. from the positive or negative directions. In that specific case, the
  20. sign of the zero determines the result of 1/0.
  21. 4. At a point where a function has no defined result in the extended
  22. reals (i.e., the reals plus an infinity or two), invalid operation is
  23. signaled and a NaN is returned.
  24. And these are what Python has historically /tried/ to do (but not
  25. always successfully, as platform libm behavior varies a lot):
  26. For #1, raise OverflowError.
  27. For #2, return a zero (with the appropriate sign if that happens by
  28. accident ;-)).
  29. For #3 and #4, raise ValueError. It may have made sense to raise
  30. Python's ZeroDivisionError in #3, but historically that's only been
  31. raised for division by zero and mod by zero.
  32. */
  33. /*
  34. In general, on an IEEE-754 platform the aim is to follow the C99
  35. standard, including Annex 'F', whenever possible. Where the
  36. standard recommends raising the 'divide-by-zero' or 'invalid'
  37. floating-point exceptions, Python should raise a ValueError. Where
  38. the standard recommends raising 'overflow', Python should raise an
  39. OverflowError. In all other circumstances a value should be
  40. returned.
  41. */
  42. #include "Python.h"
  43. #include "_math.h"
  44. #include "clinic/mathmodule.c.h"
  45. /*[clinic input]
  46. module math
  47. [clinic start generated code]*/
  48. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=76bc7002685dd942]*/
  49. /*
  50. sin(pi*x), giving accurate results for all finite x (especially x
  51. integral or close to an integer). This is here for use in the
  52. reflection formula for the gamma function. It conforms to IEEE
  53. 754-2008 for finite arguments, but not for infinities or nans.
  54. */
  55. static const double pi = 3.141592653589793238462643383279502884197;
  56. static const double logpi = 1.144729885849400174143427351353058711647;
  57. #if !defined(HAVE_ERF) || !defined(HAVE_ERFC)
  58. static const double sqrtpi = 1.772453850905516027298167483341145182798;
  59. #endif /* !defined(HAVE_ERF) || !defined(HAVE_ERFC) */
  60. /* Version of PyFloat_AsDouble() with in-line fast paths
  61. for exact floats and integers. Gives a substantial
  62. speed improvement for extracting float arguments.
  63. */
  64. #define ASSIGN_DOUBLE(target_var, obj, error_label) \
  65. if (PyFloat_CheckExact(obj)) { \
  66. target_var = PyFloat_AS_DOUBLE(obj); \
  67. } \
  68. else if (PyLong_CheckExact(obj)) { \
  69. target_var = PyLong_AsDouble(obj); \
  70. if (target_var == -1.0 && PyErr_Occurred()) { \
  71. goto error_label; \
  72. } \
  73. } \
  74. else { \
  75. target_var = PyFloat_AsDouble(obj); \
  76. if (target_var == -1.0 && PyErr_Occurred()) { \
  77. goto error_label; \
  78. } \
  79. }
  80. static double
  81. m_sinpi(double x)
  82. {
  83. double y, r;
  84. int n;
  85. /* this function should only ever be called for finite arguments */
  86. assert(Py_IS_FINITE(x));
  87. y = fmod(fabs(x), 2.0);
  88. n = (int)round(2.0*y);
  89. assert(0 <= n && n <= 4);
  90. switch (n) {
  91. case 0:
  92. r = sin(pi*y);
  93. break;
  94. case 1:
  95. r = cos(pi*(y-0.5));
  96. break;
  97. case 2:
  98. /* N.B. -sin(pi*(y-1.0)) is *not* equivalent: it would give
  99. -0.0 instead of 0.0 when y == 1.0. */
  100. r = sin(pi*(1.0-y));
  101. break;
  102. case 3:
  103. r = -cos(pi*(y-1.5));
  104. break;
  105. case 4:
  106. r = sin(pi*(y-2.0));
  107. break;
  108. default:
  109. Py_UNREACHABLE();
  110. }
  111. return copysign(1.0, x)*r;
  112. }
  113. /* Implementation of the real gamma function. In extensive but non-exhaustive
  114. random tests, this function proved accurate to within <= 10 ulps across the
  115. entire float domain. Note that accuracy may depend on the quality of the
  116. system math functions, the pow function in particular. Special cases
  117. follow C99 annex F. The parameters and method are tailored to platforms
  118. whose double format is the IEEE 754 binary64 format.
  119. Method: for x > 0.0 we use the Lanczos approximation with parameters N=13
  120. and g=6.024680040776729583740234375; these parameters are amongst those
  121. used by the Boost library. Following Boost (again), we re-express the
  122. Lanczos sum as a rational function, and compute it that way. The
  123. coefficients below were computed independently using MPFR, and have been
  124. double-checked against the coefficients in the Boost source code.
  125. For x < 0.0 we use the reflection formula.
  126. There's one minor tweak that deserves explanation: Lanczos' formula for
  127. Gamma(x) involves computing pow(x+g-0.5, x-0.5) / exp(x+g-0.5). For many x
  128. values, x+g-0.5 can be represented exactly. However, in cases where it
  129. can't be represented exactly the small error in x+g-0.5 can be magnified
  130. significantly by the pow and exp calls, especially for large x. A cheap
  131. correction is to multiply by (1 + e*g/(x+g-0.5)), where e is the error
  132. involved in the computation of x+g-0.5 (that is, e = computed value of
  133. x+g-0.5 - exact value of x+g-0.5). Here's the proof:
  134. Correction factor
  135. -----------------
  136. Write x+g-0.5 = y-e, where y is exactly representable as an IEEE 754
  137. double, and e is tiny. Then:
  138. pow(x+g-0.5,x-0.5)/exp(x+g-0.5) = pow(y-e, x-0.5)/exp(y-e)
  139. = pow(y, x-0.5)/exp(y) * C,
  140. where the correction_factor C is given by
  141. C = pow(1-e/y, x-0.5) * exp(e)
  142. Since e is tiny, pow(1-e/y, x-0.5) ~ 1-(x-0.5)*e/y, and exp(x) ~ 1+e, so:
  143. C ~ (1-(x-0.5)*e/y) * (1+e) ~ 1 + e*(y-(x-0.5))/y
  144. But y-(x-0.5) = g+e, and g+e ~ g. So we get C ~ 1 + e*g/y, and
  145. pow(x+g-0.5,x-0.5)/exp(x+g-0.5) ~ pow(y, x-0.5)/exp(y) * (1 + e*g/y),
  146. Note that for accuracy, when computing r*C it's better to do
  147. r + e*g/y*r;
  148. than
  149. r * (1 + e*g/y);
  150. since the addition in the latter throws away most of the bits of
  151. information in e*g/y.
  152. */
  153. #define LANCZOS_N 13
  154. static const double lanczos_g = 6.024680040776729583740234375;
  155. static const double lanczos_g_minus_half = 5.524680040776729583740234375;
  156. static const double lanczos_num_coeffs[LANCZOS_N] = {
  157. 23531376880.410759688572007674451636754734846804940,
  158. 42919803642.649098768957899047001988850926355848959,
  159. 35711959237.355668049440185451547166705960488635843,
  160. 17921034426.037209699919755754458931112671403265390,
  161. 6039542586.3520280050642916443072979210699388420708,
  162. 1439720407.3117216736632230727949123939715485786772,
  163. 248874557.86205415651146038641322942321632125127801,
  164. 31426415.585400194380614231628318205362874684987640,
  165. 2876370.6289353724412254090516208496135991145378768,
  166. 186056.26539522349504029498971604569928220784236328,
  167. 8071.6720023658162106380029022722506138218516325024,
  168. 210.82427775157934587250973392071336271166969580291,
  169. 2.5066282746310002701649081771338373386264310793408
  170. };
  171. /* denominator is x*(x+1)*...*(x+LANCZOS_N-2) */
  172. static const double lanczos_den_coeffs[LANCZOS_N] = {
  173. 0.0, 39916800.0, 120543840.0, 150917976.0, 105258076.0, 45995730.0,
  174. 13339535.0, 2637558.0, 357423.0, 32670.0, 1925.0, 66.0, 1.0};
  175. /* gamma values for small positive integers, 1 though NGAMMA_INTEGRAL */
  176. #define NGAMMA_INTEGRAL 23
  177. static const double gamma_integral[NGAMMA_INTEGRAL] = {
  178. 1.0, 1.0, 2.0, 6.0, 24.0, 120.0, 720.0, 5040.0, 40320.0, 362880.0,
  179. 3628800.0, 39916800.0, 479001600.0, 6227020800.0, 87178291200.0,
  180. 1307674368000.0, 20922789888000.0, 355687428096000.0,
  181. 6402373705728000.0, 121645100408832000.0, 2432902008176640000.0,
  182. 51090942171709440000.0, 1124000727777607680000.0,
  183. };
  184. /* Lanczos' sum L_g(x), for positive x */
  185. static double
  186. lanczos_sum(double x)
  187. {
  188. double num = 0.0, den = 0.0;
  189. int i;
  190. assert(x > 0.0);
  191. /* evaluate the rational function lanczos_sum(x). For large
  192. x, the obvious algorithm risks overflow, so we instead
  193. rescale the denominator and numerator of the rational
  194. function by x**(1-LANCZOS_N) and treat this as a
  195. rational function in 1/x. This also reduces the error for
  196. larger x values. The choice of cutoff point (5.0 below) is
  197. somewhat arbitrary; in tests, smaller cutoff values than
  198. this resulted in lower accuracy. */
  199. if (x < 5.0) {
  200. for (i = LANCZOS_N; --i >= 0; ) {
  201. num = num * x + lanczos_num_coeffs[i];
  202. den = den * x + lanczos_den_coeffs[i];
  203. }
  204. }
  205. else {
  206. for (i = 0; i < LANCZOS_N; i++) {
  207. num = num / x + lanczos_num_coeffs[i];
  208. den = den / x + lanczos_den_coeffs[i];
  209. }
  210. }
  211. return num/den;
  212. }
  213. /* Constant for +infinity, generated in the same way as float('inf'). */
  214. static double
  215. m_inf(void)
  216. {
  217. #ifndef PY_NO_SHORT_FLOAT_REPR
  218. return _Py_dg_infinity(0);
  219. #else
  220. return Py_HUGE_VAL;
  221. #endif
  222. }
  223. /* Constant nan value, generated in the same way as float('nan'). */
  224. /* We don't currently assume that Py_NAN is defined everywhere. */
  225. #if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
  226. static double
  227. m_nan(void)
  228. {
  229. #ifndef PY_NO_SHORT_FLOAT_REPR
  230. return _Py_dg_stdnan(0);
  231. #else
  232. return Py_NAN;
  233. #endif
  234. }
  235. #endif
  236. static double
  237. m_tgamma(double x)
  238. {
  239. double absx, r, y, z, sqrtpow;
  240. /* special cases */
  241. if (!Py_IS_FINITE(x)) {
  242. if (Py_IS_NAN(x) || x > 0.0)
  243. return x; /* tgamma(nan) = nan, tgamma(inf) = inf */
  244. else {
  245. errno = EDOM;
  246. return Py_NAN; /* tgamma(-inf) = nan, invalid */
  247. }
  248. }
  249. if (x == 0.0) {
  250. errno = EDOM;
  251. /* tgamma(+-0.0) = +-inf, divide-by-zero */
  252. return copysign(Py_HUGE_VAL, x);
  253. }
  254. /* integer arguments */
  255. if (x == floor(x)) {
  256. if (x < 0.0) {
  257. errno = EDOM; /* tgamma(n) = nan, invalid for */
  258. return Py_NAN; /* negative integers n */
  259. }
  260. if (x <= NGAMMA_INTEGRAL)
  261. return gamma_integral[(int)x - 1];
  262. }
  263. absx = fabs(x);
  264. /* tiny arguments: tgamma(x) ~ 1/x for x near 0 */
  265. if (absx < 1e-20) {
  266. r = 1.0/x;
  267. if (Py_IS_INFINITY(r))
  268. errno = ERANGE;
  269. return r;
  270. }
  271. /* large arguments: assuming IEEE 754 doubles, tgamma(x) overflows for
  272. x > 200, and underflows to +-0.0 for x < -200, not a negative
  273. integer. */
  274. if (absx > 200.0) {
  275. if (x < 0.0) {
  276. return 0.0/m_sinpi(x);
  277. }
  278. else {
  279. errno = ERANGE;
  280. return Py_HUGE_VAL;
  281. }
  282. }
  283. y = absx + lanczos_g_minus_half;
  284. /* compute error in sum */
  285. if (absx > lanczos_g_minus_half) {
  286. /* note: the correction can be foiled by an optimizing
  287. compiler that (incorrectly) thinks that an expression like
  288. a + b - a - b can be optimized to 0.0. This shouldn't
  289. happen in a standards-conforming compiler. */
  290. double q = y - absx;
  291. z = q - lanczos_g_minus_half;
  292. }
  293. else {
  294. double q = y - lanczos_g_minus_half;
  295. z = q - absx;
  296. }
  297. z = z * lanczos_g / y;
  298. if (x < 0.0) {
  299. r = -pi / m_sinpi(absx) / absx * exp(y) / lanczos_sum(absx);
  300. r -= z * r;
  301. if (absx < 140.0) {
  302. r /= pow(y, absx - 0.5);
  303. }
  304. else {
  305. sqrtpow = pow(y, absx / 2.0 - 0.25);
  306. r /= sqrtpow;
  307. r /= sqrtpow;
  308. }
  309. }
  310. else {
  311. r = lanczos_sum(absx) / exp(y);
  312. r += z * r;
  313. if (absx < 140.0) {
  314. r *= pow(y, absx - 0.5);
  315. }
  316. else {
  317. sqrtpow = pow(y, absx / 2.0 - 0.25);
  318. r *= sqrtpow;
  319. r *= sqrtpow;
  320. }
  321. }
  322. if (Py_IS_INFINITY(r))
  323. errno = ERANGE;
  324. return r;
  325. }
  326. /*
  327. lgamma: natural log of the absolute value of the Gamma function.
  328. For large arguments, Lanczos' formula works extremely well here.
  329. */
  330. static double
  331. m_lgamma(double x)
  332. {
  333. double r;
  334. double absx;
  335. /* special cases */
  336. if (!Py_IS_FINITE(x)) {
  337. if (Py_IS_NAN(x))
  338. return x; /* lgamma(nan) = nan */
  339. else
  340. return Py_HUGE_VAL; /* lgamma(+-inf) = +inf */
  341. }
  342. /* integer arguments */
  343. if (x == floor(x) && x <= 2.0) {
  344. if (x <= 0.0) {
  345. errno = EDOM; /* lgamma(n) = inf, divide-by-zero for */
  346. return Py_HUGE_VAL; /* integers n <= 0 */
  347. }
  348. else {
  349. return 0.0; /* lgamma(1) = lgamma(2) = 0.0 */
  350. }
  351. }
  352. absx = fabs(x);
  353. /* tiny arguments: lgamma(x) ~ -log(fabs(x)) for small x */
  354. if (absx < 1e-20)
  355. return -log(absx);
  356. /* Lanczos' formula. We could save a fraction of a ulp in accuracy by
  357. having a second set of numerator coefficients for lanczos_sum that
  358. absorbed the exp(-lanczos_g) term, and throwing out the lanczos_g
  359. subtraction below; it's probably not worth it. */
  360. r = log(lanczos_sum(absx)) - lanczos_g;
  361. r += (absx - 0.5) * (log(absx + lanczos_g - 0.5) - 1);
  362. if (x < 0.0)
  363. /* Use reflection formula to get value for negative x. */
  364. r = logpi - log(fabs(m_sinpi(absx))) - log(absx) - r;
  365. if (Py_IS_INFINITY(r))
  366. errno = ERANGE;
  367. return r;
  368. }
  369. #if !defined(HAVE_ERF) || !defined(HAVE_ERFC)
  370. /*
  371. Implementations of the error function erf(x) and the complementary error
  372. function erfc(x).
  373. Method: we use a series approximation for erf for small x, and a continued
  374. fraction approximation for erfc(x) for larger x;
  375. combined with the relations erf(-x) = -erf(x) and erfc(x) = 1.0 - erf(x),
  376. this gives us erf(x) and erfc(x) for all x.
  377. The series expansion used is:
  378. erf(x) = x*exp(-x*x)/sqrt(pi) * [
  379. 2/1 + 4/3 x**2 + 8/15 x**4 + 16/105 x**6 + ...]
  380. The coefficient of x**(2k-2) here is 4**k*factorial(k)/factorial(2*k).
  381. This series converges well for smallish x, but slowly for larger x.
  382. The continued fraction expansion used is:
  383. erfc(x) = x*exp(-x*x)/sqrt(pi) * [1/(0.5 + x**2 -) 0.5/(2.5 + x**2 - )
  384. 3.0/(4.5 + x**2 - ) 7.5/(6.5 + x**2 - ) ...]
  385. after the first term, the general term has the form:
  386. k*(k-0.5)/(2*k+0.5 + x**2 - ...).
  387. This expansion converges fast for larger x, but convergence becomes
  388. infinitely slow as x approaches 0.0. The (somewhat naive) continued
  389. fraction evaluation algorithm used below also risks overflow for large x;
  390. but for large x, erfc(x) == 0.0 to within machine precision. (For
  391. example, erfc(30.0) is approximately 2.56e-393).
  392. Parameters: use series expansion for abs(x) < ERF_SERIES_CUTOFF and
  393. continued fraction expansion for ERF_SERIES_CUTOFF <= abs(x) <
  394. ERFC_CONTFRAC_CUTOFF. ERFC_SERIES_TERMS and ERFC_CONTFRAC_TERMS are the
  395. numbers of terms to use for the relevant expansions. */
  396. #define ERF_SERIES_CUTOFF 1.5
  397. #define ERF_SERIES_TERMS 25
  398. #define ERFC_CONTFRAC_CUTOFF 30.0
  399. #define ERFC_CONTFRAC_TERMS 50
  400. /*
  401. Error function, via power series.
  402. Given a finite float x, return an approximation to erf(x).
  403. Converges reasonably fast for small x.
  404. */
  405. static double
  406. m_erf_series(double x)
  407. {
  408. double x2, acc, fk, result;
  409. int i, saved_errno;
  410. x2 = x * x;
  411. acc = 0.0;
  412. fk = (double)ERF_SERIES_TERMS + 0.5;
  413. for (i = 0; i < ERF_SERIES_TERMS; i++) {
  414. acc = 2.0 + x2 * acc / fk;
  415. fk -= 1.0;
  416. }
  417. /* Make sure the exp call doesn't affect errno;
  418. see m_erfc_contfrac for more. */
  419. saved_errno = errno;
  420. result = acc * x * exp(-x2) / sqrtpi;
  421. errno = saved_errno;
  422. return result;
  423. }
  424. /*
  425. Complementary error function, via continued fraction expansion.
  426. Given a positive float x, return an approximation to erfc(x). Converges
  427. reasonably fast for x large (say, x > 2.0), and should be safe from
  428. overflow if x and nterms are not too large. On an IEEE 754 machine, with x
  429. <= 30.0, we're safe up to nterms = 100. For x >= 30.0, erfc(x) is smaller
  430. than the smallest representable nonzero float. */
  431. static double
  432. m_erfc_contfrac(double x)
  433. {
  434. double x2, a, da, p, p_last, q, q_last, b, result;
  435. int i, saved_errno;
  436. if (x >= ERFC_CONTFRAC_CUTOFF)
  437. return 0.0;
  438. x2 = x*x;
  439. a = 0.0;
  440. da = 0.5;
  441. p = 1.0; p_last = 0.0;
  442. q = da + x2; q_last = 1.0;
  443. for (i = 0; i < ERFC_CONTFRAC_TERMS; i++) {
  444. double temp;
  445. a += da;
  446. da += 2.0;
  447. b = da + x2;
  448. temp = p; p = b*p - a*p_last; p_last = temp;
  449. temp = q; q = b*q - a*q_last; q_last = temp;
  450. }
  451. /* Issue #8986: On some platforms, exp sets errno on underflow to zero;
  452. save the current errno value so that we can restore it later. */
  453. saved_errno = errno;
  454. result = p / q * x * exp(-x2) / sqrtpi;
  455. errno = saved_errno;
  456. return result;
  457. }
  458. #endif /* !defined(HAVE_ERF) || !defined(HAVE_ERFC) */
  459. /* Error function erf(x), for general x */
  460. static double
  461. m_erf(double x)
  462. {
  463. #ifdef HAVE_ERF
  464. return erf(x);
  465. #else
  466. double absx, cf;
  467. if (Py_IS_NAN(x))
  468. return x;
  469. absx = fabs(x);
  470. if (absx < ERF_SERIES_CUTOFF)
  471. return m_erf_series(x);
  472. else {
  473. cf = m_erfc_contfrac(absx);
  474. return x > 0.0 ? 1.0 - cf : cf - 1.0;
  475. }
  476. #endif
  477. }
  478. /* Complementary error function erfc(x), for general x. */
  479. static double
  480. m_erfc(double x)
  481. {
  482. #ifdef HAVE_ERFC
  483. return erfc(x);
  484. #else
  485. double absx, cf;
  486. if (Py_IS_NAN(x))
  487. return x;
  488. absx = fabs(x);
  489. if (absx < ERF_SERIES_CUTOFF)
  490. return 1.0 - m_erf_series(x);
  491. else {
  492. cf = m_erfc_contfrac(absx);
  493. return x > 0.0 ? cf : 2.0 - cf;
  494. }
  495. #endif
  496. }
  497. /*
  498. wrapper for atan2 that deals directly with special cases before
  499. delegating to the platform libm for the remaining cases. This
  500. is necessary to get consistent behaviour across platforms.
  501. Windows, FreeBSD and alpha Tru64 are amongst platforms that don't
  502. always follow C99.
  503. */
  504. static double
  505. m_atan2(double y, double x)
  506. {
  507. if (Py_IS_NAN(x) || Py_IS_NAN(y))
  508. return Py_NAN;
  509. if (Py_IS_INFINITY(y)) {
  510. if (Py_IS_INFINITY(x)) {
  511. if (copysign(1., x) == 1.)
  512. /* atan2(+-inf, +inf) == +-pi/4 */
  513. return copysign(0.25*Py_MATH_PI, y);
  514. else
  515. /* atan2(+-inf, -inf) == +-pi*3/4 */
  516. return copysign(0.75*Py_MATH_PI, y);
  517. }
  518. /* atan2(+-inf, x) == +-pi/2 for finite x */
  519. return copysign(0.5*Py_MATH_PI, y);
  520. }
  521. if (Py_IS_INFINITY(x) || y == 0.) {
  522. if (copysign(1., x) == 1.)
  523. /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
  524. return copysign(0., y);
  525. else
  526. /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
  527. return copysign(Py_MATH_PI, y);
  528. }
  529. return atan2(y, x);
  530. }
  531. /* IEEE 754-style remainder operation: x - n*y where n*y is the nearest
  532. multiple of y to x, taking n even in the case of a tie. Assuming an IEEE 754
  533. binary floating-point format, the result is always exact. */
  534. static double
  535. m_remainder(double x, double y)
  536. {
  537. /* Deal with most common case first. */
  538. if (Py_IS_FINITE(x) && Py_IS_FINITE(y)) {
  539. double absx, absy, c, m, r;
  540. if (y == 0.0) {
  541. return Py_NAN;
  542. }
  543. absx = fabs(x);
  544. absy = fabs(y);
  545. m = fmod(absx, absy);
  546. /*
  547. Warning: some subtlety here. What we *want* to know at this point is
  548. whether the remainder m is less than, equal to, or greater than half
  549. of absy. However, we can't do that comparison directly because we
  550. can't be sure that 0.5*absy is representable (the multiplication
  551. might incur precision loss due to underflow). So instead we compare
  552. m with the complement c = absy - m: m < 0.5*absy if and only if m <
  553. c, and so on. The catch is that absy - m might also not be
  554. representable, but it turns out that it doesn't matter:
  555. - if m > 0.5*absy then absy - m is exactly representable, by
  556. Sterbenz's lemma, so m > c
  557. - if m == 0.5*absy then again absy - m is exactly representable
  558. and m == c
  559. - if m < 0.5*absy then either (i) 0.5*absy is exactly representable,
  560. in which case 0.5*absy < absy - m, so 0.5*absy <= c and hence m <
  561. c, or (ii) absy is tiny, either subnormal or in the lowest normal
  562. binade. Then absy - m is exactly representable and again m < c.
  563. */
  564. c = absy - m;
  565. if (m < c) {
  566. r = m;
  567. }
  568. else if (m > c) {
  569. r = -c;
  570. }
  571. else {
  572. /*
  573. Here absx is exactly halfway between two multiples of absy,
  574. and we need to choose the even multiple. x now has the form
  575. absx = n * absy + m
  576. for some integer n (recalling that m = 0.5*absy at this point).
  577. If n is even we want to return m; if n is odd, we need to
  578. return -m.
  579. So
  580. 0.5 * (absx - m) = (n/2) * absy
  581. and now reducing modulo absy gives us:
  582. | m, if n is odd
  583. fmod(0.5 * (absx - m), absy) = |
  584. | 0, if n is even
  585. Now m - 2.0 * fmod(...) gives the desired result: m
  586. if n is even, -m if m is odd.
  587. Note that all steps in fmod(0.5 * (absx - m), absy)
  588. will be computed exactly, with no rounding error
  589. introduced.
  590. */
  591. assert(m == c);
  592. r = m - 2.0 * fmod(0.5 * (absx - m), absy);
  593. }
  594. return copysign(1.0, x) * r;
  595. }
  596. /* Special values. */
  597. if (Py_IS_NAN(x)) {
  598. return x;
  599. }
  600. if (Py_IS_NAN(y)) {
  601. return y;
  602. }
  603. if (Py_IS_INFINITY(x)) {
  604. return Py_NAN;
  605. }
  606. assert(Py_IS_INFINITY(y));
  607. return x;
  608. }
  609. /*
  610. Various platforms (Solaris, OpenBSD) do nonstandard things for log(0),
  611. log(-ve), log(NaN). Here are wrappers for log and log10 that deal with
  612. special values directly, passing positive non-special values through to
  613. the system log/log10.
  614. */
  615. static double
  616. m_log(double x)
  617. {
  618. if (Py_IS_FINITE(x)) {
  619. if (x > 0.0)
  620. return log(x);
  621. errno = EDOM;
  622. if (x == 0.0)
  623. return -Py_HUGE_VAL; /* log(0) = -inf */
  624. else
  625. return Py_NAN; /* log(-ve) = nan */
  626. }
  627. else if (Py_IS_NAN(x))
  628. return x; /* log(nan) = nan */
  629. else if (x > 0.0)
  630. return x; /* log(inf) = inf */
  631. else {
  632. errno = EDOM;
  633. return Py_NAN; /* log(-inf) = nan */
  634. }
  635. }
  636. /*
  637. log2: log to base 2.
  638. Uses an algorithm that should:
  639. (a) produce exact results for powers of 2, and
  640. (b) give a monotonic log2 (for positive finite floats),
  641. assuming that the system log is monotonic.
  642. */
  643. static double
  644. m_log2(double x)
  645. {
  646. if (!Py_IS_FINITE(x)) {
  647. if (Py_IS_NAN(x))
  648. return x; /* log2(nan) = nan */
  649. else if (x > 0.0)
  650. return x; /* log2(+inf) = +inf */
  651. else {
  652. errno = EDOM;
  653. return Py_NAN; /* log2(-inf) = nan, invalid-operation */
  654. }
  655. }
  656. if (x > 0.0) {
  657. #ifdef HAVE_LOG2
  658. return log2(x);
  659. #else
  660. double m;
  661. int e;
  662. m = frexp(x, &e);
  663. /* We want log2(m * 2**e) == log(m) / log(2) + e. Care is needed when
  664. * x is just greater than 1.0: in that case e is 1, log(m) is negative,
  665. * and we get significant cancellation error from the addition of
  666. * log(m) / log(2) to e. The slight rewrite of the expression below
  667. * avoids this problem.
  668. */
  669. if (x >= 1.0) {
  670. return log(2.0 * m) / log(2.0) + (e - 1);
  671. }
  672. else {
  673. return log(m) / log(2.0) + e;
  674. }
  675. #endif
  676. }
  677. else if (x == 0.0) {
  678. errno = EDOM;
  679. return -Py_HUGE_VAL; /* log2(0) = -inf, divide-by-zero */
  680. }
  681. else {
  682. errno = EDOM;
  683. return Py_NAN; /* log2(-inf) = nan, invalid-operation */
  684. }
  685. }
  686. static double
  687. m_log10(double x)
  688. {
  689. if (Py_IS_FINITE(x)) {
  690. if (x > 0.0)
  691. return log10(x);
  692. errno = EDOM;
  693. if (x == 0.0)
  694. return -Py_HUGE_VAL; /* log10(0) = -inf */
  695. else
  696. return Py_NAN; /* log10(-ve) = nan */
  697. }
  698. else if (Py_IS_NAN(x))
  699. return x; /* log10(nan) = nan */
  700. else if (x > 0.0)
  701. return x; /* log10(inf) = inf */
  702. else {
  703. errno = EDOM;
  704. return Py_NAN; /* log10(-inf) = nan */
  705. }
  706. }
  707. /*[clinic input]
  708. math.gcd
  709. x as a: object
  710. y as b: object
  711. /
  712. greatest common divisor of x and y
  713. [clinic start generated code]*/
  714. static PyObject *
  715. math_gcd_impl(PyObject *module, PyObject *a, PyObject *b)
  716. /*[clinic end generated code: output=7b2e0c151bd7a5d8 input=c2691e57fb2a98fa]*/
  717. {
  718. PyObject *g;
  719. a = PyNumber_Index(a);
  720. if (a == NULL)
  721. return NULL;
  722. b = PyNumber_Index(b);
  723. if (b == NULL) {
  724. Py_DECREF(a);
  725. return NULL;
  726. }
  727. g = _PyLong_GCD(a, b);
  728. Py_DECREF(a);
  729. Py_DECREF(b);
  730. return g;
  731. }
  732. /* Call is_error when errno != 0, and where x is the result libm
  733. * returned. is_error will usually set up an exception and return
  734. * true (1), but may return false (0) without setting up an exception.
  735. */
  736. static int
  737. is_error(double x)
  738. {
  739. int result = 1; /* presumption of guilt */
  740. assert(errno); /* non-zero errno is a precondition for calling */
  741. if (errno == EDOM)
  742. PyErr_SetString(PyExc_ValueError, "math domain error");
  743. else if (errno == ERANGE) {
  744. /* ANSI C generally requires libm functions to set ERANGE
  745. * on overflow, but also generally *allows* them to set
  746. * ERANGE on underflow too. There's no consistency about
  747. * the latter across platforms.
  748. * Alas, C99 never requires that errno be set.
  749. * Here we suppress the underflow errors (libm functions
  750. * should return a zero on underflow, and +- HUGE_VAL on
  751. * overflow, so testing the result for zero suffices to
  752. * distinguish the cases).
  753. *
  754. * On some platforms (Ubuntu/ia64) it seems that errno can be
  755. * set to ERANGE for subnormal results that do *not* underflow
  756. * to zero. So to be safe, we'll ignore ERANGE whenever the
  757. * function result is less than one in absolute value.
  758. */
  759. if (fabs(x) < 1.0)
  760. result = 0;
  761. else
  762. PyErr_SetString(PyExc_OverflowError,
  763. "math range error");
  764. }
  765. else
  766. /* Unexpected math error */
  767. PyErr_SetFromErrno(PyExc_ValueError);
  768. return result;
  769. }
  770. /*
  771. math_1 is used to wrap a libm function f that takes a double
  772. argument and returns a double.
  773. The error reporting follows these rules, which are designed to do
  774. the right thing on C89/C99 platforms and IEEE 754/non IEEE 754
  775. platforms.
  776. - a NaN result from non-NaN inputs causes ValueError to be raised
  777. - an infinite result from finite inputs causes OverflowError to be
  778. raised if can_overflow is 1, or raises ValueError if can_overflow
  779. is 0.
  780. - if the result is finite and errno == EDOM then ValueError is
  781. raised
  782. - if the result is finite and nonzero and errno == ERANGE then
  783. OverflowError is raised
  784. The last rule is used to catch overflow on platforms which follow
  785. C89 but for which HUGE_VAL is not an infinity.
  786. For the majority of one-argument functions these rules are enough
  787. to ensure that Python's functions behave as specified in 'Annex F'
  788. of the C99 standard, with the 'invalid' and 'divide-by-zero'
  789. floating-point exceptions mapping to Python's ValueError and the
  790. 'overflow' floating-point exception mapping to OverflowError.
  791. math_1 only works for functions that don't have singularities *and*
  792. the possibility of overflow; fortunately, that covers everything we
  793. care about right now.
  794. */
  795. static PyObject *
  796. math_1_to_whatever(PyObject *arg, double (*func) (double),
  797. PyObject *(*from_double_func) (double),
  798. int can_overflow)
  799. {
  800. double x, r;
  801. x = PyFloat_AsDouble(arg);
  802. if (x == -1.0 && PyErr_Occurred())
  803. return NULL;
  804. errno = 0;
  805. PyFPE_START_PROTECT("in math_1", return 0);
  806. r = (*func)(x);
  807. PyFPE_END_PROTECT(r);
  808. if (Py_IS_NAN(r) && !Py_IS_NAN(x)) {
  809. PyErr_SetString(PyExc_ValueError,
  810. "math domain error"); /* invalid arg */
  811. return NULL;
  812. }
  813. if (Py_IS_INFINITY(r) && Py_IS_FINITE(x)) {
  814. if (can_overflow)
  815. PyErr_SetString(PyExc_OverflowError,
  816. "math range error"); /* overflow */
  817. else
  818. PyErr_SetString(PyExc_ValueError,
  819. "math domain error"); /* singularity */
  820. return NULL;
  821. }
  822. if (Py_IS_FINITE(r) && errno && is_error(r))
  823. /* this branch unnecessary on most platforms */
  824. return NULL;
  825. return (*from_double_func)(r);
  826. }
  827. /* variant of math_1, to be used when the function being wrapped is known to
  828. set errno properly (that is, errno = EDOM for invalid or divide-by-zero,
  829. errno = ERANGE for overflow). */
  830. static PyObject *
  831. math_1a(PyObject *arg, double (*func) (double))
  832. {
  833. double x, r;
  834. x = PyFloat_AsDouble(arg);
  835. if (x == -1.0 && PyErr_Occurred())
  836. return NULL;
  837. errno = 0;
  838. PyFPE_START_PROTECT("in math_1a", return 0);
  839. r = (*func)(x);
  840. PyFPE_END_PROTECT(r);
  841. if (errno && is_error(r))
  842. return NULL;
  843. return PyFloat_FromDouble(r);
  844. }
  845. /*
  846. math_2 is used to wrap a libm function f that takes two double
  847. arguments and returns a double.
  848. The error reporting follows these rules, which are designed to do
  849. the right thing on C89/C99 platforms and IEEE 754/non IEEE 754
  850. platforms.
  851. - a NaN result from non-NaN inputs causes ValueError to be raised
  852. - an infinite result from finite inputs causes OverflowError to be
  853. raised.
  854. - if the result is finite and errno == EDOM then ValueError is
  855. raised
  856. - if the result is finite and nonzero and errno == ERANGE then
  857. OverflowError is raised
  858. The last rule is used to catch overflow on platforms which follow
  859. C89 but for which HUGE_VAL is not an infinity.
  860. For most two-argument functions (copysign, fmod, hypot, atan2)
  861. these rules are enough to ensure that Python's functions behave as
  862. specified in 'Annex F' of the C99 standard, with the 'invalid' and
  863. 'divide-by-zero' floating-point exceptions mapping to Python's
  864. ValueError and the 'overflow' floating-point exception mapping to
  865. OverflowError.
  866. */
  867. static PyObject *
  868. math_1(PyObject *arg, double (*func) (double), int can_overflow)
  869. {
  870. return math_1_to_whatever(arg, func, PyFloat_FromDouble, can_overflow);
  871. }
  872. static PyObject *
  873. math_1_to_int(PyObject *arg, double (*func) (double), int can_overflow)
  874. {
  875. return math_1_to_whatever(arg, func, PyLong_FromDouble, can_overflow);
  876. }
  877. static PyObject *
  878. math_2(PyObject *const *args, Py_ssize_t nargs,
  879. double (*func) (double, double), const char *funcname)
  880. {
  881. double x, y, r;
  882. if (!_PyArg_CheckPositional(funcname, nargs, 2, 2))
  883. return NULL;
  884. x = PyFloat_AsDouble(args[0]);
  885. y = PyFloat_AsDouble(args[1]);
  886. if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
  887. return NULL;
  888. errno = 0;
  889. PyFPE_START_PROTECT("in math_2", return 0);
  890. r = (*func)(x, y);
  891. PyFPE_END_PROTECT(r);
  892. if (Py_IS_NAN(r)) {
  893. if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
  894. errno = EDOM;
  895. else
  896. errno = 0;
  897. }
  898. else if (Py_IS_INFINITY(r)) {
  899. if (Py_IS_FINITE(x) && Py_IS_FINITE(y))
  900. errno = ERANGE;
  901. else
  902. errno = 0;
  903. }
  904. if (errno && is_error(r))
  905. return NULL;
  906. else
  907. return PyFloat_FromDouble(r);
  908. }
  909. #define FUNC1(funcname, func, can_overflow, docstring) \
  910. static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
  911. return math_1(args, func, can_overflow); \
  912. }\
  913. PyDoc_STRVAR(math_##funcname##_doc, docstring);
  914. #define FUNC1A(funcname, func, docstring) \
  915. static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
  916. return math_1a(args, func); \
  917. }\
  918. PyDoc_STRVAR(math_##funcname##_doc, docstring);
  919. #define FUNC2(funcname, func, docstring) \
  920. static PyObject * math_##funcname(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { \
  921. return math_2(args, nargs, func, #funcname); \
  922. }\
  923. PyDoc_STRVAR(math_##funcname##_doc, docstring);
  924. FUNC1(acos, acos, 0,
  925. "acos($module, x, /)\n--\n\n"
  926. "Return the arc cosine (measured in radians) of x.\n\n"
  927. "The result is between 0 and pi.")
  928. FUNC1(acosh, m_acosh, 0,
  929. "acosh($module, x, /)\n--\n\n"
  930. "Return the inverse hyperbolic cosine of x.")
  931. FUNC1(asin, asin, 0,
  932. "asin($module, x, /)\n--\n\n"
  933. "Return the arc sine (measured in radians) of x.\n\n"
  934. "The result is between -pi/2 and pi/2.")
  935. FUNC1(asinh, m_asinh, 0,
  936. "asinh($module, x, /)\n--\n\n"
  937. "Return the inverse hyperbolic sine of x.")
  938. FUNC1(atan, atan, 0,
  939. "atan($module, x, /)\n--\n\n"
  940. "Return the arc tangent (measured in radians) of x.\n\n"
  941. "The result is between -pi/2 and pi/2.")
  942. FUNC2(atan2, m_atan2,
  943. "atan2($module, y, x, /)\n--\n\n"
  944. "Return the arc tangent (measured in radians) of y/x.\n\n"
  945. "Unlike atan(y/x), the signs of both x and y are considered.")
  946. FUNC1(atanh, m_atanh, 0,
  947. "atanh($module, x, /)\n--\n\n"
  948. "Return the inverse hyperbolic tangent of x.")
  949. /*[clinic input]
  950. math.ceil
  951. x as number: object
  952. /
  953. Return the ceiling of x as an Integral.
  954. This is the smallest integer >= x.
  955. [clinic start generated code]*/
  956. static PyObject *
  957. math_ceil(PyObject *module, PyObject *number)
  958. /*[clinic end generated code: output=6c3b8a78bc201c67 input=2725352806399cab]*/
  959. {
  960. _Py_IDENTIFIER(__ceil__);
  961. PyObject *method, *result;
  962. method = _PyObject_LookupSpecial(number, &PyId___ceil__);
  963. if (method == NULL) {
  964. if (PyErr_Occurred())
  965. return NULL;
  966. return math_1_to_int(number, ceil, 0);
  967. }
  968. result = _PyObject_CallNoArg(method);
  969. Py_DECREF(method);
  970. return result;
  971. }
  972. FUNC2(copysign, copysign,
  973. "copysign($module, x, y, /)\n--\n\n"
  974. "Return a float with the magnitude (absolute value) of x but the sign of y.\n\n"
  975. "On platforms that support signed zeros, copysign(1.0, -0.0)\n"
  976. "returns -1.0.\n")
  977. FUNC1(cos, cos, 0,
  978. "cos($module, x, /)\n--\n\n"
  979. "Return the cosine of x (measured in radians).")
  980. FUNC1(cosh, cosh, 1,
  981. "cosh($module, x, /)\n--\n\n"
  982. "Return the hyperbolic cosine of x.")
  983. FUNC1A(erf, m_erf,
  984. "erf($module, x, /)\n--\n\n"
  985. "Error function at x.")
  986. FUNC1A(erfc, m_erfc,
  987. "erfc($module, x, /)\n--\n\n"
  988. "Complementary error function at x.")
  989. FUNC1(exp, exp, 1,
  990. "exp($module, x, /)\n--\n\n"
  991. "Return e raised to the power of x.")
  992. FUNC1(expm1, m_expm1, 1,
  993. "expm1($module, x, /)\n--\n\n"
  994. "Return exp(x)-1.\n\n"
  995. "This function avoids the loss of precision involved in the direct "
  996. "evaluation of exp(x)-1 for small x.")
  997. FUNC1(fabs, fabs, 0,
  998. "fabs($module, x, /)\n--\n\n"
  999. "Return the absolute value of the float x.")
  1000. /*[clinic input]
  1001. math.floor
  1002. x as number: object
  1003. /
  1004. Return the floor of x as an Integral.
  1005. This is the largest integer <= x.
  1006. [clinic start generated code]*/
  1007. static PyObject *
  1008. math_floor(PyObject *module, PyObject *number)
  1009. /*[clinic end generated code: output=c6a65c4884884b8a input=63af6b5d7ebcc3d6]*/
  1010. {
  1011. _Py_IDENTIFIER(__floor__);
  1012. PyObject *method, *result;
  1013. method = _PyObject_LookupSpecial(number, &PyId___floor__);
  1014. if (method == NULL) {
  1015. if (PyErr_Occurred())
  1016. return NULL;
  1017. return math_1_to_int(number, floor, 0);
  1018. }
  1019. result = _PyObject_CallNoArg(method);
  1020. Py_DECREF(method);
  1021. return result;
  1022. }
  1023. FUNC1A(gamma, m_tgamma,
  1024. "gamma($module, x, /)\n--\n\n"
  1025. "Gamma function at x.")
  1026. FUNC1A(lgamma, m_lgamma,
  1027. "lgamma($module, x, /)\n--\n\n"
  1028. "Natural logarithm of absolute value of Gamma function at x.")
  1029. FUNC1(log1p, m_log1p, 0,
  1030. "log1p($module, x, /)\n--\n\n"
  1031. "Return the natural logarithm of 1+x (base e).\n\n"
  1032. "The result is computed in a way which is accurate for x near zero.")
  1033. FUNC2(remainder, m_remainder,
  1034. "remainder($module, x, y, /)\n--\n\n"
  1035. "Difference between x and the closest integer multiple of y.\n\n"
  1036. "Return x - n*y where n*y is the closest integer multiple of y.\n"
  1037. "In the case where x is exactly halfway between two multiples of\n"
  1038. "y, the nearest even value of n is used. The result is always exact.")
  1039. FUNC1(sin, sin, 0,
  1040. "sin($module, x, /)\n--\n\n"
  1041. "Return the sine of x (measured in radians).")
  1042. FUNC1(sinh, sinh, 1,
  1043. "sinh($module, x, /)\n--\n\n"
  1044. "Return the hyperbolic sine of x.")
  1045. FUNC1(sqrt, sqrt, 0,
  1046. "sqrt($module, x, /)\n--\n\n"
  1047. "Return the square root of x.")
  1048. FUNC1(tan, tan, 0,
  1049. "tan($module, x, /)\n--\n\n"
  1050. "Return the tangent of x (measured in radians).")
  1051. FUNC1(tanh, tanh, 0,
  1052. "tanh($module, x, /)\n--\n\n"
  1053. "Return the hyperbolic tangent of x.")
  1054. /* Precision summation function as msum() by Raymond Hettinger in
  1055. <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090>,
  1056. enhanced with the exact partials sum and roundoff from Mark
  1057. Dickinson's post at <http://bugs.python.org/file10357/msum4.py>.
  1058. See those links for more details, proofs and other references.
  1059. Note 1: IEEE 754R floating point semantics are assumed,
  1060. but the current implementation does not re-establish special
  1061. value semantics across iterations (i.e. handling -Inf + Inf).
  1062. Note 2: No provision is made for intermediate overflow handling;
  1063. therefore, sum([1e+308, 1e-308, 1e+308]) returns 1e+308 while
  1064. sum([1e+308, 1e+308, 1e-308]) raises an OverflowError due to the
  1065. overflow of the first partial sum.
  1066. Note 3: The intermediate values lo, yr, and hi are declared volatile so
  1067. aggressive compilers won't algebraically reduce lo to always be exactly 0.0.
  1068. Also, the volatile declaration forces the values to be stored in memory as
  1069. regular doubles instead of extended long precision (80-bit) values. This
  1070. prevents double rounding because any addition or subtraction of two doubles
  1071. can be resolved exactly into double-sized hi and lo values. As long as the
  1072. hi value gets forced into a double before yr and lo are computed, the extra
  1073. bits in downstream extended precision operations (x87 for example) will be
  1074. exactly zero and therefore can be losslessly stored back into a double,
  1075. thereby preventing double rounding.
  1076. Note 4: A similar implementation is in Modules/cmathmodule.c.
  1077. Be sure to update both when making changes.
  1078. Note 5: The signature of math.fsum() differs from builtins.sum()
  1079. because the start argument doesn't make sense in the context of
  1080. accurate summation. Since the partials table is collapsed before
  1081. returning a result, sum(seq2, start=sum(seq1)) may not equal the
  1082. accurate result returned by sum(itertools.chain(seq1, seq2)).
  1083. */
  1084. #define NUM_PARTIALS 32 /* initial partials array size, on stack */
  1085. /* Extend the partials array p[] by doubling its size. */
  1086. static int /* non-zero on error */
  1087. _fsum_realloc(double **p_ptr, Py_ssize_t n,
  1088. double *ps, Py_ssize_t *m_ptr)
  1089. {
  1090. void *v = NULL;
  1091. Py_ssize_t m = *m_ptr;
  1092. m += m; /* double */
  1093. if (n < m && (size_t)m < ((size_t)PY_SSIZE_T_MAX / sizeof(double))) {
  1094. double *p = *p_ptr;
  1095. if (p == ps) {
  1096. v = PyMem_Malloc(sizeof(double) * m);
  1097. if (v != NULL)
  1098. memcpy(v, ps, sizeof(double) * n);
  1099. }
  1100. else
  1101. v = PyMem_Realloc(p, sizeof(double) * m);
  1102. }
  1103. if (v == NULL) { /* size overflow or no memory */
  1104. PyErr_SetString(PyExc_MemoryError, "math.fsum partials");
  1105. return 1;
  1106. }
  1107. *p_ptr = (double*) v;
  1108. *m_ptr = m;
  1109. return 0;
  1110. }
  1111. /* Full precision summation of a sequence of floats.
  1112. def msum(iterable):
  1113. partials = [] # sorted, non-overlapping partial sums
  1114. for x in iterable:
  1115. i = 0
  1116. for y in partials:
  1117. if abs(x) < abs(y):
  1118. x, y = y, x
  1119. hi = x + y
  1120. lo = y - (hi - x)
  1121. if lo:
  1122. partials[i] = lo
  1123. i += 1
  1124. x = hi
  1125. partials[i:] = [x]
  1126. return sum_exact(partials)
  1127. Rounded x+y stored in hi with the roundoff stored in lo. Together hi+lo
  1128. are exactly equal to x+y. The inner loop applies hi/lo summation to each
  1129. partial so that the list of partial sums remains exact.
  1130. Sum_exact() adds the partial sums exactly and correctly rounds the final
  1131. result (using the round-half-to-even rule). The items in partials remain
  1132. non-zero, non-special, non-overlapping and strictly increasing in
  1133. magnitude, but possibly not all having the same sign.
  1134. Depends on IEEE 754 arithmetic guarantees and half-even rounding.
  1135. */
  1136. /*[clinic input]
  1137. math.fsum
  1138. seq: object
  1139. /
  1140. Return an accurate floating point sum of values in the iterable seq.
  1141. Assumes IEEE-754 floating point arithmetic.
  1142. [clinic start generated code]*/
  1143. static PyObject *
  1144. math_fsum(PyObject *module, PyObject *seq)
  1145. /*[clinic end generated code: output=ba5c672b87fe34fc input=c51b7d8caf6f6e82]*/
  1146. {
  1147. PyObject *item, *iter, *sum = NULL;
  1148. Py_ssize_t i, j, n = 0, m = NUM_PARTIALS;
  1149. double x, y, t, ps[NUM_PARTIALS], *p = ps;
  1150. double xsave, special_sum = 0.0, inf_sum = 0.0;
  1151. volatile double hi, yr, lo;
  1152. iter = PyObject_GetIter(seq);
  1153. if (iter == NULL)
  1154. return NULL;
  1155. PyFPE_START_PROTECT("fsum", Py_DECREF(iter); return NULL)
  1156. for(;;) { /* for x in iterable */
  1157. assert(0 <= n && n <= m);
  1158. assert((m == NUM_PARTIALS && p == ps) ||
  1159. (m > NUM_PARTIALS && p != NULL));
  1160. item = PyIter_Next(iter);
  1161. if (item == NULL) {
  1162. if (PyErr_Occurred())
  1163. goto _fsum_error;
  1164. break;
  1165. }
  1166. ASSIGN_DOUBLE(x, item, error_with_item);
  1167. Py_DECREF(item);
  1168. xsave = x;
  1169. for (i = j = 0; j < n; j++) { /* for y in partials */
  1170. y = p[j];
  1171. if (fabs(x) < fabs(y)) {
  1172. t = x; x = y; y = t;
  1173. }
  1174. hi = x + y;
  1175. yr = hi - x;
  1176. lo = y - yr;
  1177. if (lo != 0.0)
  1178. p[i++] = lo;
  1179. x = hi;
  1180. }
  1181. n = i; /* ps[i:] = [x] */
  1182. if (x != 0.0) {
  1183. if (! Py_IS_FINITE(x)) {
  1184. /* a nonfinite x could arise either as
  1185. a result of intermediate overflow, or
  1186. as a result of a nan or inf in the
  1187. summands */
  1188. if (Py_IS_FINITE(xsave)) {
  1189. PyErr_SetString(PyExc_OverflowError,
  1190. "intermediate overflow in fsum");
  1191. goto _fsum_error;
  1192. }
  1193. if (Py_IS_INFINITY(xsave))
  1194. inf_sum += xsave;
  1195. special_sum += xsave;
  1196. /* reset partials */
  1197. n = 0;
  1198. }
  1199. else if (n >= m && _fsum_realloc(&p, n, ps, &m))
  1200. goto _fsum_error;
  1201. else
  1202. p[n++] = x;
  1203. }
  1204. }
  1205. if (special_sum != 0.0) {
  1206. if (Py_IS_NAN(inf_sum))
  1207. PyErr_SetString(PyExc_ValueError,
  1208. "-inf + inf in fsum");
  1209. else
  1210. sum = PyFloat_FromDouble(special_sum);
  1211. goto _fsum_error;
  1212. }
  1213. hi = 0.0;
  1214. if (n > 0) {
  1215. hi = p[--n];
  1216. /* sum_exact(ps, hi) from the top, stop when the sum becomes
  1217. inexact. */
  1218. while (n > 0) {
  1219. x = hi;
  1220. y = p[--n];
  1221. assert(fabs(y) < fabs(x));
  1222. hi = x + y;
  1223. yr = hi - x;
  1224. lo = y - yr;
  1225. if (lo != 0.0)
  1226. break;
  1227. }
  1228. /* Make half-even rounding work across multiple partials.
  1229. Needed so that sum([1e-16, 1, 1e16]) will round-up the last
  1230. digit to two instead of down to zero (the 1e-16 makes the 1
  1231. slightly closer to two). With a potential 1 ULP rounding
  1232. error fixed-up, math.fsum() can guarantee commutativity. */
  1233. if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) ||
  1234. (lo > 0.0 && p[n-1] > 0.0))) {
  1235. y = lo * 2.0;
  1236. x = hi + y;
  1237. yr = x - hi;
  1238. if (y == yr)
  1239. hi = x;
  1240. }
  1241. }
  1242. sum = PyFloat_FromDouble(hi);
  1243. _fsum_error:
  1244. PyFPE_END_PROTECT(hi)
  1245. Py_DECREF(iter);
  1246. if (p != ps)
  1247. PyMem_Free(p);
  1248. return sum;
  1249. error_with_item:
  1250. Py_DECREF(item);
  1251. goto _fsum_error;
  1252. }
  1253. #undef NUM_PARTIALS
  1254. /* Return the smallest integer k such that n < 2**k, or 0 if n == 0.
  1255. * Equivalent to floor(lg(x))+1. Also equivalent to: bitwidth_of_type -
  1256. * count_leading_zero_bits(x)
  1257. */
  1258. /* XXX: This routine does more or less the same thing as
  1259. * bits_in_digit() in Objects/longobject.c. Someday it would be nice to
  1260. * consolidate them. On BSD, there's a library function called fls()
  1261. * that we could use, and GCC provides __builtin_clz().
  1262. */
  1263. static unsigned long
  1264. bit_length(unsigned long n)
  1265. {
  1266. unsigned long len = 0;
  1267. while (n != 0) {
  1268. ++len;
  1269. n >>= 1;
  1270. }
  1271. return len;
  1272. }
  1273. static unsigned long
  1274. count_set_bits(unsigned long n)
  1275. {
  1276. unsigned long count = 0;
  1277. while (n != 0) {
  1278. ++count;
  1279. n &= n - 1; /* clear least significant bit */
  1280. }
  1281. return count;
  1282. }
  1283. /* Integer square root
  1284. Given a nonnegative integer `n`, we want to compute the largest integer
  1285. `a` for which `a * a <= n`, or equivalently the integer part of the exact
  1286. square root of `n`.
  1287. We use an adaptive-precision pure-integer version of Newton's iteration. Given
  1288. a positive integer `n`, the algorithm produces at each iteration an integer
  1289. approximation `a` to the square root of `n >> s` for some even integer `s`,
  1290. with `s` decreasing as the iterations progress. On the final iteration, `s` is
  1291. zero and we have an approximation to the square root of `n` itself.
  1292. At every step, the approximation `a` is strictly within 1.0 of the true square
  1293. root, so we have
  1294. (a - 1)**2 < (n >> s) < (a + 1)**2
  1295. After the final iteration, a check-and-correct step is needed to determine
  1296. whether `a` or `a - 1` gives the desired integer square root of `n`.
  1297. The algorithm is remarkable in its simplicity. There's no need for a
  1298. per-iteration check-and-correct step, and termination is straightforward: the
  1299. number of iterations is known in advance (it's exactly `floor(log2(log2(n)))`
  1300. for `n > 1`). The only tricky part of the correctness proof is in establishing
  1301. that the bound `(a - 1)**2 < (n >> s) < (a + 1)**2` is maintained from one
  1302. iteration to the next. A sketch of the proof of this is given below.
  1303. In addition to the proof sketch, a formal, computer-verified proof
  1304. of correctness (using Lean) of an equivalent recursive algorithm can be found
  1305. here:
  1306. https://github.com/mdickinson/snippets/blob/master/proofs/isqrt/src/isqrt.lean
  1307. Here's Python code equivalent to the C implementation below:
  1308. def isqrt(n):
  1309. """
  1310. Return the integer part of the square root of the input.
  1311. """
  1312. n = operator.index(n)
  1313. if n < 0:
  1314. raise ValueError("isqrt() argument must be nonnegative")
  1315. if n == 0:
  1316. return 0
  1317. c = (n.bit_length() - 1) // 2
  1318. a = 1
  1319. d = 0
  1320. for s in reversed(range(c.bit_length())):
  1321. # Loop invariant: (a-1)**2 < (n >> 2*(c - d)) < (a+1)**2
  1322. e = d
  1323. d = c >> s
  1324. a = (a << d - e - 1) + (n >> 2*c - e - d + 1) // a
  1325. return a - (a*a > n)
  1326. Sketch of proof of correctness
  1327. ------------------------------
  1328. The delicate part of the correctness proof is showing that the loop invariant
  1329. is preserved from one iteration to the next. That is, just before the line
  1330. a = (a << d - e - 1) + (n >> 2*c - e - d + 1) // a
  1331. is executed in the above code, we know that
  1332. (1) (a - 1)**2 < (n >> 2*(c - e)) < (a + 1)**2.
  1333. (since `e` is always the value of `d` from the previous iteration). We must
  1334. prove that after that line is executed, we have
  1335. (a - 1)**2 < (n >> 2*(c - d)) < (a + 1)**2
  1336. To facilitate the proof, we make some changes of notation. Write `m` for
  1337. `n >> 2*(c-d)`, and write `b` for the new value of `a`, so
  1338. b = (a << d - e - 1) + (n >> 2*c - e - d + 1) // a
  1339. or equivalently:
  1340. (2) b = (a << d - e - 1) + (m >> d - e + 1) // a
  1341. Then we can rewrite (1) as:
  1342. (3) (a - 1)**2 < (m >> 2*(d - e)) < (a + 1)**2
  1343. and we must show that (b - 1)**2 < m < (b + 1)**2.
  1344. From this point on, we switch to mathematical notation, so `/` means exact
  1345. division rather than integer division and `^` is used for exponentiation. We
  1346. use the `` symbol for the exact square root. In (3), we can remove the
  1347. implicit floor operation to give:
  1348. (4) (a - 1)^2 < m / 4^(d - e) < (a + 1)^2
  1349. Taking square roots throughout (4), scaling by `2^(d-e)`, and rearranging gives
  1350. (5) 0 <= | 2^(d-e)a - m | < 2^(d-e)
  1351. Squaring and dividing through by `2^(d-e+1) a` gives
  1352. (6) 0 <= 2^(d-e-1) a + m / (2^(d-e+1) a) - m < 2^(d-e-1) / a
  1353. We'll show below that `2^(d-e-1) <= a`. Given that, we can replace the
  1354. right-hand side of (6) with `1`, and now replacing the central
  1355. term `m / (2^(d-e+1) a)` with its floor in (6) gives
  1356. (7) -1 < 2^(d-e-1) a + m // 2^(d-e+1) a - √m < 1
  1357. Or equivalently, from (2):
  1358. (7) -1 < b - m < 1
  1359. and rearranging gives that `(b-1)^2 < m < (b+1)^2`, which is what we needed
  1360. to prove.
  1361. We're not quite done: we still have to prove the inequality `2^(d - e - 1) <=
  1362. a` that was used to get line (7) above. From the definition of `c`, we have
  1363. `4^c <= n`, which implies
  1364. (8) 4^d <= m
  1365. also, since `e == d >> 1`, `d` is at most `2e + 1`, from which it follows
  1366. that `2d - 2e - 1 <= d` and hence that
  1367. (9) 4^(2d - 2e - 1) <= m
  1368. Dividing both sides by `4^(d - e)` gives
  1369. (10) 4^(d - e - 1) <= m / 4^(d - e)
  1370. But we know from (4) that `m / 4^(d-e) < (a + 1)^2`, hence
  1371. (11) 4^(d - e - 1) < (a + 1)^2
  1372. Now taking square roots of both sides and observing that both `2^(d-e-1)` and
  1373. `a` are integers gives `2^(d - e - 1) <= a`, which is what we needed. This
  1374. completes the proof sketch.
  1375. */
  1376. /* Approximate square root of a large 64-bit integer.
  1377. Given `n` satisfying `2**62 <= n < 2**64`, return `a`
  1378. satisfying `(a - 1)**2 < n < (a + 1)**2`. */
  1379. static uint64_t
  1380. _approximate_isqrt(uint64_t n)
  1381. {
  1382. uint32_t u = 1U + (n >> 62);
  1383. u = (u << 1) + (n >> 59) / u;
  1384. u = (u << 3) + (n >> 53) / u;
  1385. u = (u << 7) + (n >> 41) / u;
  1386. return (u << 15) + (n >> 17) / u;
  1387. }
  1388. /*[clinic input]
  1389. math.isqrt
  1390. n: object
  1391. /
  1392. Return the integer part of the square root of the input.
  1393. [clinic start generated code]*/
  1394. static PyObject *
  1395. math_isqrt(PyObject *module, PyObject *n)
  1396. /*[clinic end generated code: output=35a6f7f980beab26 input=5b6e7ae4fa6c43d6]*/
  1397. {
  1398. int a_too_large, c_bit_length;
  1399. size_t c, d;
  1400. uint64_t m, u;
  1401. PyObject *a = NULL, *b;
  1402. n = PyNumber_Index(n);
  1403. if (n == NULL) {
  1404. return NULL;
  1405. }
  1406. if (_PyLong_Sign(n) < 0) {
  1407. PyErr_SetString(
  1408. PyExc_ValueError,
  1409. "isqrt() argument must be nonnegative");
  1410. goto error;
  1411. }
  1412. if (_PyLong_Sign(n) == 0) {
  1413. Py_DECREF(n);
  1414. return PyLong_FromLong(0);
  1415. }
  1416. /* c = (n.bit_length() - 1) // 2 */
  1417. c = _PyLong_NumBits(n);
  1418. if (c == (size_t)(-1)) {
  1419. goto error;
  1420. }
  1421. c = (c - 1U) / 2U;
  1422. /* Fast path: if c <= 31 then n < 2**64 and we can compute directly with a
  1423. fast, almost branch-free algorithm. In the final correction, we use `u*u
  1424. - 1 >= m` instead of the simpler `u*u > m` in order to get the correct
  1425. result in the corner case where `u=2**32`. */
  1426. if (c <= 31U) {
  1427. m = (uint64_t)PyLong_AsUnsignedLongLong(n);
  1428. Py_DECREF(n);
  1429. if (m == (uint64_t)(-1) && PyErr_Occurred()) {
  1430. return NULL;
  1431. }
  1432. u = _approximate_isqrt(m << (62U - 2U*c)) >> (31U - c);
  1433. u -= u * u - 1U >= m;
  1434. return PyLong_FromUnsignedLongLong((unsigned long long)u);
  1435. }
  1436. /* Slow path: n >= 2**64. We perform the first five iterations in C integer
  1437. arithmetic, then switch to using Python long integers. */
  1438. /* From n >= 2**64 it follows that c.bit_length() >= 6. */
  1439. c_bit_length = 6;
  1440. while ((c >> c_bit_length) > 0U) {
  1441. ++c_bit_length;
  1442. }
  1443. /* Initialise d and a. */
  1444. d = c >> (c_bit_length - 5);
  1445. b = _PyLong_Rshift(n, 2U*c - 62U);
  1446. if (b == NULL) {
  1447. goto error;
  1448. }
  1449. m = (uint64_t)PyLong_AsUnsignedLongLong(b);
  1450. Py_DECREF(b);
  1451. if (m == (uint64_t)(-1) && PyErr_Occurred()) {
  1452. goto error;
  1453. }
  1454. u = _approximate_isqrt(m) >> (31U - d);
  1455. a = PyLong_FromUnsignedLongLong((unsigned long long)u);
  1456. if (a == NULL) {
  1457. goto error;
  1458. }
  1459. for (int s = c_bit_length - 6; s >= 0; --s) {
  1460. PyObject *q;
  1461. size_t e = d;
  1462. d = c >> s;
  1463. /* q = (n >> 2*c - e - d + 1) // a */
  1464. q = _PyLong_Rshift(n, 2U*c - d - e + 1U);
  1465. if (q == NULL) {
  1466. goto error;
  1467. }
  1468. Py_SETREF(q, PyNumber_FloorDivide(q, a));
  1469. if (q == NULL) {
  1470. goto error;
  1471. }
  1472. /* a = (a << d - 1 - e) + q */
  1473. Py_SETREF(a, _PyLong_Lshift(a, d - 1U - e));
  1474. if (a == NULL) {
  1475. Py_DECREF(q);
  1476. goto error;
  1477. }
  1478. Py_SETREF(a, PyNumber_Add(a, q));
  1479. Py_DECREF(q);
  1480. if (a == NULL) {
  1481. goto error;
  1482. }
  1483. }
  1484. /* The correct result is either a or a - 1. Figure out which, and
  1485. decrement a if necessary. */
  1486. /* a_too_large = n < a * a */
  1487. b = PyNumber_Multiply(a, a);
  1488. if (b == NULL) {
  1489. goto error;
  1490. }
  1491. a_too_large = PyObject_RichCompareBool(n, b, Py_LT);
  1492. Py_DECREF(b);
  1493. if (a_too_large == -1) {
  1494. goto error;
  1495. }
  1496. if (a_too_large) {
  1497. Py_SETREF(a, PyNumber_Subtract(a, _PyLong_One));
  1498. }
  1499. Py_DECREF(n);
  1500. return a;
  1501. error:
  1502. Py_XDECREF(a);
  1503. Py_DECREF(n);
  1504. return NULL;
  1505. }
  1506. /* Divide-and-conquer factorial algorithm
  1507. *
  1508. * Based on the formula and pseudo-code provided at:
  1509. * http://www.luschny.de/math/factorial/binarysplitfact.html
  1510. *
  1511. * Faster algorithms exist, but they're more complicated and depend on
  1512. * a fast prime factorization algorithm.
  1513. *
  1514. * Notes on the algorithm
  1515. * ----------------------
  1516. *
  1517. * factorial(n) is written in the form 2**k * m, with m odd. k and m are
  1518. * computed separately, and then combined using a left shift.
  1519. *
  1520. * The function factorial_odd_part computes the odd part m (i.e., the greatest
  1521. * odd divisor) of factorial(n), using the formula:
  1522. *
  1523. * factorial_odd_part(n) =
  1524. *
  1525. * product_{i >= 0} product_{0 < j <= n / 2**i, j odd} j
  1526. *
  1527. * Example: factorial_odd_part(20) =
  1528. *
  1529. * (1) *
  1530. * (1) *
  1531. * (1 * 3 * 5) *
  1532. * (1 * 3 * 5 * 7 * 9)
  1533. * (1 * 3 * 5 * 7 * 9 * 11 * 13 * 15 * 17 * 19)
  1534. *
  1535. * Here i goes from large to small: the first term corresponds to i=4 (any
  1536. * larger i gives an empty product), and the last term corresponds to i=0.
  1537. * Each term can be computed from the last by multiplying by the extra odd
  1538. * numbers required: e.g., to get from the penultimate term to the last one,
  1539. * we multiply by (11 * 13 * 15 * 17 * 19).
  1540. *
  1541. * To see a hint of why this formula works, here are the same numbers as above
  1542. * but with the even parts (i.e., the appropriate powers of 2) included. For
  1543. * each subterm in the product for i, we multiply that subterm by 2**i:
  1544. *
  1545. * factorial(20) =
  1546. *
  1547. * (16) *
  1548. * (8) *
  1549. * (4 * 12 * 20) *
  1550. * (2 * 6 * 10 * 14 * 18) *
  1551. * (1 * 3 * 5 * 7 * 9 * 11 * 13 * 15 * 17 * 19)
  1552. *
  1553. * The factorial_partial_product function computes the product of all odd j in
  1554. * range(start, stop) for given start and stop. It's used to compute the
  1555. * partial products like (11 * 13 * 15 * 17 * 19) in the example above. It
  1556. * operates recursively, repeatedly splitting the range into two roughly equal
  1557. * pieces until the subranges are small enough to be computed using only C
  1558. * integer arithmetic.
  1559. *
  1560. * The two-valuation k (i.e., the exponent of the largest power of 2 dividing
  1561. * the factorial) is computed independently in the main math_factorial
  1562. * function. By standard results, its value is:
  1563. *
  1564. * two_valuation = n//2 + n//4 + n//8 + ....
  1565. *
  1566. * It can be shown (e.g., by complete induction on n) that two_valuation is
  1567. * equal to n - count_set_bits(n), where count_set_bits(n) gives the number of
  1568. * '1'-bits in the binary expansion of n.
  1569. */
  1570. /* factorial_partial_product: Compute product(range(start, stop, 2)) using
  1571. * divide and conquer. Assumes start and stop are odd and stop > start.
  1572. * max_bits must be >= bit_length(stop - 2). */
  1573. static PyObject *
  1574. factorial_partial_product(unsigned long start, unsigned long stop,
  1575. unsigned long max_bits)
  1576. {
  1577. unsigned long midpoint, num_operands;
  1578. PyObject *left = NULL, *right = NULL, *result = NULL;
  1579. /* If the return value will fit an unsigned long, then we can
  1580. * multiply in a tight, fast loop where each multiply is O(1).
  1581. * Compute an upper bound on the number of bits required to store
  1582. * the answer.
  1583. *
  1584. * Storing some integer z requires floor(lg(z))+1 bits, which is
  1585. * conveniently the value returned by bit_length(z). The
  1586. * product x*y will require at most
  1587. * bit_length(x) + bit_length(y) bits to store, based
  1588. * on the idea that lg product = lg x + lg y.
  1589. *
  1590. * We know that stop - 2 is the largest number to be multiplied. From
  1591. * there, we have: bit_length(answer) <= num_operands *
  1592. * bit_length(stop - 2)
  1593. */
  1594. num_operands = (stop - start) / 2;
  1595. /* The "num_operands <= 8 * SIZEOF_LONG" check guards against the
  1596. * unlikely case of an overflow in num_operands * max_bits. */
  1597. if (num_operands <= 8 * SIZEOF_LONG &&
  1598. num_operands * max_bits <= 8 * SIZEOF_LONG) {
  1599. unsigned long j, total;
  1600. for (total = start, j = start + 2; j < stop; j += 2)
  1601. total *= j;
  1602. return PyLong_FromUnsignedLong(total);
  1603. }
  1604. /* find midpoint of range(start, stop), rounded up to next odd number. */
  1605. midpoint = (start + num_operands) | 1;
  1606. left = factorial_partial_product(start, midpoint,
  1607. bit_length(midpoint - 2));
  1608. if (left == NULL)
  1609. goto error;
  1610. right = factorial_partial_product(midpoint, stop, max_bits);
  1611. if (right == NULL)
  1612. goto error;
  1613. result = PyNumber_Multiply(left, right);
  1614. error:
  1615. Py_XDECREF(left);
  1616. Py_XDECREF(right);
  1617. return result;
  1618. }
  1619. /* factorial_odd_part: compute the odd part of factorial(n). */
  1620. static PyObject *
  1621. factorial_odd_part(unsigned long n)
  1622. {
  1623. long i;
  1624. unsigned long v, lower, upper;
  1625. PyObject *partial, *tmp, *inner, *outer;
  1626. inner = PyLong_FromLong(1);
  1627. if (inner == NULL)
  1628. return NULL;
  1629. outer = inner;
  1630. Py_INCREF(outer);
  1631. upper = 3;
  1632. for (i = bit_length(n) - 2; i >= 0; i--) {
  1633. v = n >> i;
  1634. if (v <= 2)
  1635. continue;
  1636. lower = upper;
  1637. /* (v + 1) | 1 = least odd integer strictly larger than n / 2**i */
  1638. upper = (v + 1) | 1;
  1639. /* Here inner is the product of all odd integers j in the range (0,
  1640. n/2**(i+1)]. The factorial_partial_product call below gives the
  1641. product of all odd integers j in the range (n/2**(i+1), n/2**i]. */
  1642. partial = factorial_partial_product(lower, upper, bit_length(upper-2));
  1643. /* inner *= partial */
  1644. if (partial == NULL)
  1645. goto error;
  1646. tmp = PyNumber_Multiply(inner, partial);
  1647. Py_DECREF(partial);
  1648. if (tmp == NULL)
  1649. goto error;
  1650. Py_DECREF(inner);
  1651. inner = tmp;
  1652. /* Now inner is the product of all odd integers j in the range (0,
  1653. n/2**i], giving the inner product in the formula above. */
  1654. /* outer *= inner; */
  1655. tmp = PyNumber_Multiply(outer, inner);
  1656. if (tmp == NULL)
  1657. goto error;
  1658. Py_DECREF(outer);
  1659. outer = tmp;
  1660. }
  1661. Py_DECREF(inner);
  1662. return outer;
  1663. error:
  1664. Py_DECREF(outer);
  1665. Py_DECREF(inner);
  1666. return NULL;
  1667. }
  1668. /* Lookup table for small factorial values */
  1669. static const unsigned long SmallFactorials[] = {
  1670. 1, 1, 2, 6, 24, 120, 720, 5040, 40320,
  1671. 362880, 3628800, 39916800, 479001600,
  1672. #if SIZEOF_LONG >= 8
  1673. 6227020800, 87178291200, 1307674368000,
  1674. 20922789888000, 355687428096000, 6402373705728000,
  1675. 121645100408832000, 2432902008176640000
  1676. #endif
  1677. };
  1678. /*[clinic input]
  1679. math.factorial
  1680. x as arg: object
  1681. /
  1682. Find x!.
  1683. Raise a ValueError if x is negative or non-integral.
  1684. [clinic start generated code]*/
  1685. static PyObject *
  1686. math_factorial(PyObject *module, PyObject *arg)
  1687. /*[clinic end generated code: output=6686f26fae00e9ca input=6d1c8105c0d91fb4]*/
  1688. {
  1689. long x, two_valuation;
  1690. int overflow;
  1691. PyObject *result, *odd_part, *pyint_form;
  1692. if (PyFloat_Check(arg)) {
  1693. if (PyErr_WarnEx(PyExc_DeprecationWarning,
  1694. "Using factorial() with floats is deprecated",
  1695. 1) < 0)
  1696. {
  1697. return NULL;
  1698. }
  1699. PyObject *lx;
  1700. double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg);
  1701. if (!(Py_IS_FINITE(dx) && dx == floor(dx))) {
  1702. PyErr_SetString(PyExc_ValueError,
  1703. "factorial() only accepts integral values");
  1704. return NULL;
  1705. }
  1706. lx = PyLong_FromDouble(dx);
  1707. if (lx == NULL)
  1708. return NULL;
  1709. x = PyLong_AsLongAndOverflow(lx, &overflow);
  1710. Py_DECREF(lx);
  1711. }
  1712. else {
  1713. pyint_form = PyNumber_Index(arg);
  1714. if (pyint_form == NULL) {
  1715. return NULL;
  1716. }
  1717. x = PyLong_AsLongAndOverflow(pyint_form, &overflow);
  1718. Py_DECREF(pyint_form);
  1719. }
  1720. if (x == -1 && PyErr_Occurred()) {
  1721. return NULL;
  1722. }
  1723. else if (overflow == 1) {
  1724. PyErr_Format(PyExc_OverflowError,
  1725. "factorial() argument should not exceed %ld",
  1726. LONG_MAX);
  1727. return NULL;
  1728. }
  1729. else if (overflow == -1 || x < 0) {
  1730. PyErr_SetString(PyExc_ValueError,
  1731. "factorial() not defined for negative values");
  1732. return NULL;
  1733. }
  1734. /* use lookup table if x is small */
  1735. if (x < (long)Py_ARRAY_LENGTH(SmallFactorials))
  1736. return PyLong_FromUnsignedLong(SmallFactorials[x]);
  1737. /* else express in the form odd_part * 2**two_valuation, and compute as
  1738. odd_part << two_valuation. */
  1739. odd_part = factorial_odd_part(x);
  1740. if (odd_part == NULL)
  1741. return NULL;
  1742. two_valuation = x - count_set_bits(x);
  1743. result = _PyLong_Lshift(odd_part, two_valuation);
  1744. Py_DECREF(odd_part);
  1745. return result;
  1746. }
  1747. /*[clinic input]
  1748. math.trunc
  1749. x: object
  1750. /
  1751. Truncates the Real x to the nearest Integral toward 0.
  1752. Uses the __trunc__ magic method.
  1753. [clinic start generated code]*/
  1754. static PyObject *
  1755. math_trunc(PyObject *module, PyObject *x)
  1756. /*[clinic end generated code: output=34b9697b707e1031 input=2168b34e0a09134d]*/
  1757. {
  1758. _Py_IDENTIFIER(__trunc__);
  1759. PyObject *trunc, *result;
  1760. if (Py_TYPE(x)->tp_dict == NULL) {
  1761. if (PyType_Ready(Py_TYPE(x)) < 0)
  1762. return NULL;
  1763. }
  1764. trunc = _PyObject_LookupSpecial(x, &PyId___trunc__);
  1765. if (trunc == NULL) {
  1766. if (!PyErr_Occurred())
  1767. PyErr_Format(PyExc_TypeError,
  1768. "type %.100s doesn't define __trunc__ method",
  1769. Py_TYPE(x)->tp_name);
  1770. return NULL;
  1771. }
  1772. result = _PyObject_CallNoArg(trunc);
  1773. Py_DECREF(trunc);
  1774. return result;
  1775. }
  1776. /*[clinic input]
  1777. math.frexp
  1778. x: double
  1779. /
  1780. Return the mantissa and exponent of x, as pair (m, e).
  1781. m is a float and e is an int, such that x = m * 2.**e.
  1782. If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
  1783. [clinic start generated code]*/
  1784. static PyObject *
  1785. math_frexp_impl(PyObject *module, double x)
  1786. /*[clinic end generated code: output=03e30d252a15ad4a input=96251c9e208bc6e9]*/
  1787. {
  1788. int i;
  1789. /* deal with special cases directly, to sidestep platform
  1790. differences */
  1791. if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) {
  1792. i = 0;
  1793. }
  1794. else {
  1795. PyFPE_START_PROTECT("in math_frexp", return 0);
  1796. x = frexp(x, &i);
  1797. PyFPE_END_PROTECT(x);
  1798. }
  1799. return Py_BuildValue("(di)", x, i);
  1800. }
  1801. /*[clinic input]
  1802. math.ldexp
  1803. x: double
  1804. i: object
  1805. /
  1806. Return x * (2**i).
  1807. This is essentially the inverse of frexp().
  1808. [clinic start generated code]*/
  1809. static PyObject *
  1810. math_ldexp_impl(PyObject *module, double x, PyObject *i)
  1811. /*[clinic end generated code: output=b6892f3c2df9cc6a input=17d5970c1a40a8c1]*/
  1812. {
  1813. double r;
  1814. long exp;
  1815. int overflow;
  1816. if (PyLong_Check(i)) {
  1817. /* on overflow, replace exponent with either LONG_MAX
  1818. or LONG_MIN, depending on the sign. */
  1819. exp = PyLong_AsLongAndOverflow(i, &overflow);
  1820. if (exp == -1 && PyErr_Occurred())
  1821. return NULL;
  1822. if (overflow)
  1823. exp = overflow < 0 ? LONG_MIN : LONG_MAX;
  1824. }
  1825. else {
  1826. PyErr_SetString(PyExc_TypeError,
  1827. "Expected an int as second argument to ldexp.");
  1828. return NULL;
  1829. }
  1830. if (x == 0. || !Py_IS_FINITE(x)) {
  1831. /* NaNs, zeros and infinities are returned unchanged */
  1832. r = x;
  1833. errno = 0;
  1834. } else if (exp > INT_MAX) {
  1835. /* overflow */
  1836. r = copysign(Py_HUGE_VAL, x);
  1837. errno = ERANGE;
  1838. } else if (exp < INT_MIN) {
  1839. /* underflow to +-0 */
  1840. r = copysign(0., x);
  1841. errno = 0;
  1842. } else {
  1843. errno = 0;
  1844. PyFPE_START_PROTECT("in math_ldexp", return 0);
  1845. r = ldexp(x, (int)exp);
  1846. PyFPE_END_PROTECT(r);
  1847. if (Py_IS_INFINITY(r))
  1848. errno = ERANGE;
  1849. }
  1850. if (errno && is_error(r))
  1851. return NULL;
  1852. return PyFloat_FromDouble(r);
  1853. }
  1854. /*[clinic input]
  1855. math.modf
  1856. x: double
  1857. /
  1858. Return the fractional and integer parts of x.
  1859. Both results carry the sign of x and are floats.
  1860. [clinic start generated code]*/
  1861. static PyObject *
  1862. math_modf_impl(PyObject *module, double x)
  1863. /*[clinic end generated code: output=90cee0260014c3c0 input=b4cfb6786afd9035]*/
  1864. {
  1865. double y;
  1866. /* some platforms don't do the right thing for NaNs and
  1867. infinities, so we take care of special cases directly. */
  1868. if (!Py_IS_FINITE(x)) {
  1869. if (Py_IS_INFINITY(x))
  1870. return Py_BuildValue("(dd)", copysign(0., x), x);
  1871. else if (Py_IS_NAN(x))
  1872. return Py_BuildValue("(dd)", x, x);
  1873. }
  1874. errno = 0;
  1875. PyFPE_START_PROTECT("in math_modf", return 0);
  1876. x = modf(x, &y);
  1877. PyFPE_END_PROTECT(x);
  1878. return Py_BuildValue("(dd)", x, y);
  1879. }
  1880. /* A decent logarithm is easy to compute even for huge ints, but libm can't
  1881. do that by itself -- loghelper can. func is log or log10, and name is
  1882. "log" or "log10". Note that overflow of the result isn't possible: an int
  1883. can contain no more than INT_MAX * SHIFT bits, so has value certainly less
  1884. than 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
  1885. small enough to fit in an IEEE single. log and log10 are even smaller.
  1886. However, intermediate overflow is possible for an int if the number of bits
  1887. in that int is larger than PY_SSIZE_T_MAX. */
  1888. static PyObject*
  1889. loghelper(PyObject* arg, double (*func)(double), const char *funcname)
  1890. {
  1891. /* If it is int, do it ourselves. */
  1892. if (PyLong_Check(arg)) {
  1893. double x, result;
  1894. Py_ssize_t e;
  1895. /* Negative or zero inputs give a ValueError. */
  1896. if (Py_SIZE(arg) <= 0) {
  1897. PyErr_SetString(PyExc_ValueError,
  1898. "math domain error");
  1899. return NULL;
  1900. }
  1901. x = PyLong_AsDouble(arg);
  1902. if (x == -1.0 && PyErr_Occurred()) {
  1903. if (!PyErr_ExceptionMatches(PyExc_OverflowError))
  1904. return NULL;
  1905. /* Here the conversion to double overflowed, but it's possible
  1906. to compute the log anyway. Clear the exception and continue. */
  1907. PyErr_Clear();
  1908. x = _PyLong_Frexp((PyLongObject *)arg, &e);
  1909. if (x == -1.0 && PyErr_Occurred())
  1910. return NULL;
  1911. /* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */
  1912. result = func(x) + func(2.0) * e;
  1913. }
  1914. else
  1915. /* Successfully converted x to a double. */
  1916. result = func(x);
  1917. return PyFloat_FromDouble(result);
  1918. }
  1919. /* Else let libm handle it by itself. */
  1920. return math_1(arg, func, 0);
  1921. }
  1922. /*[clinic input]
  1923. math.log
  1924. x: object
  1925. [
  1926. base: object(c_default="NULL") = math.e
  1927. ]
  1928. /
  1929. Return the logarithm of x to the given base.
  1930. If the base not specified, returns the natural logarithm (base e) of x.
  1931. [clinic start generated code]*/
  1932. static PyObject *
  1933. math_log_impl(PyObject *module, PyObject *x, int group_right_1,
  1934. PyObject *base)
  1935. /*[clinic end generated code: output=7b5a39e526b73fc9 input=0f62d5726cbfebbd]*/
  1936. {
  1937. PyObject *num, *den;
  1938. PyObject *ans;
  1939. num = loghelper(x, m_log, "log");
  1940. if (num == NULL || base == NULL)
  1941. return num;
  1942. den = loghelper(base, m_log, "log");
  1943. if (den == NULL) {
  1944. Py_DECREF(num);
  1945. return NULL;
  1946. }
  1947. ans = PyNumber_TrueDivide(num, den);
  1948. Py_DECREF(num);
  1949. Py_DECREF(den);
  1950. return ans;
  1951. }
  1952. /*[clinic input]
  1953. math.log2
  1954. x: object
  1955. /
  1956. Return the base 2 logarithm of x.
  1957. [clinic start generated code]*/
  1958. static PyObject *
  1959. math_log2(PyObject *module, PyObject *x)
  1960. /*[clinic end generated code: output=5425899a4d5d6acb input=08321262bae4f39b]*/
  1961. {
  1962. return loghelper(x, m_log2, "log2");
  1963. }
  1964. /*[clinic input]
  1965. math.log10
  1966. x: object
  1967. /
  1968. Return the base 10 logarithm of x.
  1969. [clinic start generated code]*/
  1970. static PyObject *
  1971. math_log10(PyObject *module, PyObject *x)
  1972. /*[clinic end generated code: output=be72a64617df9c6f input=b2469d02c6469e53]*/
  1973. {
  1974. return loghelper(x, m_log10, "log10");
  1975. }
  1976. /*[clinic input]
  1977. math.fmod
  1978. x: double
  1979. y: double
  1980. /
  1981. Return fmod(x, y), according to platform C.
  1982. x % y may differ.
  1983. [clinic start generated code]*/
  1984. static PyObject *
  1985. math_fmod_impl(PyObject *module, double x, double y)
  1986. /*[clinic end generated code: output=7559d794343a27b5 input=4f84caa8cfc26a03]*/
  1987. {
  1988. double r;
  1989. /* fmod(x, +/-Inf) returns x for finite x. */
  1990. if (Py_IS_INFINITY(y) && Py_IS_FINITE(x))
  1991. return PyFloat_FromDouble(x);
  1992. errno = 0;
  1993. PyFPE_START_PROTECT("in math_fmod", return 0);
  1994. r = fmod(x, y);
  1995. PyFPE_END_PROTECT(r);
  1996. if (Py_IS_NAN(r)) {
  1997. if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
  1998. errno = EDOM;
  1999. else
  2000. errno = 0;
  2001. }
  2002. if (errno && is_error(r))
  2003. return NULL;
  2004. else
  2005. return PyFloat_FromDouble(r);
  2006. }
  2007. /*
  2008. Given an *n* length *vec* of values and a value *max*, compute:
  2009. max * sqrt(sum((x / max) ** 2 for x in vec))
  2010. The value of the *max* variable must be non-negative and
  2011. equal to the absolute value of the largest magnitude
  2012. entry in the vector. If n==0, then *max* should be 0.0.
  2013. If an infinity is present in the vec, *max* should be INF.
  2014. The *found_nan* variable indicates whether some member of
  2015. the *vec* is a NaN.
  2016. To improve accuracy and to increase the number of cases where
  2017. vector_norm() is commutative, we use a variant of Neumaier
  2018. summation specialized to exploit that we always know that
  2019. |csum| >= |x|.
  2020. The *csum* variable tracks the cumulative sum and *frac* tracks
  2021. the cumulative fractional errors at each step. Since this
  2022. variant assumes that |csum| >= |x| at each step, we establish
  2023. the precondition by starting the accumulation from 1.0 which
  2024. represents the largest possible value of (x/max)**2.
  2025. After the loop is finished, the initial 1.0 is subtracted out
  2026. for a net zero effect on the final sum. Since *csum* will be
  2027. greater than 1.0, the subtraction of 1.0 will not cause
  2028. fractional digits to be dropped from *csum*.
  2029. */
  2030. static inline double
  2031. vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
  2032. {
  2033. double x, csum = 1.0, oldcsum, frac = 0.0;
  2034. Py_ssize_t i;
  2035. if (Py_IS_INFINITY(max)) {
  2036. return max;
  2037. }
  2038. if (found_nan) {
  2039. return Py_NAN;
  2040. }
  2041. if (max == 0.0 || n <= 1) {
  2042. return max;
  2043. }
  2044. for (i=0 ; i < n ; i++) {
  2045. x = vec[i];
  2046. assert(Py_IS_FINITE(x) && fabs(x) <= max);
  2047. x /= max;
  2048. x = x*x;
  2049. oldcsum = csum;
  2050. csum += x;
  2051. assert(csum >= x);
  2052. frac += (oldcsum - csum) + x;
  2053. }
  2054. return max * sqrt(csum - 1.0 + frac);
  2055. }
  2056. #define NUM_STACK_ELEMS 16
  2057. /*[clinic input]
  2058. math.dist
  2059. p: object
  2060. q: object
  2061. /
  2062. Return the Euclidean distance between two points p and q.
  2063. The points should be specified as sequences (or iterables) of
  2064. coordinates. Both inputs must have the same dimension.
  2065. Roughly equivalent to:
  2066. sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))
  2067. [clinic start generated code]*/
  2068. static PyObject *
  2069. math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
  2070. /*[clinic end generated code: output=56bd9538d06bbcfe input=74e85e1b6092e68e]*/
  2071. {
  2072. PyObject *item;
  2073. double max = 0.0;
  2074. double x, px, qx, result;
  2075. Py_ssize_t i, m, n;
  2076. int found_nan = 0, p_allocated = 0, q_allocated = 0;
  2077. double diffs_on_stack[NUM_STACK_ELEMS];
  2078. double *diffs = diffs_on_stack;
  2079. if (!PyTuple_Check(p)) {
  2080. p = PySequence_Tuple(p);
  2081. if (p == NULL) {
  2082. return NULL;
  2083. }
  2084. p_allocated = 1;
  2085. }
  2086. if (!PyTuple_Check(q)) {
  2087. q = PySequence_Tuple(q);
  2088. if (q == NULL) {
  2089. if (p_allocated) {
  2090. Py_DECREF(p);
  2091. }
  2092. return NULL;
  2093. }
  2094. q_allocated = 1;
  2095. }
  2096. m = PyTuple_GET_SIZE(p);
  2097. n = PyTuple_GET_SIZE(q);
  2098. if (m != n) {
  2099. PyErr_SetString(PyExc_ValueError,
  2100. "both points must have the same number of dimensions");
  2101. return NULL;
  2102. }
  2103. if (n > NUM_STACK_ELEMS) {
  2104. diffs = (double *) PyObject_Malloc(n * sizeof(double));
  2105. if (diffs == NULL) {
  2106. return PyErr_NoMemory();
  2107. }
  2108. }
  2109. for (i=0 ; i<n ; i++) {
  2110. item = PyTuple_GET_ITEM(p, i);
  2111. ASSIGN_DOUBLE(px, item, error_exit);
  2112. item = PyTuple_GET_ITEM(q, i);
  2113. ASSIGN_DOUBLE(qx, item, error_exit);
  2114. x = fabs(px - qx);
  2115. diffs[i] = x;
  2116. found_nan |= Py_IS_NAN(x);
  2117. if (x > max) {
  2118. max = x;
  2119. }
  2120. }
  2121. result = vector_norm(n, diffs, max, found_nan);
  2122. if (diffs != diffs_on_stack) {
  2123. PyObject_Free(diffs);
  2124. }
  2125. if (p_allocated) {
  2126. Py_DECREF(p);
  2127. }
  2128. if (q_allocated) {
  2129. Py_DECREF(q);
  2130. }
  2131. return PyFloat_FromDouble(result);
  2132. error_exit:
  2133. if (diffs != diffs_on_stack) {
  2134. PyObject_Free(diffs);
  2135. }
  2136. if (p_allocated) {
  2137. Py_DECREF(p);
  2138. }
  2139. if (q_allocated) {
  2140. Py_DECREF(q);
  2141. }
  2142. return NULL;
  2143. }
  2144. /* AC: cannot convert yet, waiting for *args support */
  2145. static PyObject *
  2146. math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
  2147. {
  2148. Py_ssize_t i;
  2149. PyObject *item;
  2150. double max = 0.0;
  2151. double x, result;
  2152. int found_nan = 0;
  2153. double coord_on_stack[NUM_STACK_ELEMS];
  2154. double *coordinates = coord_on_stack;
  2155. if (nargs > NUM_STACK_ELEMS) {
  2156. coordinates = (double *) PyObject_Malloc(nargs * sizeof(double));
  2157. if (coordinates == NULL) {
  2158. return PyErr_NoMemory();
  2159. }
  2160. }
  2161. for (i = 0; i < nargs; i++) {
  2162. item = args[i];
  2163. ASSIGN_DOUBLE(x, item, error_exit);
  2164. x = fabs(x);
  2165. coordinates[i] = x;
  2166. found_nan |= Py_IS_NAN(x);
  2167. if (x > max) {
  2168. max = x;
  2169. }
  2170. }
  2171. result = vector_norm(nargs, coordinates, max, found_nan);
  2172. if (coordinates != coord_on_stack) {
  2173. PyObject_Free(coordinates);
  2174. }
  2175. return PyFloat_FromDouble(result);
  2176. error_exit:
  2177. if (coordinates != coord_on_stack) {
  2178. PyObject_Free(coordinates);
  2179. }
  2180. return NULL;
  2181. }
  2182. #undef NUM_STACK_ELEMS
  2183. PyDoc_STRVAR(math_hypot_doc,
  2184. "hypot(*coordinates) -> value\n\n\
  2185. Multidimensional Euclidean distance from the origin to a point.\n\
  2186. \n\
  2187. Roughly equivalent to:\n\
  2188. sqrt(sum(x**2 for x in coordinates))\n\
  2189. \n\
  2190. For a two dimensional point (x, y), gives the hypotenuse\n\
  2191. using the Pythagorean theorem: sqrt(x*x + y*y).\n\
  2192. \n\
  2193. For example, the hypotenuse of a 3/4/5 right triangle is:\n\
  2194. \n\
  2195. >>> hypot(3.0, 4.0)\n\
  2196. 5.0\n\
  2197. ");
  2198. /* pow can't use math_2, but needs its own wrapper: the problem is
  2199. that an infinite result can arise either as a result of overflow
  2200. (in which case OverflowError should be raised) or as a result of
  2201. e.g. 0.**-5. (for which ValueError needs to be raised.)
  2202. */
  2203. /*[clinic input]
  2204. math.pow
  2205. x: double
  2206. y: double
  2207. /
  2208. Return x**y (x to the power of y).
  2209. [clinic start generated code]*/
  2210. static PyObject *
  2211. math_pow_impl(PyObject *module, double x, double y)
  2212. /*[clinic end generated code: output=fff93e65abccd6b0 input=c26f1f6075088bfd]*/
  2213. {
  2214. double r;
  2215. int odd_y;
  2216. /* deal directly with IEEE specials, to cope with problems on various
  2217. platforms whose semantics don't exactly match C99 */
  2218. r = 0.; /* silence compiler warning */
  2219. if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) {
  2220. errno = 0;
  2221. if (Py_IS_NAN(x))
  2222. r = y == 0. ? 1. : x; /* NaN**0 = 1 */
  2223. else if (Py_IS_NAN(y))
  2224. r = x == 1. ? 1. : y; /* 1**NaN = 1 */
  2225. else if (Py_IS_INFINITY(x)) {
  2226. odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0;
  2227. if (y > 0.)
  2228. r = odd_y ? x : fabs(x);
  2229. else if (y == 0.)
  2230. r = 1.;
  2231. else /* y < 0. */
  2232. r = odd_y ? copysign(0., x) : 0.;
  2233. }
  2234. else if (Py_IS_INFINITY(y)) {
  2235. if (fabs(x) == 1.0)
  2236. r = 1.;
  2237. else if (y > 0. && fabs(x) > 1.0)
  2238. r = y;
  2239. else if (y < 0. && fabs(x) < 1.0) {
  2240. r = -y; /* result is +inf */
  2241. if (x == 0.) /* 0**-inf: divide-by-zero */
  2242. errno = EDOM;
  2243. }
  2244. else
  2245. r = 0.;
  2246. }
  2247. }
  2248. else {
  2249. /* let libm handle finite**finite */
  2250. errno = 0;
  2251. PyFPE_START_PROTECT("in math_pow", return 0);
  2252. r = pow(x, y);
  2253. PyFPE_END_PROTECT(r);
  2254. /* a NaN result should arise only from (-ve)**(finite
  2255. non-integer); in this case we want to raise ValueError. */
  2256. if (!Py_IS_FINITE(r)) {
  2257. if (Py_IS_NAN(r)) {
  2258. errno = EDOM;
  2259. }
  2260. /*
  2261. an infinite result here arises either from:
  2262. (A) (+/-0.)**negative (-> divide-by-zero)
  2263. (B) overflow of x**y with x and y finite
  2264. */
  2265. else if (Py_IS_INFINITY(r)) {
  2266. if (x == 0.)
  2267. errno = EDOM;
  2268. else
  2269. errno = ERANGE;
  2270. }
  2271. }
  2272. }
  2273. if (errno && is_error(r))
  2274. return NULL;
  2275. else
  2276. return PyFloat_FromDouble(r);
  2277. }
  2278. static const double degToRad = Py_MATH_PI / 180.0;
  2279. static const double radToDeg = 180.0 / Py_MATH_PI;
  2280. /*[clinic input]
  2281. math.degrees
  2282. x: double
  2283. /
  2284. Convert angle x from radians to degrees.
  2285. [clinic start generated code]*/
  2286. static PyObject *
  2287. math_degrees_impl(PyObject *module, double x)
  2288. /*[clinic end generated code: output=7fea78b294acd12f input=81e016555d6e3660]*/
  2289. {
  2290. return PyFloat_FromDouble(x * radToDeg);
  2291. }
  2292. /*[clinic input]
  2293. math.radians
  2294. x: double
  2295. /
  2296. Convert angle x from degrees to radians.
  2297. [clinic start generated code]*/
  2298. static PyObject *
  2299. math_radians_impl(PyObject *module, double x)
  2300. /*[clinic end generated code: output=34daa47caf9b1590 input=91626fc489fe3d63]*/
  2301. {
  2302. return PyFloat_FromDouble(x * degToRad);
  2303. }
  2304. /*[clinic input]
  2305. math.isfinite
  2306. x: double
  2307. /
  2308. Return True if x is neither an infinity nor a NaN, and False otherwise.
  2309. [clinic start generated code]*/
  2310. static PyObject *
  2311. math_isfinite_impl(PyObject *module, double x)
  2312. /*[clinic end generated code: output=8ba1f396440c9901 input=46967d254812e54a]*/
  2313. {
  2314. return PyBool_FromLong((long)Py_IS_FINITE(x));
  2315. }
  2316. /*[clinic input]
  2317. math.isnan
  2318. x: double
  2319. /
  2320. Return True if x is a NaN (not a number), and False otherwise.
  2321. [clinic start generated code]*/
  2322. static PyObject *
  2323. math_isnan_impl(PyObject *module, double x)
  2324. /*[clinic end generated code: output=f537b4d6df878c3e input=935891e66083f46a]*/
  2325. {
  2326. return PyBool_FromLong((long)Py_IS_NAN(x));
  2327. }
  2328. /*[clinic input]
  2329. math.isinf
  2330. x: double
  2331. /
  2332. Return True if x is a positive or negative infinity, and False otherwise.
  2333. [clinic start generated code]*/
  2334. static PyObject *
  2335. math_isinf_impl(PyObject *module, double x)
  2336. /*[clinic end generated code: output=9f00cbec4de7b06b input=32630e4212cf961f]*/
  2337. {
  2338. return PyBool_FromLong((long)Py_IS_INFINITY(x));
  2339. }
  2340. /*[clinic input]
  2341. math.isclose -> bool
  2342. a: double
  2343. b: double
  2344. *
  2345. rel_tol: double = 1e-09
  2346. maximum difference for being considered "close", relative to the
  2347. magnitude of the input values
  2348. abs_tol: double = 0.0
  2349. maximum difference for being considered "close", regardless of the
  2350. magnitude of the input values
  2351. Determine whether two floating point numbers are close in value.
  2352. Return True if a is close in value to b, and False otherwise.
  2353. For the values to be considered close, the difference between them
  2354. must be smaller than at least one of the tolerances.
  2355. -inf, inf and NaN behave similarly to the IEEE 754 Standard. That
  2356. is, NaN is not close to anything, even itself. inf and -inf are
  2357. only close to themselves.
  2358. [clinic start generated code]*/
  2359. static int
  2360. math_isclose_impl(PyObject *module, double a, double b, double rel_tol,
  2361. double abs_tol)
  2362. /*[clinic end generated code: output=b73070207511952d input=f28671871ea5bfba]*/
  2363. {
  2364. double diff = 0.0;
  2365. /* sanity check on the inputs */
  2366. if (rel_tol < 0.0 || abs_tol < 0.0 ) {
  2367. PyErr_SetString(PyExc_ValueError,
  2368. "tolerances must be non-negative");
  2369. return -1;
  2370. }
  2371. if ( a == b ) {
  2372. /* short circuit exact equality -- needed to catch two infinities of
  2373. the same sign. And perhaps speeds things up a bit sometimes.
  2374. */
  2375. return 1;
  2376. }
  2377. /* This catches the case of two infinities of opposite sign, or
  2378. one infinity and one finite number. Two infinities of opposite
  2379. sign would otherwise have an infinite relative tolerance.
  2380. Two infinities of the same sign are caught by the equality check
  2381. above.
  2382. */
  2383. if (Py_IS_INFINITY(a) || Py_IS_INFINITY(b)) {
  2384. return 0;
  2385. }
  2386. /* now do the regular computation
  2387. this is essentially the "weak" test from the Boost library
  2388. */
  2389. diff = fabs(b - a);
  2390. return (((diff <= fabs(rel_tol * b)) ||
  2391. (diff <= fabs(rel_tol * a))) ||
  2392. (diff <= abs_tol));
  2393. }
  2394. static inline int
  2395. _check_long_mult_overflow(long a, long b) {
  2396. /* From Python2's int_mul code:
  2397. Integer overflow checking for * is painful: Python tried a couple ways, but
  2398. they didn't work on all platforms, or failed in endcases (a product of
  2399. -sys.maxint-1 has been a particular pain).
  2400. Here's another way:
  2401. The native long product x*y is either exactly right or *way* off, being
  2402. just the last n bits of the true product, where n is the number of bits
  2403. in a long (the delivered product is the true product plus i*2**n for
  2404. some integer i).
  2405. The native double product (double)x * (double)y is subject to three
  2406. rounding errors: on a sizeof(long)==8 box, each cast to double can lose
  2407. info, and even on a sizeof(long)==4 box, the multiplication can lose info.
  2408. But, unlike the native long product, it's not in *range* trouble: even
  2409. if sizeof(long)==32 (256-bit longs), the product easily fits in the
  2410. dynamic range of a double. So the leading 50 (or so) bits of the double
  2411. product are correct.
  2412. We check these two ways against each other, and declare victory if they're
  2413. approximately the same. Else, because the native long product is the only
  2414. one that can lose catastrophic amounts of information, it's the native long
  2415. product that must have overflowed.
  2416. */
  2417. long longprod = (long)((unsigned long)a * b);
  2418. double doubleprod = (double)a * (double)b;
  2419. double doubled_longprod = (double)longprod;
  2420. if (doubled_longprod == doubleprod) {
  2421. return 0;
  2422. }
  2423. const double diff = doubled_longprod - doubleprod;
  2424. const double absdiff = diff >= 0.0 ? diff : -diff;
  2425. const double absprod = doubleprod >= 0.0 ? doubleprod : -doubleprod;
  2426. if (32.0 * absdiff <= absprod) {
  2427. return 0;
  2428. }
  2429. return 1;
  2430. }
  2431. /*[clinic input]
  2432. math.prod
  2433. iterable: object
  2434. /
  2435. *
  2436. start: object(c_default="NULL") = 1
  2437. Calculate the product of all the elements in the input iterable.
  2438. The default start value for the product is 1.
  2439. When the iterable is empty, return the start value. This function is
  2440. intended specifically for use with numeric values and may reject
  2441. non-numeric types.
  2442. [clinic start generated code]*/
  2443. static PyObject *
  2444. math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
  2445. /*[clinic end generated code: output=36153bedac74a198 input=4c5ab0682782ed54]*/
  2446. {
  2447. PyObject *result = start;
  2448. PyObject *temp, *item, *iter;
  2449. iter = PyObject_GetIter(iterable);
  2450. if (iter == NULL) {
  2451. return NULL;
  2452. }
  2453. if (result == NULL) {
  2454. result = PyLong_FromLong(1);
  2455. if (result == NULL) {
  2456. Py_DECREF(iter);
  2457. return NULL;
  2458. }
  2459. } else {
  2460. Py_INCREF(result);
  2461. }
  2462. #ifndef SLOW_PROD
  2463. /* Fast paths for integers keeping temporary products in C.
  2464. * Assumes all inputs are the same type.
  2465. * If the assumption fails, default to use PyObjects instead.
  2466. */
  2467. if (PyLong_CheckExact(result)) {
  2468. int overflow;
  2469. long i_result = PyLong_AsLongAndOverflow(result, &overflow);
  2470. /* If this already overflowed, don't even enter the loop. */
  2471. if (overflow == 0) {
  2472. Py_DECREF(result);
  2473. result = NULL;
  2474. }
  2475. /* Loop over all the items in the iterable until we finish, we overflow
  2476. * or we found a non integer element */
  2477. while(result == NULL) {
  2478. item = PyIter_Next(iter);
  2479. if (item == NULL) {
  2480. Py_DECREF(iter);
  2481. if (PyErr_Occurred()) {
  2482. return NULL;
  2483. }
  2484. return PyLong_FromLong(i_result);
  2485. }
  2486. if (PyLong_CheckExact(item)) {
  2487. long b = PyLong_AsLongAndOverflow(item, &overflow);
  2488. if (overflow == 0 && !_check_long_mult_overflow(i_result, b)) {
  2489. long x = i_result * b;
  2490. i_result = x;
  2491. Py_DECREF(item);
  2492. continue;
  2493. }
  2494. }
  2495. /* Either overflowed or is not an int.
  2496. * Restore real objects and process normally */
  2497. result = PyLong_FromLong(i_result);
  2498. if (result == NULL) {
  2499. Py_DECREF(item);
  2500. Py_DECREF(iter);
  2501. return NULL;
  2502. }
  2503. temp = PyNumber_Multiply(result, item);
  2504. Py_DECREF(result);
  2505. Py_DECREF(item);
  2506. result = temp;
  2507. if (result == NULL) {
  2508. Py_DECREF(iter);
  2509. return NULL;
  2510. }
  2511. }
  2512. }
  2513. /* Fast paths for floats keeping temporary products in C.
  2514. * Assumes all inputs are the same type.
  2515. * If the assumption fails, default to use PyObjects instead.
  2516. */
  2517. if (PyFloat_CheckExact(result)) {
  2518. double f_result = PyFloat_AS_DOUBLE(result);
  2519. Py_DECREF(result);
  2520. result = NULL;
  2521. while(result == NULL) {
  2522. item = PyIter_Next(iter);
  2523. if (item == NULL) {
  2524. Py_DECREF(iter);
  2525. if (PyErr_Occurred()) {
  2526. return NULL;
  2527. }
  2528. return PyFloat_FromDouble(f_result);
  2529. }
  2530. if (PyFloat_CheckExact(item)) {
  2531. f_result *= PyFloat_AS_DOUBLE(item);
  2532. Py_DECREF(item);
  2533. continue;
  2534. }
  2535. if (PyLong_CheckExact(item)) {
  2536. long value;
  2537. int overflow;
  2538. value = PyLong_AsLongAndOverflow(item, &overflow);
  2539. if (!overflow) {
  2540. f_result *= (double)value;
  2541. Py_DECREF(item);
  2542. continue;
  2543. }
  2544. }
  2545. result = PyFloat_FromDouble(f_result);
  2546. if (result == NULL) {
  2547. Py_DECREF(item);
  2548. Py_DECREF(iter);
  2549. return NULL;
  2550. }
  2551. temp = PyNumber_Multiply(result, item);
  2552. Py_DECREF(result);
  2553. Py_DECREF(item);
  2554. result = temp;
  2555. if (result == NULL) {
  2556. Py_DECREF(iter);
  2557. return NULL;
  2558. }
  2559. }
  2560. }
  2561. #endif
  2562. /* Consume rest of the iterable (if any) that could not be handled
  2563. * by specialized functions above.*/
  2564. for(;;) {
  2565. item = PyIter_Next(iter);
  2566. if (item == NULL) {
  2567. /* error, or end-of-sequence */
  2568. if (PyErr_Occurred()) {
  2569. Py_DECREF(result);
  2570. result = NULL;
  2571. }
  2572. break;
  2573. }
  2574. temp = PyNumber_Multiply(result, item);
  2575. Py_DECREF(result);
  2576. Py_DECREF(item);
  2577. result = temp;
  2578. if (result == NULL)
  2579. break;
  2580. }
  2581. Py_DECREF(iter);
  2582. return result;
  2583. }
  2584. /*[clinic input]
  2585. math.perm
  2586. n: object
  2587. k: object = None
  2588. /
  2589. Number of ways to choose k items from n items without repetition and with order.
  2590. Evaluates to n! / (n - k)! when k <= n and evaluates
  2591. to zero when k > n.
  2592. If k is not specified or is None, then k defaults to n
  2593. and the function returns n!.
  2594. Raises TypeError if either of the arguments are not integers.
  2595. Raises ValueError if either of the arguments are negative.
  2596. [clinic start generated code]*/
  2597. static PyObject *
  2598. math_perm_impl(PyObject *module, PyObject *n, PyObject *k)
  2599. /*[clinic end generated code: output=e021a25469653e23 input=5311c5a00f359b53]*/
  2600. {
  2601. PyObject *result = NULL, *factor = NULL;
  2602. int overflow, cmp;
  2603. long long i, factors;
  2604. if (k == Py_None) {
  2605. return math_factorial(module, n);
  2606. }
  2607. n = PyNumber_Index(n);
  2608. if (n == NULL) {
  2609. return NULL;
  2610. }
  2611. if (!PyLong_CheckExact(n)) {
  2612. Py_SETREF(n, _PyLong_Copy((PyLongObject *)n));
  2613. if (n == NULL) {
  2614. return NULL;
  2615. }
  2616. }
  2617. k = PyNumber_Index(k);
  2618. if (k == NULL) {
  2619. Py_DECREF(n);
  2620. return NULL;
  2621. }
  2622. if (!PyLong_CheckExact(k)) {
  2623. Py_SETREF(k, _PyLong_Copy((PyLongObject *)k));
  2624. if (k == NULL) {
  2625. Py_DECREF(n);
  2626. return NULL;
  2627. }
  2628. }
  2629. if (Py_SIZE(n) < 0) {
  2630. PyErr_SetString(PyExc_ValueError,
  2631. "n must be a non-negative integer");
  2632. goto error;
  2633. }
  2634. if (Py_SIZE(k) < 0) {
  2635. PyErr_SetString(PyExc_ValueError,
  2636. "k must be a non-negative integer");
  2637. goto error;
  2638. }
  2639. cmp = PyObject_RichCompareBool(n, k, Py_LT);
  2640. if (cmp != 0) {
  2641. if (cmp > 0) {
  2642. result = PyLong_FromLong(0);
  2643. goto done;
  2644. }
  2645. goto error;
  2646. }
  2647. factors = PyLong_AsLongLongAndOverflow(k, &overflow);
  2648. if (overflow > 0) {
  2649. PyErr_Format(PyExc_OverflowError,
  2650. "k must not exceed %lld",
  2651. LLONG_MAX);
  2652. goto error;
  2653. }
  2654. else if (factors == -1) {
  2655. /* k is nonnegative, so a return value of -1 can only indicate error */
  2656. goto error;
  2657. }
  2658. if (factors == 0) {
  2659. result = PyLong_FromLong(1);
  2660. goto done;
  2661. }
  2662. result = n;
  2663. Py_INCREF(result);
  2664. if (factors == 1) {
  2665. goto done;
  2666. }
  2667. factor = n;
  2668. Py_INCREF(factor);
  2669. for (i = 1; i < factors; ++i) {
  2670. Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_One));
  2671. if (factor == NULL) {
  2672. goto error;
  2673. }
  2674. Py_SETREF(result, PyNumber_Multiply(result, factor));
  2675. if (result == NULL) {
  2676. goto error;
  2677. }
  2678. }
  2679. Py_DECREF(factor);
  2680. done:
  2681. Py_DECREF(n);
  2682. Py_DECREF(k);
  2683. return result;
  2684. error:
  2685. Py_XDECREF(factor);
  2686. Py_XDECREF(result);
  2687. Py_DECREF(n);
  2688. Py_DECREF(k);
  2689. return NULL;
  2690. }
  2691. /*[clinic input]
  2692. math.comb
  2693. n: object
  2694. k: object
  2695. /
  2696. Number of ways to choose k items from n items without repetition and without order.
  2697. Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates
  2698. to zero when k > n.
  2699. Also called the binomial coefficient because it is equivalent
  2700. to the coefficient of k-th term in polynomial expansion of the
  2701. expression (1 + x)**n.
  2702. Raises TypeError if either of the arguments are not integers.
  2703. Raises ValueError if either of the arguments are negative.
  2704. [clinic start generated code]*/
  2705. static PyObject *
  2706. math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
  2707. /*[clinic end generated code: output=bd2cec8d854f3493 input=9a05315af2518709]*/
  2708. {
  2709. PyObject *result = NULL, *factor = NULL, *temp;
  2710. int overflow, cmp;
  2711. long long i, factors;
  2712. n = PyNumber_Index(n);
  2713. if (n == NULL) {
  2714. return NULL;
  2715. }
  2716. if (!PyLong_CheckExact(n)) {
  2717. Py_SETREF(n, _PyLong_Copy((PyLongObject *)n));
  2718. if (n == NULL) {
  2719. return NULL;
  2720. }
  2721. }
  2722. k = PyNumber_Index(k);
  2723. if (k == NULL) {
  2724. Py_DECREF(n);
  2725. return NULL;
  2726. }
  2727. if (!PyLong_CheckExact(k)) {
  2728. Py_SETREF(k, _PyLong_Copy((PyLongObject *)k));
  2729. if (k == NULL) {
  2730. Py_DECREF(n);
  2731. return NULL;
  2732. }
  2733. }
  2734. if (Py_SIZE(n) < 0) {
  2735. PyErr_SetString(PyExc_ValueError,
  2736. "n must be a non-negative integer");
  2737. goto error;
  2738. }
  2739. if (Py_SIZE(k) < 0) {
  2740. PyErr_SetString(PyExc_ValueError,
  2741. "k must be a non-negative integer");
  2742. goto error;
  2743. }
  2744. /* k = min(k, n - k) */
  2745. temp = PyNumber_Subtract(n, k);
  2746. if (temp == NULL) {
  2747. goto error;
  2748. }
  2749. if (Py_SIZE(temp) < 0) {
  2750. Py_DECREF(temp);
  2751. result = PyLong_FromLong(0);
  2752. goto done;
  2753. }
  2754. cmp = PyObject_RichCompareBool(temp, k, Py_LT);
  2755. if (cmp > 0) {
  2756. Py_SETREF(k, temp);
  2757. }
  2758. else {
  2759. Py_DECREF(temp);
  2760. if (cmp < 0) {
  2761. goto error;
  2762. }
  2763. }
  2764. factors = PyLong_AsLongLongAndOverflow(k, &overflow);
  2765. if (overflow > 0) {
  2766. PyErr_Format(PyExc_OverflowError,
  2767. "min(n - k, k) must not exceed %lld",
  2768. LLONG_MAX);
  2769. goto error;
  2770. }
  2771. if (factors == -1) {
  2772. /* k is nonnegative, so a return value of -1 can only indicate error */
  2773. goto error;
  2774. }
  2775. if (factors == 0) {
  2776. result = PyLong_FromLong(1);
  2777. goto done;
  2778. }
  2779. result = n;
  2780. Py_INCREF(result);
  2781. if (factors == 1) {
  2782. goto done;
  2783. }
  2784. factor = n;
  2785. Py_INCREF(factor);
  2786. for (i = 1; i < factors; ++i) {
  2787. Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_One));
  2788. if (factor == NULL) {
  2789. goto error;
  2790. }
  2791. Py_SETREF(result, PyNumber_Multiply(result, factor));
  2792. if (result == NULL) {
  2793. goto error;
  2794. }
  2795. temp = PyLong_FromUnsignedLongLong((unsigned long long)i + 1);
  2796. if (temp == NULL) {
  2797. goto error;
  2798. }
  2799. Py_SETREF(result, PyNumber_FloorDivide(result, temp));
  2800. Py_DECREF(temp);
  2801. if (result == NULL) {
  2802. goto error;
  2803. }
  2804. }
  2805. Py_DECREF(factor);
  2806. done:
  2807. Py_DECREF(n);
  2808. Py_DECREF(k);
  2809. return result;
  2810. error:
  2811. Py_XDECREF(factor);
  2812. Py_XDECREF(result);
  2813. Py_DECREF(n);
  2814. Py_DECREF(k);
  2815. return NULL;
  2816. }
  2817. static PyMethodDef math_methods[] = {
  2818. {"acos", math_acos, METH_O, math_acos_doc},
  2819. {"acosh", math_acosh, METH_O, math_acosh_doc},
  2820. {"asin", math_asin, METH_O, math_asin_doc},
  2821. {"asinh", math_asinh, METH_O, math_asinh_doc},
  2822. {"atan", math_atan, METH_O, math_atan_doc},
  2823. {"atan2", (PyCFunction)(void(*)(void))math_atan2, METH_FASTCALL, math_atan2_doc},
  2824. {"atanh", math_atanh, METH_O, math_atanh_doc},
  2825. MATH_CEIL_METHODDEF
  2826. {"copysign", (PyCFunction)(void(*)(void))math_copysign, METH_FASTCALL, math_copysign_doc},
  2827. {"cos", math_cos, METH_O, math_cos_doc},
  2828. {"cosh", math_cosh, METH_O, math_cosh_doc},
  2829. MATH_DEGREES_METHODDEF
  2830. MATH_DIST_METHODDEF
  2831. {"erf", math_erf, METH_O, math_erf_doc},
  2832. {"erfc", math_erfc, METH_O, math_erfc_doc},
  2833. {"exp", math_exp, METH_O, math_exp_doc},
  2834. {"expm1", math_expm1, METH_O, math_expm1_doc},
  2835. {"fabs", math_fabs, METH_O, math_fabs_doc},
  2836. MATH_FACTORIAL_METHODDEF
  2837. MATH_FLOOR_METHODDEF
  2838. MATH_FMOD_METHODDEF
  2839. MATH_FREXP_METHODDEF
  2840. MATH_FSUM_METHODDEF
  2841. {"gamma", math_gamma, METH_O, math_gamma_doc},
  2842. MATH_GCD_METHODDEF
  2843. {"hypot", (PyCFunction)(void(*)(void))math_hypot, METH_FASTCALL, math_hypot_doc},
  2844. MATH_ISCLOSE_METHODDEF
  2845. MATH_ISFINITE_METHODDEF
  2846. MATH_ISINF_METHODDEF
  2847. MATH_ISNAN_METHODDEF
  2848. MATH_ISQRT_METHODDEF
  2849. MATH_LDEXP_METHODDEF
  2850. {"lgamma", math_lgamma, METH_O, math_lgamma_doc},
  2851. MATH_LOG_METHODDEF
  2852. {"log1p", math_log1p, METH_O, math_log1p_doc},
  2853. MATH_LOG10_METHODDEF
  2854. MATH_LOG2_METHODDEF
  2855. MATH_MODF_METHODDEF
  2856. MATH_POW_METHODDEF
  2857. MATH_RADIANS_METHODDEF
  2858. {"remainder", (PyCFunction)(void(*)(void))math_remainder, METH_FASTCALL, math_remainder_doc},
  2859. {"sin", math_sin, METH_O, math_sin_doc},
  2860. {"sinh", math_sinh, METH_O, math_sinh_doc},
  2861. {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
  2862. {"tan", math_tan, METH_O, math_tan_doc},
  2863. {"tanh", math_tanh, METH_O, math_tanh_doc},
  2864. MATH_TRUNC_METHODDEF
  2865. MATH_PROD_METHODDEF
  2866. MATH_PERM_METHODDEF
  2867. MATH_COMB_METHODDEF
  2868. {NULL, NULL} /* sentinel */
  2869. };
  2870. PyDoc_STRVAR(module_doc,
  2871. "This module provides access to the mathematical functions\n"
  2872. "defined by the C standard.");
  2873. static struct PyModuleDef mathmodule = {
  2874. PyModuleDef_HEAD_INIT,
  2875. "math",
  2876. module_doc,
  2877. -1,
  2878. math_methods,
  2879. NULL,
  2880. NULL,
  2881. NULL,
  2882. NULL
  2883. };
  2884. PyMODINIT_FUNC
  2885. PyInit_math(void)
  2886. {
  2887. PyObject *m;
  2888. m = PyModule_Create(&mathmodule);
  2889. if (m == NULL)
  2890. goto finally;
  2891. PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI));
  2892. PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
  2893. PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */
  2894. PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf()));
  2895. #if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
  2896. PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan()));
  2897. #endif
  2898. finally:
  2899. return m;
  2900. }