mirror of
https://github.com/python/cpython.git
synced 2025-11-10 10:32:04 +00:00
svn+ssh://pythondev@svn.python.org/python/trunk
................
r62425 | andrew.kuchling | 2008-04-21 03:45:57 +0200 (Mon, 21 Apr 2008) | 1 line
Comment typo
................
r62426 | mark.dickinson | 2008-04-21 03:55:50 +0200 (Mon, 21 Apr 2008) | 2 lines
Silence 'r may be used uninitialized' compiler warning.
................
r62427 | andrew.kuchling | 2008-04-21 04:08:00 +0200 (Mon, 21 Apr 2008) | 1 line
Markup fix
................
r62428 | andrew.kuchling | 2008-04-21 04:08:13 +0200 (Mon, 21 Apr 2008) | 1 line
Wording changes
................
r62429 | andrew.kuchling | 2008-04-21 04:14:24 +0200 (Mon, 21 Apr 2008) | 1 line
Add various items
................
r62434 | thomas.heller | 2008-04-21 15:46:55 +0200 (Mon, 21 Apr 2008) | 1 line
Fix typo.
................
r62435 | david.goodger | 2008-04-21 16:40:22 +0200 (Mon, 21 Apr 2008) | 1 line
corrections ("reStructuredText" is one word)
................
r62436 | david.goodger | 2008-04-21 16:43:33 +0200 (Mon, 21 Apr 2008) | 1 line
capitalization
................
r62441 | gregory.p.smith | 2008-04-21 19:46:40 +0200 (Mon, 21 Apr 2008) | 2 lines
explicitly flush after the ... since there wasn't a newline
................
r62444 | jeroen.ruigrok | 2008-04-21 22:15:39 +0200 (Mon, 21 Apr 2008) | 2 lines
Windows x64 also falls under VER_PLATFORM_WIN32_NT.
................
r62446 | gregory.p.smith | 2008-04-21 23:31:08 +0200 (Mon, 21 Apr 2008) | 3 lines
If sys.stdin is not a tty, fall back to default_getpass after printing
a warning instead of failing with a termios.error.
................
r62447 | mark.dickinson | 2008-04-22 00:32:24 +0200 (Tue, 22 Apr 2008) | 8 lines
test_math and test_cmath are failing on the FreeBSD 6.2 trunk buildbot,
apparently because tanh(-0.) loses the sign of zero on that platform.
If true, this is a bug in FreeBSD.
Added a configure test to verify this. I still need to figure out
how best to deal with this failure.
................
r62448 | amaury.forgeotdarc | 2008-04-22 00:35:30 +0200 (Tue, 22 Apr 2008) | 7 lines
Issue 2665: On Windows, sys.stderr does not contain a valid file when running without a console.
It seems to work, but will fail at the first flush.
This causes IDLE to crash when too many warnings are printed.
Will backport.
................
r62450 | benjamin.peterson | 2008-04-22 00:57:00 +0200 (Tue, 22 Apr 2008) | 2 lines
Fix Sphinx warnings
................
r62451 | mark.dickinson | 2008-04-22 02:54:27 +0200 (Tue, 22 Apr 2008) | 3 lines
Make configure test for tanh(-0.) == -0. committed in r62447 actually
work. (The test wasn't properly linked with libm. Sigh.)
................
r62452 | benjamin.peterson | 2008-04-22 04:16:03 +0200 (Tue, 22 Apr 2008) | 2 lines
Various io doc updates
................
r62453 | neal.norwitz | 2008-04-22 07:07:47 +0200 (Tue, 22 Apr 2008) | 1 line
Add Thomas Lee
................
r62454 | gregory.p.smith | 2008-04-22 10:08:41 +0200 (Tue, 22 Apr 2008) | 8 lines
Major improvements:
* Default to using /dev/tty for the password prompt and input before
falling back to sys.stdin and sys.stderr.
* Use sys.stderr instead of sys.stdout.
* print the 'password may be echoed' warning to stream used to display
the prompt rather than always sys.stderr.
* warn() with GetPassWarning when input may be echoed.
................
r62455 | gregory.p.smith | 2008-04-22 10:11:33 +0200 (Tue, 22 Apr 2008) | 2 lines
update the getpass entry
................
r62463 | amaury.forgeotdarc | 2008-04-22 23:14:41 +0200 (Tue, 22 Apr 2008) | 5 lines
Issue #2670: urllib2.build_opener() failed when two handlers
derive the same default base class.
Will backport.
................
r62465 | skip.montanaro | 2008-04-23 00:45:09 +0200 (Wed, 23 Apr 2008) | 3 lines
Factor in documentation changes from issue 1753732.
................
r62466 | gregory.p.smith | 2008-04-23 03:06:42 +0200 (Wed, 23 Apr 2008) | 2 lines
syntax fixup
................
r62469 | benjamin.peterson | 2008-04-23 22:38:06 +0200 (Wed, 23 Apr 2008) | 2 lines
#2673 Fix example typo in optparse docs
................
r62474 | martin.v.loewis | 2008-04-24 11:50:50 +0200 (Thu, 24 Apr 2008) | 2 lines
Add Guilherme Polo.
................
r62476 | martin.v.loewis | 2008-04-24 15:16:36 +0200 (Thu, 24 Apr 2008) | 3 lines
Remove Py_Refcnt, Py_Type, Py_Size, as they were added only
for backwards compatibility, yet 2.5 did not have them at all.
................
r62477 | martin.v.loewis | 2008-04-24 15:17:24 +0200 (Thu, 24 Apr 2008) | 2 lines
Fix typo.
................
r62478 | martin.v.loewis | 2008-04-24 15:18:03 +0200 (Thu, 24 Apr 2008) | 2 lines
Add Jesus Cea.
................
r62480 | amaury.forgeotdarc | 2008-04-24 20:07:05 +0200 (Thu, 24 Apr 2008) | 4 lines
Issue2681: the literal 0o8 was wrongly accepted, and evaluated as float(0.0).
This happened only when 8 is the first digit.
Credits go to Lukas Meuser.
................
r62485 | amaury.forgeotdarc | 2008-04-24 22:10:26 +0200 (Thu, 24 Apr 2008) | 5 lines
Disable gc when running test_trace, or we may record the __del__ of collected objects.
See http://mail.python.org/pipermail/python-checkins/2008-April/068633.html
the extra events perfectly match several calls to socket._fileobject.__del__()
................
r62492 | neal.norwitz | 2008-04-25 05:40:17 +0200 (Fri, 25 Apr 2008) | 1 line
Fix typo (now -> no)
................
r62497 | armin.rigo | 2008-04-25 11:35:18 +0200 (Fri, 25 Apr 2008) | 2 lines
A new crasher.
................
r62498 | thomas.heller | 2008-04-25 17:44:16 +0200 (Fri, 25 Apr 2008) | 1 line
Add from_buffer and from_buffer_copy class methods to ctypes types.
................
r62500 | mark.dickinson | 2008-04-25 18:59:09 +0200 (Fri, 25 Apr 2008) | 3 lines
Issue 2635: fix bug in the fix_sentence_endings option to textwrap.fill.
................
r62507 | benjamin.peterson | 2008-04-25 23:43:56 +0200 (Fri, 25 Apr 2008) | 2 lines
Allow test_import to work when it is invoked directly
................
r62513 | georg.brandl | 2008-04-26 20:31:07 +0200 (Sat, 26 Apr 2008) | 2 lines
#2691: document PyLong (s)size_t APIs, patch by Alexander Belopolsky.
................
r62514 | georg.brandl | 2008-04-26 20:32:17 +0200 (Sat, 26 Apr 2008) | 2 lines
Add missing return type to dealloc.
................
r62516 | alexandre.vassalotti | 2008-04-27 02:52:24 +0200 (Sun, 27 Apr 2008) | 2 lines
Fixed URL of PEP 205 in weakref's module docstring.
................
r62521 | georg.brandl | 2008-04-27 11:39:59 +0200 (Sun, 27 Apr 2008) | 2 lines
#2677: add note that not all functions may accept keyword args.
................
r62531 | georg.brandl | 2008-04-27 19:38:55 +0200 (Sun, 27 Apr 2008) | 2 lines
Use correct XHTML tags.
................
r62535 | benjamin.peterson | 2008-04-27 20:14:39 +0200 (Sun, 27 Apr 2008) | 2 lines
#2700 Document PyNumber_ToBase
................
r62545 | skip.montanaro | 2008-04-27 22:53:57 +0200 (Sun, 27 Apr 2008) | 1 line
minor wording changes, rewrap a few lines
................
r62546 | kurt.kaiser | 2008-04-27 23:07:41 +0200 (Sun, 27 Apr 2008) | 7 lines
Home / Control-A toggles between left margin and end of leading white
space. Patch 1196903 Jeff Shute.
M idlelib/PyShell.py
M idlelib/EditorWindow.py
M idlelib/NEWS.txt
................
r62548 | kurt.kaiser | 2008-04-27 23:38:05 +0200 (Sun, 27 Apr 2008) | 2 lines
Improved AutoCompleteWindow logic. Patch 2062 Tal Einat.
................
r62549 | kurt.kaiser | 2008-04-27 23:52:19 +0200 (Sun, 27 Apr 2008) | 4 lines
Autocompletion of filenames now support alternate separators, e.g. the
'/' char on Windows. Patch 2061 Tal Einat.
................
r62550 | skip.montanaro | 2008-04-28 00:49:56 +0200 (Mon, 28 Apr 2008) | 6 lines
A few small changes:
* The only exception we should catch when trying to import cStringIO is an
ImportError.
* Delete the function signatures embedded in the mk*temp docstrings.
* The tempdir global variable was initialized twice.
................
r62551 | skip.montanaro | 2008-04-28 00:52:02 +0200 (Mon, 28 Apr 2008) | 4 lines
Wrap some long paragraphs and include the default values for optional
function parameters.
................
r62553 | skip.montanaro | 2008-04-28 04:57:23 +0200 (Mon, 28 Apr 2008) | 7 lines
Minor cleanups:
* Avoid creating unused local variables where we can. Where we can't prefix
the unused variables with '_'.
* Avoid shadowing builtins where it won't change the external interface of a
function.
* Use None as default path arg to readmodule and readmodule_ex.
................
r62554 | skip.montanaro | 2008-04-28 04:59:45 +0200 (Mon, 28 Apr 2008) | 6 lines
Correct documentation to match implementation: "Class" instead of
"class_descriptor", "Function" instead of "function_descriptor". Note
default path value for readmodule*. Wrap some long paragraphs. Don't
mention 'inpackage' which isn't part of the public API.
................
r62555 | brett.cannon | 2008-04-28 05:23:50 +0200 (Mon, 28 Apr 2008) | 5 lines
Fix a bug introduced by the warnings rewrite where tracebacks were being
improperly indented.
Closes issue #2699.
................
r62556 | skip.montanaro | 2008-04-28 05:25:37 +0200 (Mon, 28 Apr 2008) | 2 lines
Wrap some long lines.
................
r62557 | skip.montanaro | 2008-04-28 05:27:53 +0200 (Mon, 28 Apr 2008) | 6 lines
Get rid of _test(), _main(), _debug() and _check(). Tests are no longer
needed (better set available in Lib/test/test_robotparser.py). Clean up a
few PEP 8 nits (compound statements on a single line, whitespace around
operators).
................
r62558 | brett.cannon | 2008-04-28 06:50:06 +0200 (Mon, 28 Apr 2008) | 3 lines
Rename the test_traceback_print() function to traceback_print() to prevent
test_capi from automatically calling the function.
................
r62559 | georg.brandl | 2008-04-28 07:16:30 +0200 (Mon, 28 Apr 2008) | 2 lines
Fix markup.
................
r62569 | amaury.forgeotdarc | 2008-04-28 23:07:06 +0200 (Mon, 28 Apr 2008) | 5 lines
test_sundry performs minimal tests (a simple import...) on modules that are not tested otherwise.
Some of them now have tests and can be removed.
Only 70 to go...
................
r62574 | andrew.kuchling | 2008-04-29 04:03:54 +0200 (Tue, 29 Apr 2008) | 1 line
Strip down SSL docs; I'm not managing to get test programs working, so I'll just give a minimal description
................
r62577 | martin.v.loewis | 2008-04-29 08:10:53 +0200 (Tue, 29 Apr 2008) | 2 lines
Add Rodrigo and Heiko.
................
r62593 | nick.coghlan | 2008-04-30 16:23:36 +0200 (Wed, 30 Apr 2008) | 1 line
Update command line usage documentation to reflect 2.6 changes (also includes some minor cleanups). Addresses TODO list issue 2258
................
r62595 | andrew.kuchling | 2008-04-30 18:19:55 +0200 (Wed, 30 Apr 2008) | 1 line
Typo fix
................
r62604 | benjamin.peterson | 2008-04-30 23:03:58 +0200 (Wed, 30 Apr 2008) | 2 lines
make test_support's captured_output a bit more robust when exceptions happen
................
r62605 | georg.brandl | 2008-04-30 23:08:42 +0200 (Wed, 30 Apr 2008) | 2 lines
#1748: use functools.wraps instead of rolling own metadata update.
................
r62606 | benjamin.peterson | 2008-04-30 23:25:55 +0200 (Wed, 30 Apr 2008) | 2 lines
Remove some from __future__ import with_statements
................
r62608 | benjamin.peterson | 2008-05-01 00:03:36 +0200 (Thu, 01 May 2008) | 2 lines
Fix typo in whatsnew
................
r62616 | georg.brandl | 2008-05-01 20:24:32 +0200 (Thu, 01 May 2008) | 2 lines
Fix synopsis.
................
r62626 | brett.cannon | 2008-05-02 04:25:09 +0200 (Fri, 02 May 2008) | 6 lines
Fix a backwards-compatibility mistake where a new optional argument for
warnings.showwarning() was being used. This broke pre-existing replacements for
the function since they didn't support the extra argument.
Closes issue 2705.
................
r62627 | gregory.p.smith | 2008-05-02 09:26:52 +0200 (Fri, 02 May 2008) | 20 lines
This should fix issue2632. A long description of the two competing
problems is in the bug report (one old, one recently introduced trying
to fix the old one). In short:
buffer data during socket._fileobject.read() and readlines() within a
cStringIO object instead of a [] of str()s returned from the recv()
call.
This prevents excessive memory use due to the size parameter being
passed to recv() being grossly larger than the actual size of the data
returned *and* prevents excessive cpu usage due to looping in python
calling recv() with a very tiny size value if min() is used as the
previous memory-use bug "fix" did.
It also documents what the socket._fileobject._rbufsize member is
actually used for.
This is a candidate for back porting to 2.5.
................
r62636 | mark.hammond | 2008-05-02 14:48:15 +0200 (Fri, 02 May 2008) | 2 lines
#2581: Vista UAC/elevation support for bdist_wininst
................
r62638 | facundo.batista | 2008-05-02 19:39:00 +0200 (Fri, 02 May 2008) | 3 lines
Fixed some test structures. Thanks Mark Dickinson.
................
r62644 | ronald.oussoren | 2008-05-02 21:45:11 +0200 (Fri, 02 May 2008) | 7 lines
Fix for issue #2573: Can't change the framework name on OS X builds
This introduces a new configure option: --with-framework-name=NAME
(defaulting to 'Python'). This allows you to install several copies
of the Python framework with different names (such as a normal build
and a debug build).
................
r62645 | ronald.oussoren | 2008-05-02 21:58:56 +0200 (Fri, 02 May 2008) | 2 lines
Finish fix for issue2573, previous patch was incomplete.
................
r62647 | martin.v.loewis | 2008-05-02 23:30:20 +0200 (Fri, 02 May 2008) | 13 lines
Merged revisions 62263-62646 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r62470 | david.wolever | 2008-04-24 02:11:07 +0200 (Do, 24 Apr 2008) | 3 lines
Fixed up and applied the patch for #2431 -- speeding up 2to3 with a lookup table.
........
r62646 | martin.v.loewis | 2008-05-02 23:29:27 +0200 (Fr, 02 Mai 2008) | 2 lines
Fix whitespace.
........
................
r62648 | ronald.oussoren | 2008-05-02 23:42:35 +0200 (Fri, 02 May 2008) | 4 lines
Fix for #1905: PythonLauncher not working correctly on OSX 10.5/Leopard
This fixes both Python Launchar and the terminalcommand module.
................
r62651 | ronald.oussoren | 2008-05-02 23:54:56 +0200 (Fri, 02 May 2008) | 2 lines
Fix for issue #2520 (cannot import macerrors)
................
r62652 | benjamin.peterson | 2008-05-03 00:12:58 +0200 (Sat, 03 May 2008) | 2 lines
capitalization nit for reStructuredText
................
r62653 | brett.cannon | 2008-05-03 03:02:41 +0200 (Sat, 03 May 2008) | 2 lines
Fix some indentation errors.
................
r62656 | brett.cannon | 2008-05-03 05:19:39 +0200 (Sat, 03 May 2008) | 6 lines
Fix the C implementation of 'warnings' to infer the filename of the module that
raised an exception properly when __file__ is not set, __name__ == '__main__',
and sys.argv[0] is a false value.
Closes issue2743.
................
r62661 | amaury.forgeotdarc | 2008-05-03 14:21:13 +0200 (Sat, 03 May 2008) | 8 lines
In test_io, StatefulIncrementalDecoderTest was not part of the test suite.
And of course, the test failed:
a bytearray was used without reason in io.TextIOWrapper.tell().
The difference is that iterating over bytes (i.e. str in python2.6) returns 1-char bytes,
whereas bytearrays yield integers.
This code should still work with python3.0
................
r62663 | benjamin.peterson | 2008-05-03 17:56:42 +0200 (Sat, 03 May 2008) | 2 lines
The compiling struct is now passed around to all AST helpers (see issue 2720)
................
r62680 | benjamin.peterson | 2008-05-03 23:35:18 +0200 (Sat, 03 May 2008) | 2 lines
Moved testing of builtin types out of test_builtin and into type specific modules
................
r62686 | mark.dickinson | 2008-05-04 04:25:46 +0200 (Sun, 04 May 2008) | 4 lines
Make sure that Context traps and flags dictionaries have values 0 and 1
(as documented) rather than True and False.
................
r62687 | benjamin.peterson | 2008-05-04 05:05:49 +0200 (Sun, 04 May 2008) | 2 lines
Fix typo in whatsnew
................
r62696 | georg.brandl | 2008-05-04 11:15:04 +0200 (Sun, 04 May 2008) | 2 lines
#2752: wrong meaning of '' for socket host.
................
r62699 | christian.heimes | 2008-05-04 13:50:53 +0200 (Sun, 04 May 2008) | 1 line
Added note that Python requires at least Win2k SP4
................
r62700 | gerhard.haering | 2008-05-04 14:59:57 +0200 (Sun, 04 May 2008) | 3 lines
SQLite requires 64-bit integers in order to build. So the whole HAVE_LONG_LONG
#ifdefing was useless.
................
r62701 | gerhard.haering | 2008-05-04 15:15:12 +0200 (Sun, 04 May 2008) | 3 lines
Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
sqlite3.Row type is now correctly hashable.
................
r62702 | gerhard.haering | 2008-05-04 15:42:44 +0200 (Sun, 04 May 2008) | 5 lines
Implemented feature request 2157: Converter names are cut off at '('
characters. This avoids the common case of something like 'NUMBER(10)' not
being parsed as 'NUMBER', like expected. Also corrected the docs about
converter names being case-sensitive. They aren't any longer.
................
r62703 | georg.brandl | 2008-05-04 17:45:05 +0200 (Sun, 04 May 2008) | 2 lines
#2757: Remove spare newline.
................
r62711 | benjamin.peterson | 2008-05-04 21:10:02 +0200 (Sun, 04 May 2008) | 2 lines
Fix typo in bugs.rst
................
830 lines
24 KiB
C
830 lines
24 KiB
C
/* Math module -- standard C math library functions, pi and e */
|
|
|
|
/* Here are some comments from Tim Peters, extracted from the
|
|
discussion attached to http://bugs.python.org/issue1640. They
|
|
describe the general aims of the math module with respect to
|
|
special values, IEEE-754 floating-point exceptions, and Python
|
|
exceptions.
|
|
|
|
These are the "spirit of 754" rules:
|
|
|
|
1. If the mathematical result is a real number, but of magnitude too
|
|
large to approximate by a machine float, overflow is signaled and the
|
|
result is an infinity (with the appropriate sign).
|
|
|
|
2. If the mathematical result is a real number, but of magnitude too
|
|
small to approximate by a machine float, underflow is signaled and the
|
|
result is a zero (with the appropriate sign).
|
|
|
|
3. At a singularity (a value x such that the limit of f(y) as y
|
|
approaches x exists and is an infinity), "divide by zero" is signaled
|
|
and the result is an infinity (with the appropriate sign). This is
|
|
complicated a little by that the left-side and right-side limits may
|
|
not be the same; e.g., 1/x approaches +inf or -inf as x approaches 0
|
|
from the positive or negative directions. In that specific case, the
|
|
sign of the zero determines the result of 1/0.
|
|
|
|
4. At a point where a function has no defined result in the extended
|
|
reals (i.e., the reals plus an infinity or two), invalid operation is
|
|
signaled and a NaN is returned.
|
|
|
|
And these are what Python has historically /tried/ to do (but not
|
|
always successfully, as platform libm behavior varies a lot):
|
|
|
|
For #1, raise OverflowError.
|
|
|
|
For #2, return a zero (with the appropriate sign if that happens by
|
|
accident ;-)).
|
|
|
|
For #3 and #4, raise ValueError. It may have made sense to raise
|
|
Python's ZeroDivisionError in #3, but historically that's only been
|
|
raised for division by zero and mod by zero.
|
|
|
|
*/
|
|
|
|
/*
|
|
In general, on an IEEE-754 platform the aim is to follow the C99
|
|
standard, including Annex 'F', whenever possible. Where the
|
|
standard recommends raising the 'divide-by-zero' or 'invalid'
|
|
floating-point exceptions, Python should raise a ValueError. Where
|
|
the standard recommends raising 'overflow', Python should raise an
|
|
OverflowError. In all other circumstances a value should be
|
|
returned.
|
|
*/
|
|
|
|
#include "Python.h"
|
|
#include "longintrepr.h" /* just for SHIFT */
|
|
|
|
#ifdef _OSF_SOURCE
|
|
/* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */
|
|
extern double copysign(double, double);
|
|
#endif
|
|
|
|
/* Call is_error when errno != 0, and where x is the result libm
|
|
* returned. is_error will usually set up an exception and return
|
|
* true (1), but may return false (0) without setting up an exception.
|
|
*/
|
|
static int
|
|
is_error(double x)
|
|
{
|
|
int result = 1; /* presumption of guilt */
|
|
assert(errno); /* non-zero errno is a precondition for calling */
|
|
if (errno == EDOM)
|
|
PyErr_SetString(PyExc_ValueError, "math domain error");
|
|
|
|
else if (errno == ERANGE) {
|
|
/* ANSI C generally requires libm functions to set ERANGE
|
|
* on overflow, but also generally *allows* them to set
|
|
* ERANGE on underflow too. There's no consistency about
|
|
* the latter across platforms.
|
|
* Alas, C99 never requires that errno be set.
|
|
* Here we suppress the underflow errors (libm functions
|
|
* should return a zero on underflow, and +- HUGE_VAL on
|
|
* overflow, so testing the result for zero suffices to
|
|
* distinguish the cases).
|
|
*/
|
|
if (x)
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
"math range error");
|
|
else
|
|
result = 0;
|
|
}
|
|
else
|
|
/* Unexpected math error */
|
|
PyErr_SetFromErrno(PyExc_ValueError);
|
|
return result;
|
|
}
|
|
|
|
/*
|
|
wrapper for atan2 that deals directly with special cases before
|
|
delegating to the platform libm for the remaining cases. This
|
|
is necessary to get consistent behaviour across platforms.
|
|
Windows, FreeBSD and alpha Tru64 are amongst platforms that don't
|
|
always follow C99.
|
|
*/
|
|
|
|
static double
|
|
m_atan2(double y, double x)
|
|
{
|
|
if (Py_IS_NAN(x) || Py_IS_NAN(y))
|
|
return Py_NAN;
|
|
if (Py_IS_INFINITY(y)) {
|
|
if (Py_IS_INFINITY(x)) {
|
|
if (copysign(1., x) == 1.)
|
|
/* atan2(+-inf, +inf) == +-pi/4 */
|
|
return copysign(0.25*Py_MATH_PI, y);
|
|
else
|
|
/* atan2(+-inf, -inf) == +-pi*3/4 */
|
|
return copysign(0.75*Py_MATH_PI, y);
|
|
}
|
|
/* atan2(+-inf, x) == +-pi/2 for finite x */
|
|
return copysign(0.5*Py_MATH_PI, y);
|
|
}
|
|
if (Py_IS_INFINITY(x) || y == 0.) {
|
|
if (copysign(1., x) == 1.)
|
|
/* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
|
|
return copysign(0., y);
|
|
else
|
|
/* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
|
|
return copysign(Py_MATH_PI, y);
|
|
}
|
|
return atan2(y, x);
|
|
}
|
|
|
|
/*
|
|
math_1 is used to wrap a libm function f that takes a double
|
|
arguments and returns a double.
|
|
|
|
The error reporting follows these rules, which are designed to do
|
|
the right thing on C89/C99 platforms and IEEE 754/non IEEE 754
|
|
platforms.
|
|
|
|
- a NaN result from non-NaN inputs causes ValueError to be raised
|
|
- an infinite result from finite inputs causes OverflowError to be
|
|
raised if can_overflow is 1, or raises ValueError if can_overflow
|
|
is 0.
|
|
- if the result is finite and errno == EDOM then ValueError is
|
|
raised
|
|
- if the result is finite and nonzero and errno == ERANGE then
|
|
OverflowError is raised
|
|
|
|
The last rule is used to catch overflow on platforms which follow
|
|
C89 but for which HUGE_VAL is not an infinity.
|
|
|
|
For the majority of one-argument functions these rules are enough
|
|
to ensure that Python's functions behave as specified in 'Annex F'
|
|
of the C99 standard, with the 'invalid' and 'divide-by-zero'
|
|
floating-point exceptions mapping to Python's ValueError and the
|
|
'overflow' floating-point exception mapping to OverflowError.
|
|
math_1 only works for functions that don't have singularities *and*
|
|
the possibility of overflow; fortunately, that covers everything we
|
|
care about right now.
|
|
*/
|
|
|
|
static PyObject *
|
|
math_1_to_whatever(PyObject *arg, double (*func) (double),
|
|
PyObject *(*from_double_func) (double),
|
|
int can_overflow)
|
|
{
|
|
double x, r;
|
|
char err_message[150];
|
|
x = PyFloat_AsDouble(arg);
|
|
if (x == -1.0 && PyErr_Occurred())
|
|
return NULL;
|
|
errno = 0;
|
|
PyFPE_START_PROTECT("in math_1", return 0);
|
|
r = (*func)(x);
|
|
PyFPE_END_PROTECT(r);
|
|
if (Py_IS_NAN(r) && !Py_IS_NAN(x)) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"math domain error (invalid argument)");
|
|
return NULL;
|
|
}
|
|
if (Py_IS_INFINITY(r) && Py_IS_FINITE(x)) {
|
|
if (can_overflow)
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
"math range error (overflow)");
|
|
else {
|
|
/* temporary code to include the inputs
|
|
and outputs to func in the error
|
|
message */
|
|
sprintf(err_message,
|
|
"math domain error (singularity) "
|
|
"%.17g -> %.17g",
|
|
x, r);
|
|
PyErr_SetString(PyExc_ValueError, err_message);
|
|
}
|
|
return NULL;
|
|
}
|
|
if (Py_IS_FINITE(r) && errno && is_error(r))
|
|
/* this branch unnecessary on most platforms */
|
|
return NULL;
|
|
|
|
return (*from_double_func)(r);
|
|
}
|
|
|
|
/*
|
|
math_2 is used to wrap a libm function f that takes two double
|
|
arguments and returns a double.
|
|
|
|
The error reporting follows these rules, which are designed to do
|
|
the right thing on C89/C99 platforms and IEEE 754/non IEEE 754
|
|
platforms.
|
|
|
|
- a NaN result from non-NaN inputs causes ValueError to be raised
|
|
- an infinite result from finite inputs causes OverflowError to be
|
|
raised.
|
|
- if the result is finite and errno == EDOM then ValueError is
|
|
raised
|
|
- if the result is finite and nonzero and errno == ERANGE then
|
|
OverflowError is raised
|
|
|
|
The last rule is used to catch overflow on platforms which follow
|
|
C89 but for which HUGE_VAL is not an infinity.
|
|
|
|
For most two-argument functions (copysign, fmod, hypot, atan2)
|
|
these rules are enough to ensure that Python's functions behave as
|
|
specified in 'Annex F' of the C99 standard, with the 'invalid' and
|
|
'divide-by-zero' floating-point exceptions mapping to Python's
|
|
ValueError and the 'overflow' floating-point exception mapping to
|
|
OverflowError.
|
|
*/
|
|
|
|
static PyObject *
|
|
math_1(PyObject *arg, double (*func) (double), int can_overflow)
|
|
{
|
|
return math_1_to_whatever(arg, func, PyFloat_FromDouble, can_overflow);
|
|
}
|
|
|
|
static PyObject *
|
|
math_1_to_int(PyObject *arg, double (*func) (double), int can_overflow)
|
|
{
|
|
return math_1_to_whatever(arg, func, PyLong_FromDouble, can_overflow);
|
|
}
|
|
|
|
static PyObject *
|
|
math_2(PyObject *args, double (*func) (double, double), char *funcname)
|
|
{
|
|
PyObject *ox, *oy;
|
|
double x, y, r;
|
|
if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy))
|
|
return NULL;
|
|
x = PyFloat_AsDouble(ox);
|
|
y = PyFloat_AsDouble(oy);
|
|
if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
|
|
return NULL;
|
|
errno = 0;
|
|
PyFPE_START_PROTECT("in math_2", return 0);
|
|
r = (*func)(x, y);
|
|
PyFPE_END_PROTECT(r);
|
|
if (Py_IS_NAN(r)) {
|
|
if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
|
|
errno = EDOM;
|
|
else
|
|
errno = 0;
|
|
}
|
|
else if (Py_IS_INFINITY(r)) {
|
|
if (Py_IS_FINITE(x) && Py_IS_FINITE(y))
|
|
errno = ERANGE;
|
|
else
|
|
errno = 0;
|
|
}
|
|
if (errno && is_error(r))
|
|
return NULL;
|
|
else
|
|
return PyFloat_FromDouble(r);
|
|
}
|
|
|
|
#define FUNC1(funcname, func, can_overflow, docstring) \
|
|
static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
|
|
return math_1(args, func, can_overflow); \
|
|
}\
|
|
PyDoc_STRVAR(math_##funcname##_doc, docstring);
|
|
|
|
#define FUNC2(funcname, func, docstring) \
|
|
static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
|
|
return math_2(args, func, #funcname); \
|
|
}\
|
|
PyDoc_STRVAR(math_##funcname##_doc, docstring);
|
|
|
|
FUNC1(acos, acos, 0,
|
|
"acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
|
|
FUNC1(acosh, acosh, 0,
|
|
"acosh(x)\n\nReturn the hyperbolic arc cosine (measured in radians) of x.")
|
|
FUNC1(asin, asin, 0,
|
|
"asin(x)\n\nReturn the arc sine (measured in radians) of x.")
|
|
FUNC1(asinh, asinh, 0,
|
|
"asinh(x)\n\nReturn the hyperbolic arc sine (measured in radians) of x.")
|
|
FUNC1(atan, atan, 0,
|
|
"atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
|
|
FUNC2(atan2, m_atan2,
|
|
"atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
|
|
"Unlike atan(y/x), the signs of both x and y are considered.")
|
|
FUNC1(atanh, atanh, 0,
|
|
"atanh(x)\n\nReturn the hyperbolic arc tangent (measured in radians) of x.")
|
|
|
|
static PyObject * math_ceil(PyObject *self, PyObject *number) {
|
|
static PyObject *ceil_str = NULL;
|
|
PyObject *method;
|
|
|
|
if (ceil_str == NULL) {
|
|
ceil_str = PyUnicode_InternFromString("__ceil__");
|
|
if (ceil_str == NULL)
|
|
return NULL;
|
|
}
|
|
|
|
method = _PyType_Lookup(Py_TYPE(number), ceil_str);
|
|
if (method == NULL)
|
|
return math_1_to_int(number, ceil, 0);
|
|
else
|
|
return PyObject_CallFunction(method, "O", number);
|
|
}
|
|
|
|
PyDoc_STRVAR(math_ceil_doc,
|
|
"ceil(x)\n\nReturn the ceiling of x as an int.\n"
|
|
"This is the smallest integral value >= x.");
|
|
|
|
FUNC2(copysign, copysign,
|
|
"copysign(x,y)\n\nReturn x with the sign of y.")
|
|
FUNC1(cos, cos, 0,
|
|
"cos(x)\n\nReturn the cosine of x (measured in radians).")
|
|
FUNC1(cosh, cosh, 1,
|
|
"cosh(x)\n\nReturn the hyperbolic cosine of x.")
|
|
FUNC1(exp, exp, 1,
|
|
"exp(x)\n\nReturn e raised to the power of x.")
|
|
FUNC1(fabs, fabs, 0,
|
|
"fabs(x)\n\nReturn the absolute value of the float x.")
|
|
|
|
static PyObject * math_floor(PyObject *self, PyObject *number) {
|
|
static PyObject *floor_str = NULL;
|
|
PyObject *method;
|
|
|
|
if (floor_str == NULL) {
|
|
floor_str = PyUnicode_InternFromString("__floor__");
|
|
if (floor_str == NULL)
|
|
return NULL;
|
|
}
|
|
|
|
method = _PyType_Lookup(Py_TYPE(number), floor_str);
|
|
if (method == NULL)
|
|
return math_1_to_int(number, floor, 0);
|
|
else
|
|
return PyObject_CallFunction(method, "O", number);
|
|
}
|
|
|
|
PyDoc_STRVAR(math_floor_doc,
|
|
"floor(x)\n\nReturn the floor of x as an int.\n"
|
|
"This is the largest integral value <= x.");
|
|
|
|
FUNC1(log1p, log1p, 1,
|
|
"log1p(x)\n\nReturn the natural logarithm of 1+x (base e).\n\
|
|
The result is computed in a way which is accurate for x near zero.")
|
|
FUNC1(sin, sin, 0,
|
|
"sin(x)\n\nReturn the sine of x (measured in radians).")
|
|
FUNC1(sinh, sinh, 1,
|
|
"sinh(x)\n\nReturn the hyperbolic sine of x.")
|
|
FUNC1(sqrt, sqrt, 0,
|
|
"sqrt(x)\n\nReturn the square root of x.")
|
|
FUNC1(tan, tan, 0,
|
|
"tan(x)\n\nReturn the tangent of x (measured in radians).")
|
|
FUNC1(tanh, tanh, 0,
|
|
"tanh(x)\n\nReturn the hyperbolic tangent of x.")
|
|
|
|
static PyObject *
|
|
math_trunc(PyObject *self, PyObject *number)
|
|
{
|
|
static PyObject *trunc_str = NULL;
|
|
PyObject *trunc;
|
|
|
|
if (Py_TYPE(number)->tp_dict == NULL) {
|
|
if (PyType_Ready(Py_TYPE(number)) < 0)
|
|
return NULL;
|
|
}
|
|
|
|
if (trunc_str == NULL) {
|
|
trunc_str = PyUnicode_InternFromString("__trunc__");
|
|
if (trunc_str == NULL)
|
|
return NULL;
|
|
}
|
|
|
|
trunc = _PyType_Lookup(Py_TYPE(number), trunc_str);
|
|
if (trunc == NULL) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"type %.100s doesn't define __trunc__ method",
|
|
Py_TYPE(number)->tp_name);
|
|
return NULL;
|
|
}
|
|
return PyObject_CallFunctionObjArgs(trunc, number, NULL);
|
|
}
|
|
|
|
PyDoc_STRVAR(math_trunc_doc,
|
|
"trunc(x:Real) -> Integral\n"
|
|
"\n"
|
|
"Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.");
|
|
|
|
static PyObject *
|
|
math_frexp(PyObject *self, PyObject *arg)
|
|
{
|
|
int i;
|
|
double x = PyFloat_AsDouble(arg);
|
|
if (x == -1.0 && PyErr_Occurred())
|
|
return NULL;
|
|
/* deal with special cases directly, to sidestep platform
|
|
differences */
|
|
if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) {
|
|
i = 0;
|
|
}
|
|
else {
|
|
PyFPE_START_PROTECT("in math_frexp", return 0);
|
|
x = frexp(x, &i);
|
|
PyFPE_END_PROTECT(x);
|
|
}
|
|
return Py_BuildValue("(di)", x, i);
|
|
}
|
|
|
|
PyDoc_STRVAR(math_frexp_doc,
|
|
"frexp(x)\n"
|
|
"\n"
|
|
"Return the mantissa and exponent of x, as pair (m, e).\n"
|
|
"m is a float and e is an int, such that x = m * 2.**e.\n"
|
|
"If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.");
|
|
|
|
static PyObject *
|
|
math_ldexp(PyObject *self, PyObject *args)
|
|
{
|
|
double x, r;
|
|
int exp;
|
|
if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
|
|
return NULL;
|
|
errno = 0;
|
|
PyFPE_START_PROTECT("in math_ldexp", return 0)
|
|
r = ldexp(x, exp);
|
|
PyFPE_END_PROTECT(r)
|
|
if (Py_IS_FINITE(x) && Py_IS_INFINITY(r))
|
|
errno = ERANGE;
|
|
/* Windows MSVC8 sets errno = EDOM on ldexp(NaN, i);
|
|
we unset it to avoid raising a ValueError here. */
|
|
if (errno == EDOM)
|
|
errno = 0;
|
|
if (errno && is_error(r))
|
|
return NULL;
|
|
else
|
|
return PyFloat_FromDouble(r);
|
|
}
|
|
|
|
PyDoc_STRVAR(math_ldexp_doc,
|
|
"ldexp(x, i) -> x * (2**i)");
|
|
|
|
static PyObject *
|
|
math_modf(PyObject *self, PyObject *arg)
|
|
{
|
|
double y, x = PyFloat_AsDouble(arg);
|
|
if (x == -1.0 && PyErr_Occurred())
|
|
return NULL;
|
|
/* some platforms don't do the right thing for NaNs and
|
|
infinities, so we take care of special cases directly. */
|
|
if (!Py_IS_FINITE(x)) {
|
|
if (Py_IS_INFINITY(x))
|
|
return Py_BuildValue("(dd)", copysign(0., x), x);
|
|
else if (Py_IS_NAN(x))
|
|
return Py_BuildValue("(dd)", x, x);
|
|
}
|
|
|
|
errno = 0;
|
|
PyFPE_START_PROTECT("in math_modf", return 0);
|
|
x = modf(x, &y);
|
|
PyFPE_END_PROTECT(x);
|
|
return Py_BuildValue("(dd)", x, y);
|
|
}
|
|
|
|
PyDoc_STRVAR(math_modf_doc,
|
|
"modf(x)\n"
|
|
"\n"
|
|
"Return the fractional and integer parts of x. Both results carry the sign\n"
|
|
"of x. The integer part is returned as a real.");
|
|
|
|
/* A decent logarithm is easy to compute even for huge longs, but libm can't
|
|
do that by itself -- loghelper can. func is log or log10, and name is
|
|
"log" or "log10". Note that overflow isn't possible: a long can contain
|
|
no more than INT_MAX * SHIFT bits, so has value certainly less than
|
|
2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
|
|
small enough to fit in an IEEE single. log and log10 are even smaller.
|
|
*/
|
|
|
|
static PyObject*
|
|
loghelper(PyObject* arg, double (*func)(double), char *funcname)
|
|
{
|
|
/* If it is long, do it ourselves. */
|
|
if (PyLong_Check(arg)) {
|
|
double x;
|
|
int e;
|
|
x = _PyLong_AsScaledDouble(arg, &e);
|
|
if (x <= 0.0) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"math domain error");
|
|
return NULL;
|
|
}
|
|
/* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~=
|
|
log(x) + log(2) * e * PyLong_SHIFT.
|
|
CAUTION: e*PyLong_SHIFT may overflow using int arithmetic,
|
|
so force use of double. */
|
|
x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0);
|
|
return PyFloat_FromDouble(x);
|
|
}
|
|
|
|
/* Else let libm handle it by itself. */
|
|
return math_1(arg, func, 0);
|
|
}
|
|
|
|
static PyObject *
|
|
math_log(PyObject *self, PyObject *args)
|
|
{
|
|
PyObject *arg;
|
|
PyObject *base = NULL;
|
|
PyObject *num, *den;
|
|
PyObject *ans;
|
|
|
|
if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
|
|
return NULL;
|
|
|
|
num = loghelper(arg, log, "log");
|
|
if (num == NULL || base == NULL)
|
|
return num;
|
|
|
|
den = loghelper(base, log, "log");
|
|
if (den == NULL) {
|
|
Py_DECREF(num);
|
|
return NULL;
|
|
}
|
|
|
|
ans = PyNumber_TrueDivide(num, den);
|
|
Py_DECREF(num);
|
|
Py_DECREF(den);
|
|
return ans;
|
|
}
|
|
|
|
PyDoc_STRVAR(math_log_doc,
|
|
"log(x[, base]) -> the logarithm of x to the given base.\n\
|
|
If the base not specified, returns the natural logarithm (base e) of x.");
|
|
|
|
static PyObject *
|
|
math_log10(PyObject *self, PyObject *arg)
|
|
{
|
|
return loghelper(arg, log10, "log10");
|
|
}
|
|
|
|
PyDoc_STRVAR(math_log10_doc,
|
|
"log10(x) -> the base 10 logarithm of x.");
|
|
|
|
static PyObject *
|
|
math_fmod(PyObject *self, PyObject *args)
|
|
{
|
|
PyObject *ox, *oy;
|
|
double r, x, y;
|
|
if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy))
|
|
return NULL;
|
|
x = PyFloat_AsDouble(ox);
|
|
y = PyFloat_AsDouble(oy);
|
|
if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
|
|
return NULL;
|
|
/* fmod(x, +/-Inf) returns x for finite x. */
|
|
if (Py_IS_INFINITY(y) && Py_IS_FINITE(x))
|
|
return PyFloat_FromDouble(x);
|
|
errno = 0;
|
|
PyFPE_START_PROTECT("in math_fmod", return 0);
|
|
r = fmod(x, y);
|
|
PyFPE_END_PROTECT(r);
|
|
if (Py_IS_NAN(r)) {
|
|
if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
|
|
errno = EDOM;
|
|
else
|
|
errno = 0;
|
|
}
|
|
if (errno && is_error(r))
|
|
return NULL;
|
|
else
|
|
return PyFloat_FromDouble(r);
|
|
}
|
|
|
|
PyDoc_STRVAR(math_fmod_doc,
|
|
"fmod(x,y)\n\nReturn fmod(x, y), according to platform C."
|
|
" x % y may differ.");
|
|
|
|
static PyObject *
|
|
math_hypot(PyObject *self, PyObject *args)
|
|
{
|
|
PyObject *ox, *oy;
|
|
double r, x, y;
|
|
if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy))
|
|
return NULL;
|
|
x = PyFloat_AsDouble(ox);
|
|
y = PyFloat_AsDouble(oy);
|
|
if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
|
|
return NULL;
|
|
/* hypot(x, +/-Inf) returns Inf, even if x is a NaN. */
|
|
if (Py_IS_INFINITY(x))
|
|
return PyFloat_FromDouble(fabs(x));
|
|
if (Py_IS_INFINITY(y))
|
|
return PyFloat_FromDouble(fabs(y));
|
|
errno = 0;
|
|
PyFPE_START_PROTECT("in math_hypot", return 0);
|
|
r = hypot(x, y);
|
|
PyFPE_END_PROTECT(r);
|
|
if (Py_IS_NAN(r)) {
|
|
if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
|
|
errno = EDOM;
|
|
else
|
|
errno = 0;
|
|
}
|
|
else if (Py_IS_INFINITY(r)) {
|
|
if (Py_IS_FINITE(x) && Py_IS_FINITE(y))
|
|
errno = ERANGE;
|
|
else
|
|
errno = 0;
|
|
}
|
|
if (errno && is_error(r))
|
|
return NULL;
|
|
else
|
|
return PyFloat_FromDouble(r);
|
|
}
|
|
|
|
PyDoc_STRVAR(math_hypot_doc,
|
|
"hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).");
|
|
|
|
/* pow can't use math_2, but needs its own wrapper: the problem is
|
|
that an infinite result can arise either as a result of overflow
|
|
(in which case OverflowError should be raised) or as a result of
|
|
e.g. 0.**-5. (for which ValueError needs to be raised.)
|
|
*/
|
|
|
|
static PyObject *
|
|
math_pow(PyObject *self, PyObject *args)
|
|
{
|
|
PyObject *ox, *oy;
|
|
double r, x, y;
|
|
int odd_y;
|
|
|
|
if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy))
|
|
return NULL;
|
|
x = PyFloat_AsDouble(ox);
|
|
y = PyFloat_AsDouble(oy);
|
|
if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
|
|
return NULL;
|
|
|
|
/* deal directly with IEEE specials, to cope with problems on various
|
|
platforms whose semantics don't exactly match C99 */
|
|
r = 0.; /* silence compiler warning */
|
|
if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) {
|
|
errno = 0;
|
|
if (Py_IS_NAN(x))
|
|
r = y == 0. ? 1. : x; /* NaN**0 = 1 */
|
|
else if (Py_IS_NAN(y))
|
|
r = x == 1. ? 1. : y; /* 1**NaN = 1 */
|
|
else if (Py_IS_INFINITY(x)) {
|
|
odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0;
|
|
if (y > 0.)
|
|
r = odd_y ? x : fabs(x);
|
|
else if (y == 0.)
|
|
r = 1.;
|
|
else /* y < 0. */
|
|
r = odd_y ? copysign(0., x) : 0.;
|
|
}
|
|
else if (Py_IS_INFINITY(y)) {
|
|
if (fabs(x) == 1.0)
|
|
r = 1.;
|
|
else if (y > 0. && fabs(x) > 1.0)
|
|
r = y;
|
|
else if (y < 0. && fabs(x) < 1.0) {
|
|
r = -y; /* result is +inf */
|
|
if (x == 0.) /* 0**-inf: divide-by-zero */
|
|
errno = EDOM;
|
|
}
|
|
else
|
|
r = 0.;
|
|
}
|
|
}
|
|
else {
|
|
/* let libm handle finite**finite */
|
|
errno = 0;
|
|
PyFPE_START_PROTECT("in math_pow", return 0);
|
|
r = pow(x, y);
|
|
PyFPE_END_PROTECT(r);
|
|
/* a NaN result should arise only from (-ve)**(finite
|
|
non-integer); in this case we want to raise ValueError. */
|
|
if (!Py_IS_FINITE(r)) {
|
|
if (Py_IS_NAN(r)) {
|
|
errno = EDOM;
|
|
}
|
|
/*
|
|
an infinite result here arises either from:
|
|
(A) (+/-0.)**negative (-> divide-by-zero)
|
|
(B) overflow of x**y with x and y finite
|
|
*/
|
|
else if (Py_IS_INFINITY(r)) {
|
|
if (x == 0.)
|
|
errno = EDOM;
|
|
else
|
|
errno = ERANGE;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (errno && is_error(r))
|
|
return NULL;
|
|
else
|
|
return PyFloat_FromDouble(r);
|
|
}
|
|
|
|
PyDoc_STRVAR(math_pow_doc,
|
|
"pow(x,y)\n\nReturn x**y (x to the power of y).");
|
|
|
|
static const double degToRad = Py_MATH_PI / 180.0;
|
|
static const double radToDeg = 180.0 / Py_MATH_PI;
|
|
|
|
static PyObject *
|
|
math_degrees(PyObject *self, PyObject *arg)
|
|
{
|
|
double x = PyFloat_AsDouble(arg);
|
|
if (x == -1.0 && PyErr_Occurred())
|
|
return NULL;
|
|
return PyFloat_FromDouble(x * radToDeg);
|
|
}
|
|
|
|
PyDoc_STRVAR(math_degrees_doc,
|
|
"degrees(x) -> converts angle x from radians to degrees");
|
|
|
|
static PyObject *
|
|
math_radians(PyObject *self, PyObject *arg)
|
|
{
|
|
double x = PyFloat_AsDouble(arg);
|
|
if (x == -1.0 && PyErr_Occurred())
|
|
return NULL;
|
|
return PyFloat_FromDouble(x * degToRad);
|
|
}
|
|
|
|
PyDoc_STRVAR(math_radians_doc,
|
|
"radians(x) -> converts angle x from degrees to radians");
|
|
|
|
static PyObject *
|
|
math_isnan(PyObject *self, PyObject *arg)
|
|
{
|
|
double x = PyFloat_AsDouble(arg);
|
|
if (x == -1.0 && PyErr_Occurred())
|
|
return NULL;
|
|
return PyBool_FromLong((long)Py_IS_NAN(x));
|
|
}
|
|
|
|
PyDoc_STRVAR(math_isnan_doc,
|
|
"isnan(x) -> bool\n\
|
|
Checks if float x is not a number (NaN)");
|
|
|
|
static PyObject *
|
|
math_isinf(PyObject *self, PyObject *arg)
|
|
{
|
|
double x = PyFloat_AsDouble(arg);
|
|
if (x == -1.0 && PyErr_Occurred())
|
|
return NULL;
|
|
return PyBool_FromLong((long)Py_IS_INFINITY(x));
|
|
}
|
|
|
|
PyDoc_STRVAR(math_isinf_doc,
|
|
"isinf(x) -> bool\n\
|
|
Checks if float x is infinite (positive or negative)");
|
|
|
|
static PyMethodDef math_methods[] = {
|
|
{"acos", math_acos, METH_O, math_acos_doc},
|
|
{"acosh", math_acosh, METH_O, math_acosh_doc},
|
|
{"asin", math_asin, METH_O, math_asin_doc},
|
|
{"asinh", math_asinh, METH_O, math_asinh_doc},
|
|
{"atan", math_atan, METH_O, math_atan_doc},
|
|
{"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
|
|
{"atanh", math_atanh, METH_O, math_atanh_doc},
|
|
{"ceil", math_ceil, METH_O, math_ceil_doc},
|
|
{"copysign", math_copysign, METH_VARARGS, math_copysign_doc},
|
|
{"cos", math_cos, METH_O, math_cos_doc},
|
|
{"cosh", math_cosh, METH_O, math_cosh_doc},
|
|
{"degrees", math_degrees, METH_O, math_degrees_doc},
|
|
{"exp", math_exp, METH_O, math_exp_doc},
|
|
{"fabs", math_fabs, METH_O, math_fabs_doc},
|
|
{"floor", math_floor, METH_O, math_floor_doc},
|
|
{"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
|
|
{"frexp", math_frexp, METH_O, math_frexp_doc},
|
|
{"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
|
|
{"isinf", math_isinf, METH_O, math_isinf_doc},
|
|
{"isnan", math_isnan, METH_O, math_isnan_doc},
|
|
{"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
|
|
{"log", math_log, METH_VARARGS, math_log_doc},
|
|
{"log1p", math_log1p, METH_O, math_log1p_doc},
|
|
{"log10", math_log10, METH_O, math_log10_doc},
|
|
{"modf", math_modf, METH_O, math_modf_doc},
|
|
{"pow", math_pow, METH_VARARGS, math_pow_doc},
|
|
{"radians", math_radians, METH_O, math_radians_doc},
|
|
{"sin", math_sin, METH_O, math_sin_doc},
|
|
{"sinh", math_sinh, METH_O, math_sinh_doc},
|
|
{"sqrt", math_sqrt, METH_O, math_sqrt_doc},
|
|
{"tan", math_tan, METH_O, math_tan_doc},
|
|
{"tanh", math_tanh, METH_O, math_tanh_doc},
|
|
{"trunc", math_trunc, METH_O, math_trunc_doc},
|
|
{NULL, NULL} /* sentinel */
|
|
};
|
|
|
|
|
|
PyDoc_STRVAR(module_doc,
|
|
"This module is always available. It provides access to the\n"
|
|
"mathematical functions defined by the C standard.");
|
|
|
|
PyMODINIT_FUNC
|
|
initmath(void)
|
|
{
|
|
PyObject *m;
|
|
|
|
m = Py_InitModule3("math", math_methods, module_doc);
|
|
if (m == NULL)
|
|
goto finally;
|
|
|
|
PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI));
|
|
PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
|
|
|
|
finally:
|
|
return;
|
|
}
|