[pypy-svn] pypy cmath: (lac, arigo)

arigo commits-noreply at bitbucket.org
Mon Jan 17 19:11:17 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: cmath
Changeset: r40810:f6f6b04eef6b
Date: 2011-01-17 19:10 +0100
http://bitbucket.org/pypy/pypy/changeset/f6f6b04eef6b/

Log:	(lac, arigo)

	phase().

diff --git a/pypy/module/cmath/__init__.py b/pypy/module/cmath/__init__.py
--- a/pypy/module/cmath/__init__.py
+++ b/pypy/module/cmath/__init__.py
@@ -25,7 +25,8 @@
     'polar': ("polar(z) -> r: float, phi: float\n"
               "Convert a complex from rectangular coordinates "
               "to polar coordinates. r is\n"
-              "the distance from 0 and phi the phase angle.")
+              "the distance from 0 and phi the phase angle."),
+    'phase': "Return argument, also known as the phase angle, of a complex.",
     }
 
 

diff --git a/pypy/module/cmath/test/test_cmath.py b/pypy/module/cmath/test/test_cmath.py
--- a/pypy/module/cmath/test/test_cmath.py
+++ b/pypy/module/cmath/test/test_cmath.py
@@ -61,6 +61,11 @@
         assert r == 2
         assert abs(phi - cmath.pi/2) < 1e-10
 
+    def test_phase(self):
+        import cmath
+        phi = cmath.phase(2j)
+        assert abs(phi - cmath.pi/2) < 1e-10
+
 
 def parse_testfile(fname):
     """Parse a file with test values

diff --git a/pypy/module/cmath/interp_cmath.py b/pypy/module/cmath/interp_cmath.py
--- a/pypy/module/cmath/interp_cmath.py
+++ b/pypy/module/cmath/interp_cmath.py
@@ -29,14 +29,14 @@
 @specialize.arg(0)
 def call_c_func(c_func, x, y):
     try:
-        resx, resy = c_func(x, y)
+        result = c_func(x, y)
     except ValueError:
         raise OperationError(space.w_ValueError,
                              space.wrap("math domain error"))
     except OverflowError:
         raise OperationError(space.w_OverflowError,
                              space.wrap("math range error"))
-    return resx, resy
+    return result
 
 
 def unaryfn(c_func):
@@ -499,7 +499,7 @@
 wrapped_rect.func_doc = names_and_docstrings['rect']
 
 
-def c_atan2(x, y):
+def c_phase(x, y):
     # Windows screws up atan2 for inf and nan, and alpha Tru64 5.1 doesn't
     # follow C99 for atan2(0., 0.).
     if isnan(x) or isnan(y):
@@ -523,9 +523,16 @@
             return copysign(math.pi, y)
     return math.atan2(y, x)
 
+def wrapped_phase(space, w_z):
+    x, y = space.unpackcomplex(w_z)
+    result = call_c_func(c_phase, x, y)
+    return space.newfloat(result)
+wrapped_phase.unwrap_spec = [ObjSpace, W_Root]
+wrapped_phase.func_doc = names_and_docstrings['phase']
+
 
 def c_polar(x, y):
-    phi = c_atan2(x, y)
+    phi = c_phase(x, y)
     r = math.hypot(x, y)
     return r, phi
 


More information about the Pypy-commit mailing list