This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: sqrt(-1) doesn't raise ValueError on OS X
Type: behavior Stage:
Components: Extension Modules Versions: Python 2.6
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: david.kwast, mark.dickinson
Priority: normal Keywords: patch

Created on 2008-01-21 02:51 by mark.dickinson, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
sqrt_minus_one.patch mark.dickinson, 2008-01-21 02:51
Messages (4)
msg61373 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-01-21 02:51
On OS X 10.4, and probably other versions of OS X too, calls to math.log and math.sqrt that 
should raise ValueError instead return a NaN:

Python 2.6a0 (trunk:60144, Jan 20 2008, 21:43:56) 
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import sqrt, log 
>>> sqrt(-1)
>>> log(-1)

The problem is that OS X doesn't set errno in these cases.  Attached is a quick fix for this.  
Note that there's already a test for this (test_exceptions in, but this test is 
only run in verbose mode.

See also issue #871657.
msg62863 - (view) Author: David Kwast (david.kwast) Date: 2008-02-24 01:56
On OSX 10.5 this behavior is confirmed (Python2.5 and 2.6a). This fix 
appears to be a good fix for python 2.6a.

(Python 2.6a with patch applied)
Python 2.6a0 (trunk:60977M, Feb 23 2008, 19:24:52) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import log,sqrt
>>> sqrt(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> log(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

david-kwasts-macbook:python-trunk davidkwast$ ./python 
testAcos (__main__.MathTests) ... ok
testAsin (__main__.MathTests) ... ok
testAtan (__main__.MathTests) ... ok
testAtan2 (__main__.MathTests) ... ok
testCeil (__main__.MathTests) ... ok
testConstants (__main__.MathTests) ... ok
testCopysign (__main__.MathTests) ... ok
testCos (__main__.MathTests) ... ok
testCosh (__main__.MathTests) ... ok
testDegrees (__main__.MathTests) ... ok
testExp (__main__.MathTests) ... ok
testFabs (__main__.MathTests) ... ok
testFloor (__main__.MathTests) ... ok
testFmod (__main__.MathTests) ... ok
testFrexp (__main__.MathTests) ... ok
testHypot (__main__.MathTests) ... ok
testIsinf (__main__.MathTests) ... ok
testIsnan (__main__.MathTests) ... ok
testLdexp (__main__.MathTests) ... ok
testLog (__main__.MathTests) ... ok
testLog10 (__main__.MathTests) ... ok
testModf (__main__.MathTests) ... ok
testPow (__main__.MathTests) ... ok
testRadians (__main__.MathTests) ... ok
testSin (__main__.MathTests) ... ok
testSinh (__main__.MathTests) ... ok
testSqrt (__main__.MathTests) ... ok
testTan (__main__.MathTests) ... ok
testTanh (__main__.MathTests) ... ok
test_exceptions (__main__.MathTests) ... ok
test_trunc (__main__.MathTests) ... ok

Ran 31 tests in 0.011s

msg62865 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-02-24 02:02
Thanks for the confirmation, David!

The work in the trunk-math branch should also fix this, in a rather 
better fashion (i.e., without per-platform hacks).  This issue should probably be revisited after it's decided whether to merge trunk-math 
into the trunk before 2.6.
msg65668 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-04-22 02:13
This is now fixed in the trunk, as a result of the trunk-math merge.
Closing as 'out of date'.
Date User Action Args
2022-04-11 14:56:30adminsetgithub: 46187
2008-04-22 02:13:09mark.dickinsonsetstatus: open -> closed
resolution: out of date
messages: + msg65668
2008-02-24 02:02:43mark.dickinsonsetmessages: + msg62865
2008-02-24 01:56:59david.kwastsetnosy: + david.kwast
messages: + msg62863
versions: - Python 2.5, Python 3.0
2008-01-21 02:51:27mark.dickinsoncreate