[pypy-commit] pypy py3k: added cmath.isfinite

alex_gaynor noreply at buildbot.pypy.org
Tue Nov 8 17:42:59 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: py3k
Changeset: r48956:a2dc8ef2c638
Date: 2011-11-08 11:42 -0500
http://bitbucket.org/pypy/pypy/changeset/a2dc8ef2c638/

Log:	added cmath.isfinite

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
@@ -29,7 +29,8 @@
     'phase': "Return argument, also known as the phase angle, of a complex.",
     'isinf': "Checks if the real or imaginary part of z is infinite.",
     'isnan': "Checks if the real or imaginary part of z is not a number (NaN)",
-    }
+    'isfinite': "isfinite(z) -> bool\nReturn True if both the real and imaginary parts of z are finite, else False.",
+}
 
 
 class Module(MixedModule):
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
@@ -1,33 +1,25 @@
 import math
 from math import fabs
-from pypy.rlib.objectmodel import specialize
-from pypy.rlib.rfloat import copysign, asinh, log1p, isinf, isnan
-from pypy.tool.sourcetools import func_with_new_name
+
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import NoneNotWrapped
 from pypy.module.cmath import names_and_docstrings
-from pypy.module.cmath.constant import DBL_MIN, CM_SCALE_UP, CM_SCALE_DOWN
-from pypy.module.cmath.constant import CM_LARGE_DOUBLE, DBL_MANT_DIG
-from pypy.module.cmath.constant import M_LN2, M_LN10
-from pypy.module.cmath.constant import CM_SQRT_LARGE_DOUBLE, CM_SQRT_DBL_MIN
-from pypy.module.cmath.constant import CM_LOG_LARGE_DOUBLE
-from pypy.module.cmath.special_value import isfinite, special_type, INF, NAN
-from pypy.module.cmath.special_value import sqrt_special_values
-from pypy.module.cmath.special_value import acos_special_values
-from pypy.module.cmath.special_value import acosh_special_values
-from pypy.module.cmath.special_value import asinh_special_values
-from pypy.module.cmath.special_value import atanh_special_values
-from pypy.module.cmath.special_value import log_special_values
-from pypy.module.cmath.special_value import exp_special_values
-from pypy.module.cmath.special_value import cosh_special_values
-from pypy.module.cmath.special_value import sinh_special_values
-from pypy.module.cmath.special_value import tanh_special_values
-from pypy.module.cmath.special_value import rect_special_values
+from pypy.module.cmath.constant import (DBL_MIN, CM_SCALE_UP, CM_SCALE_DOWN,
+    CM_LARGE_DOUBLE, DBL_MANT_DIG, M_LN2, M_LN10, CM_SQRT_LARGE_DOUBLE,
+    CM_SQRT_DBL_MIN, CM_LOG_LARGE_DOUBLE)
+from pypy.module.cmath.special_value import (special_type, INF, NAN,
+    sqrt_special_values, acos_special_values, acosh_special_values,
+    asinh_special_values, atanh_special_values, log_special_values,
+    exp_special_values, cosh_special_values, sinh_special_values,
+    tanh_special_values, rect_special_values)
+from pypy.rlib.objectmodel import specialize
+from pypy.rlib.rfloat import copysign, asinh, log1p, isinf, isnan, isfinite
+from pypy.tool.sourcetools import func_with_new_name
+
 
 pi = math.pi
 e  = math.e
 
-
 @specialize.arg(0)
 def call_c_func(c_func, space, x, y):
     try:
@@ -579,3 +571,12 @@
     res = c_isnan(x, y)
     return space.newbool(res)
 wrapped_isnan.func_doc = names_and_docstrings['isnan']
+
+def c_isfinite(x, y):
+    return isfinite(x) and isfinite(y)
+
+def wrapped_isfinite(space, w_z):
+    x, y = space.unpackcomplex(w_z)
+    res = c_isfinite(x, y)
+    return space.newbool(res)
+wrapped_isfinite.func_doc = names_and_docstrings['isfinite']
diff --git a/pypy/module/cmath/special_value.py b/pypy/module/cmath/special_value.py
--- a/pypy/module/cmath/special_value.py
+++ b/pypy/module/cmath/special_value.py
@@ -32,9 +32,6 @@
             else:
                 return ST_NZERO
 
-def isfinite(d):
-    return not isinf(d) and not isnan(d)
-
 
 P   = math.pi
 P14 = 0.25 * math.pi
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
@@ -92,6 +92,18 @@
         assert cmath.isnan(complex("inf+nanj"))
         assert cmath.isnan(complex("nan+infj"))
 
+    def test_isfinite(self):
+        import cmath
+        import math
+
+        real_vals = [
+            float('-inf'), -2.3, -0.0, 0.0, 2.3, float('inf'), float('nan')
+        ]
+        for x in real_vals:
+            for y in real_vals:
+                z = complex(x, y)
+                assert cmath.isfinite(z) == (math.isfinite(x) and math.isfinite(y))
+
     def test_user_defined_complex(self):
         import cmath
         class Foo(object):
diff --git a/pypy/module/math/__init__.py b/pypy/module/math/__init__.py
--- a/pypy/module/math/__init__.py
+++ b/pypy/module/math/__init__.py
@@ -8,8 +8,8 @@
     }
 
     interpleveldefs = {
-       'e'              : 'interp_math.get(space).w_e', 
-       'pi'             : 'interp_math.get(space).w_pi', 
+       'e'              : 'interp_math.get(space).w_e',
+       'pi'             : 'interp_math.get(space).w_pi',
        'pow'            : 'interp_math.pow',
        'cosh'           : 'interp_math.cosh',
        'copysign'       : 'interp_math.copysign',
@@ -39,6 +39,7 @@
        'acos'           : 'interp_math.acos',
        'isinf'          : 'interp_math.isinf',
        'isnan'          : 'interp_math.isnan',
+       'isfinite'       : 'interp_math.isfinite',
        'trunc'          : 'interp_math.trunc',
        'fsum'           : 'interp_math.fsum',
        'asinh'          : 'interp_math.asinh',
diff --git a/pypy/module/math/interp_math.py b/pypy/module/math/interp_math.py
--- a/pypy/module/math/interp_math.py
+++ b/pypy/module/math/interp_math.py
@@ -77,6 +77,12 @@
     """Return True if x is not a number."""
     return space.wrap(rfloat.isnan(_get_double(space, w_x)))
 
+def isfinite(space, w_x):
+    """isfinite(x) -> bool
+
+    Return True if x is neither an infinity nor a NaN, and False otherwise."""
+    return space.wrap(rfloat.isfinite(_get_double(space, w_x)))
+
 def pow(space, w_x, w_y):
     """pow(x,y)
 


More information about the pypy-commit mailing list