[pypy-commit] pypy numpypy-complex2: start to test, implement exp2; need to fix rcomplex for corner cases

mattip noreply at buildbot.pypy.org
Fri Sep 14 15:18:43 CEST 2012


Author: mattip <matti.picus at gmail.com>
Branch: numpypy-complex2
Changeset: r57347:cce47db56c99
Date: 2012-09-14 14:09 +0300
http://bitbucket.org/pypy/pypy/changeset/cce47db56c99/

Log:	start to test, implement exp2; need to fix rcomplex for corner cases

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
@@ -463,7 +463,7 @@
 
     def test_exp2(self):
         import math, cmath
-        from _numpypy import array, exp2
+        from _numpypy import array, exp2, complex128, complex64
         inf = float('inf')
         ninf = -float('inf')
         nan = float('nan')
@@ -481,22 +481,41 @@
         assert exp2(3) == 8
         assert math.isnan(exp2(nan))
 
-        a = array([cmpl(-5., 0), cmpl(-5., -5.), cmpl(-5., 5.),
-                   cmpl(0., -5.), cmpl(0., 0.), cmpl(0., 5.),
-                   cmpl(-0., -5.), cmpl(-0., 0.), cmpl(-0., 5.),
-                   cmpl(-0., -0.), cmpl(inf, 0.), cmpl(inf, 5.),
-                   cmpl(inf, -0.), cmpl(ninf, 0.), cmpl(ninf, 5.),
-                   cmpl(ninf, -0.), cmpl(ninf, inf), cmpl(inf, inf),
-                   cmpl(ninf, ninf), cmpl(5., inf), cmpl(5., ninf),
-                   cmpl(nan, 5.), cmpl(5., nan), cmpl(nan, nan),
-                 ])
-        b = exp2(a)
-        for i in range(len(a)):
-            try:
-                res = cmath.pow(2,a[i])
-            except OverflowError:
-                res = cmpl(inf, nan)
-            assert b[i] == res
+        for c,rel_err in ((complex128, 5e-323), (complex64, 1e-7)):
+            a = array([cmpl(-5., 0), cmpl(-5., -5.), cmpl(-5., 5.),
+                       cmpl(0., -5.), cmpl(0., 0.), cmpl(0., 5.),
+                       cmpl(-0., -5.), cmpl(-0., 0.), cmpl(-0., 5.),
+                       cmpl(-0., -0.), cmpl(inf, 0.), cmpl(inf, 5.),
+                       cmpl(inf, -0.), cmpl(ninf, 0.), cmpl(ninf, 5.),
+                       cmpl(ninf, -0.), cmpl(ninf, inf), cmpl(inf, inf),
+                       cmpl(ninf, ninf), cmpl(5., inf), cmpl(5., ninf),
+                       cmpl(nan, 5.), cmpl(5., nan), cmpl(nan, nan),
+                     ], dtype=c)
+            b = exp2(a)
+            got_err = False
+            for i in range(len(a)):
+                try:
+                    res = 2 ** a[i]
+                    if a[i].imag == 0. and math.copysign(1., a[i].imag)<0:
+                        res = cmpl(res.real, -0.)
+                    elif a[i].imag == 0.:
+                        res = cmpl(res.real, 0.)
+                except OverflowError:
+                    res = cmpl(inf, nan)
+                msg = 'result of 2**%r(%r) got %r expected %r\n ' % \
+                            (c,a[i], b[i], res)
+                try:        
+                    t1 = float(res.real)        
+                    t2 = float(b[i].real)        
+                    self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
+                    t1 = float(res.imag)        
+                    t2 = float(b[i].imag)        
+                    self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
+                except AssertionError as e:
+                    print e.message
+                    got_err = True
+        if got_err:
+            raise AssertionError('Errors were printed to stdout')
 
     def test_expm1(self):
         import math
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -1260,9 +1260,11 @@
     @complex_unary_op
     def exp2(self, v):
         try:
-            return rcomplex.c_pow((2,0), v2)
+            return rcomplex.c_pow((2,0), v)
         except OverflowError:
             return rfloat.INFINITY, rfloat.NAN
+        except ValueError:
+            return rfloat.NAN, rfloat.NAN
 
     @complex_unary_op
     def expm1(self, v):


More information about the pypy-commit mailing list