[pypy-commit] pypy default: fix for invalid inputs in numpy.sqrt

alex_gaynor noreply at buildbot.pypy.org
Mon Nov 28 11:10:38 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r49885:25f6f5031705
Date: 2011-11-28 05:10 -0500
http://bitbucket.org/pypy/pypy/changeset/25f6f5031705/

Log:	fix for invalid inputs in numpy.sqrt

diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -295,7 +295,10 @@
         return math.atanh(v)
     @unaryop
     def sqrt(self, v):
-        return math.sqrt(v)
+        try:
+            return math.sqrt(v)
+        except ValueError:
+            return rfloat.NAN
 
 class IntegerArithmeticDtype(ArithmeticTypeMixin):
     _mixin_ = True
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -320,10 +320,15 @@
             assert arctanh(v) == math.copysign(float("inf"), v)
 
     def test_sqrt(self):
+        import math
         from numpypy import sqrt
-        import math
-        assert (sqrt([1, 2, 3]) == [math.sqrt(1), math.sqrt(2),
-                                    math.sqrt(3)]).all()
+
+        nan, inf = float("nan"), float("inf")
+        data = [1, 2, 3, inf]
+        results = [math.sqrt(1), math.sqrt(2), math.sqrt(3), inf]
+        assert (sqrt(data) == results).all()
+        assert math.isnan(sqrt(-1))
+        assert math.isnan(sqrt(nan))
 
     def test_reduce_errors(self):
         from numpypy import sin, add


More information about the pypy-commit mailing list