Alex Gaynor committed a2dc8ef

added cmath.isfinite

  • Participants
  • Parent commits 5c7c60f
  • Branches py3k

Comments (0)

Files changed (6)

File pypy/module/cmath/

     '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):

File pypy/module/cmath/

 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,
+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
 def call_c_func(c_func, space, x, y):
     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']

File pypy/module/cmath/

                 return ST_NZERO
-def isfinite(d):
-    return not isinf(d) and not isnan(d)
 P   = math.pi
 P14 = 0.25 * math.pi

File pypy/module/cmath/test/

         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):

File pypy/module/math/

     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',
        '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',

File pypy/module/math/

     """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):