classification
Title: cmath is numerically unsound
Type: behavior Stage:
Components: Extension Modules Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alanmcintyre, christian.heimes, gvanrossum, inducer, jcea, loewis, marketdickinson (7)
Priority: normal Keywords: patch

Created on 2007-11-03 18:58 by inducer, last changed 2008-10-13 11:57 by jcea.

Files
File name Uploaded Description Edit Remove
cmath_py.py marketdickinson, 2007-11-04 15:16
cmath.patch marketdickinson, 2007-11-26 02:18
cmath3.patch marketdickinson, 2008-01-05 00:25 Updated patch for cmath module, with docs and tests
Messages (16)
msg57088 - (view) Author: Andreas Kloeckner (inducer) Date: 2007-11-03 18:58
This here basically says it all:

>>> import cmath;[cmath.asinh(i*1e-17).real for i in range(0,20)]
[4.4408920985006257e-16, 4.4408920985006257e-16, 4.4408920985006257e-16,
4.4408920985006257e-16, 4.4408920985006257e-16, 4.4408920985006257e-16,
4.4408920985006257e-16, 4.4408920985006257e-16, 4.4408920985006257e-16,
4.4408920985006257e-16, 4.4408920985006257e-16, 4.4408920985006257e-16,
4.4408920985006257e-16, 4.4408920985006257e-16, 4.4408920985006257e-16,
4.4408920985006257e-16, 4.4408920985006257e-16, 4.4408920985006257e-16,
4.4408920985006257e-16, 4.4408920985006257e-16]

The boost.math toolkit at [2] is an implementation that does better in
the above (real-only) aspect.
[2] http://freespace.virgin.net/boost.regex/toolkit/html/index.html

Tim Peters remarks in [1] that basically all of cmath is unsound.
http://mail.python.org/pipermail/python-bugs-list/2001-February/004126.html

I just wanted to make sure that this issue remains on the radar.
msg57089 - (view) Author: Martin v. Löwis (loewis) Date: 2007-11-03 19:17
Can you propose a patch?
msg57090 - (view) Author: Andreas Kloeckner (inducer) Date: 2007-11-03 19:42
On Samstag 03 November 2007, Martin v. Löwis wrote:
> Martin v. Löwis added the comment:
>
> Can you propose a patch?

Other than point at how boost.math does things, I don't have the time to work 
on this right now, sorry.

Andreas
msg57092 - (view) Author: Alan McIntyre (alanmcintyre) Date: 2007-11-04 05:46
I have to review a few complex math topics for some of my current course
work, so I wouldn't mind taking a look into this.  I can't promise I'll
have the time required to make all of cmath correct (assuming it's as
unsound as claimed), but I'll do what I can.
msg57098 - (view) Author: Mark Dickinson (marketdickinson) Date: 2007-11-04 15:16
I took a look at this a while back, and got as far as writing a pure 
Python drop-in replacement for cmath, based on Kahan's "branch cuts for 
elementary functions" paper.  This fixes a variety of problems in cmath, 
including the buggy branch cuts for asinh.  File attached, in case it's 
of any use.

As Tim Peters pointed out, the real problem here is a lack of decent 
unit tests for these functions.  I have tests for the file above, but 
they assume IEEE754 floats, which is probably not an acceptable 
assumption in general.
msg57104 - (view) Author: Martin v. Löwis (loewis) Date: 2007-11-04 18:38
It would be ok if a test is only run on a system with IEEE floats, and
skipped elsewhere. For all practical purposes, Python assumes that all
systems have IEEE float.
msg57835 - (view) Author: Mark Dickinson (marketdickinson) Date: 2007-11-26 02:18
Here is (quite a large) patch, cmath.patch, that fixes a variety of 
problems in the current cmath module.  A summary of the changes:

* sqrt, log, acos, acosh, asin, asinh, atan, atanh no longer produce 
overflow errors for very large inputs

* exp, cos, sin, tan, cosh, sinh, tanh produce valid answers in some cases
where they incorrectly overflowed before.

* sqrt and log are more accurate for tiny numbers

* numeric problems in some functions (especially asinh and asin) should
have been fixed

* the branch cuts for asinh have been moved to the standard positions

* the direction of continuity for the branch cuts of tan, tanh have been 
fixed

* on systems with full hardware and system support for signed zeros (most 
modern systems), functions with a branch cut are continuous on both sides 
of the branch cut.  (As recommended by the C99 standard, amongst others.)

The patch includes documentation updates, but there are still no tests.  
(My current tests make heavy use of the MPFR library, and assume IEEE-754 
format doubles, so need to be seriously reworked.)  The tests are on my to-
do list, but I'm unlikely to find time for them before the new year.  In 
the meantime, I'd very much appreciate any feedback on this patch, if 
anyone has the time/energy/inclination to look at it.  (Andreas:  are you 
in a position to give this a test-run?)
msg59268 - (view) Author: Mark Dickinson (marketdickinson) Date: 2008-01-05 00:25
Here's an updated patch for cmath, complete with tests and documentation changes.
There's an extra file, Lib/test/cmath.ctest, containing test values for the various functions;  
these test values were obtained using MPFR and interval arithmetic, and (modulo bugs) should 
all be correctly rounded.

Any feedback would be much appreciated.
msg59557 - (view) Author: Mark Dickinson (marketdickinson) Date: 2008-01-08 20:55
A note to any potential reviewers for this patch (cmath3.patch):  I'm especially 
looking for non-mathematical feedback here, so please don't be put off by the 
mathematics.  Some questions:

- there's a new file cmath.ctest containing testcases;  should this be put 
directly into Lib/test (where it is right now), or is there a better place.  Is 
the name of this file reasonable?

- is the new stuff in pyport.h (checks for _copysign and copysign, and automatic 
renaming of _copysign to copysign) okay?  Should it be in cmathmodule.c instead?

- is the code in test_cmath that looks for the testcase file okay?

And it would be really great if someone with access to a Windows box could just 
verify that the tests pass with this patch applied;  I've only tested it on OS X 
and Linux.
msg59568 - (view) Author: Christian Heimes (christian.heimes) Date: 2008-01-08 22:42
* I've added a configure test for copysign a while ago. I'm using this
constructor for Windows compatibility in mathmodule.c:

#ifdef MS_WINDOWS
#  define copysign _copysign
#  define HAVE_COPYSIGN 1
#endif
#ifdef HAVE_COPYSIGN
#endif

I know no platform besides Windows with a _copysign method. You don't
have to add tests for _copysign for Windows. You may want to add the
code to PC/pyconfig.h and define HAVE_COPYSIGN there.

* test_file = testdir + os.sep + 'cmath.ctest
  make that:
  test_file = os.path.join(testdir, 'cmath.ctest')

* Windows doesn't have log1p, the hyperbolic area functions (asinh) and
some other functions, too. IIRC Windows does only implement the Bessel
functions of 1st and 2nd kind but non of the other C99 math functions.
msg59570 - (view) Author: Mark Dickinson (marketdickinson) Date: 2008-01-08 22:54
Okay:  would it be okay for me to move the #ifdef MS_WINDOWS sequence 
somewhere where it can be used by both mathmodule.c and cmathmodule.c, 
then?

There are replacements for log1p and asinh in the patch, so it shouldn't 
matter than they're missing on Windows---it might mean that the accuracy 
of some of the functions is slightly reduced on Windows, though.
msg59572 - (view) Author: Christian Heimes (christian.heimes) Date: 2008-01-08 23:16
It may even be a good idea to move asinh, copysign and log1p to a new
file Python/pymath.c and add compensatory implementations of acosh,
atanh and expm1, too. The math related code in pyport.h could then be
moved to Include/pymath.h. In the past some people have asked me to
implement the reverse hyperbolic functions for the math module.

This is beyond a simple fix for the cmath module and should be discussed
on the python-dev list first.
msg59637 - (view) Author: Christian Heimes (christian.heimes) Date: 2008-01-10 00:20
Guido, how do you like the idea of Include/pymath.h and Python/pymath.c
containing supplementary mathematical functions and mathematical
constants? Mark's patch for cmath is rather large, can it still be
applied to 2.5?
msg59638 - (view) Author: Guido van Rossum (gvanrossum) Date: 2008-01-10 00:29
Are you crazy?  Adding new features to 2.5?  No way!
msg61499 - (view) Author: Christian Heimes (christian.heimes) Date: 2008-01-22 11:16
See #1640 and svn+ssh://pythondev@svn.python.org/python/branches/trunk-math
msg65703 - (view) Author: Mark Dickinson (marketdickinson) Date: 2008-04-23 17:08
Substantial fixes for the cmath module went into the trunk and the py3k 
branches as part of the merge of the trunk-math branch.  These fixes 
address the asinh problems in this issue, amongst other things.  See 
r62380 (trunk) and r62384 (py3k).
History
Date User Action Args
2008-10-13 11:57:09jceasetnosy: + jcea
2008-04-23 17:08:48marketdickinsonsetstatus: open -> closed
resolution: fixed
messages: + msg65703
2008-01-22 11:17:00christian.heimessetpriority: normal
keywords: + patch
messages: + msg61499
components: + Extension Modules, - Library (Lib)
versions: + Python 3.0, - Python 2.5
2008-01-10 00:29:05gvanrossumsetmessages: + msg59638
2008-01-10 00:20:00christian.heimessetnosy: + gvanrossum
messages: + msg59637
versions: + Python 2.6
2008-01-08 23:16:54christian.heimessetmessages: + msg59572
2008-01-08 22:54:57marketdickinsonsetmessages: + msg59570
2008-01-08 22:42:17christian.heimessetnosy: + christian.heimes
messages: + msg59568
2008-01-08 20:55:48marketdickinsonsetmessages: + msg59557
2008-01-05 00:25:28marketdickinsonsetfiles: + cmath3.patch
messages: + msg59268
2007-11-26 02:18:52marketdickinsonsetfiles: + cmath.patch
messages: + msg57835
2007-11-04 18:38:37loewissetmessages: + msg57104
2007-11-04 15:16:22marketdickinsonsetfiles: + cmath_py.py
nosy: + marketdickinson
messages: + msg57098
2007-11-04 05:46:53alanmcintyresetnosy: + alanmcintyre
messages: + msg57092
2007-11-03 19:42:43inducersetmessages: + msg57090
2007-11-03 19:17:37loewissetnosy: + loewis
messages: + msg57089
2007-11-03 18:58:58inducercreate