Index: Misc/NEWS =================================================================== --- Misc/NEWS (revision 82559) +++ Misc/NEWS (working copy) @@ -1394,6 +1394,9 @@ Extension Modules ----------------- +- Add new functions math.isfinite and cmath.isfinite, to round out + existing isinf and isnan functions. + - In the math module, correctly lookup __trunc__, __ceil__, and __floor__ as special methods. Index: Doc/library/cmath.rst =================================================================== --- Doc/library/cmath.rst (revision 82559) +++ Doc/library/cmath.rst (working copy) @@ -187,6 +187,11 @@ Classification functions ------------------------ +.. function:: isfinite(x) + + Return ``True`` if both the real and imaginary parts of *x* are finite, + and ``False`` otherwise. + .. function:: isinf(x) Return *True* if the real or the imaginary part of x is positive Index: Doc/library/math.rst =================================================================== --- Doc/library/math.rst (revision 82559) +++ Doc/library/math.rst (working copy) @@ -97,6 +97,12 @@ `_\. +.. function:: isfinite(x) + + Return ``True`` if *x* is neither an infinity nor a nan, and + ``False`` otherwise. (Note that 0.0 *is* considered finite.) + + .. function:: isinf(x) Check if the float *x* is positive or negative infinity. Index: Lib/test/test_math.py =================================================================== --- Lib/test/test_math.py (revision 82559) +++ Lib/test/test_math.py (working copy) @@ -930,6 +930,15 @@ #self.assertEquals((), math.trunc(t)) #self.assertRaises(TypeError, math.trunc, t, 0) + def testIsfinite(self): + self.assertTrue(math.isfinite(0.0)) + self.assertTrue(math.isfinite(-0.0)) + self.assertTrue(math.isfinite(1.0)) + self.assertTrue(math.isfinite(-1.0)) + self.assertFalse(math.isfinite(float("nan"))) + self.assertFalse(math.isfinite(float("inf"))) + self.assertFalse(math.isfinite(float("-inf"))) + def testIsnan(self): self.assertTrue(math.isnan(float("nan"))) self.assertTrue(math.isnan(float("inf")* 0.)) Index: Lib/test/test_cmath.py =================================================================== --- Lib/test/test_cmath.py (revision 82559) +++ Lib/test/test_cmath.py (working copy) @@ -442,6 +442,15 @@ self.assertCEqual(rect(1, pi/2), (0, 1.)) self.assertCEqual(rect(1, -pi/2), (0, -1.)) + def test_isfinite(self): + 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) + self.assertEquals(cmath.isfinite(z), + math.isfinite(x) and math.isfinite(y)) + def test_isnan(self): self.assertFalse(cmath.isnan(1)) self.assertFalse(cmath.isnan(1j)) Index: Modules/mathmodule.c =================================================================== --- Modules/mathmodule.c (revision 82559) +++ Modules/mathmodule.c (working copy) @@ -1822,6 +1822,19 @@ Convert angle x from degrees to radians."); static PyObject * +math_isfinite(PyObject *self, PyObject *arg) +{ + double x = PyFloat_AsDouble(arg); + if (x == -1.0 && PyErr_Occurred()) + return NULL; + return PyBool_FromLong((long)Py_IS_FINITE(x)); +} + +PyDoc_STRVAR(math_isfinite_doc, +"isfinite(x) -> bool\n\n\ +Check if float x is finite (not an infinity or NaN)."); + +static PyObject * math_isnan(PyObject *self, PyObject *arg) { double x = PyFloat_AsDouble(arg); @@ -1874,6 +1887,7 @@ {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, {"isinf", math_isinf, METH_O, math_isinf_doc}, {"isnan", math_isnan, METH_O, math_isnan_doc}, + {"isfinite", math_isfinite, METH_O, math_isfinite_doc}, {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, {"lgamma", math_lgamma, METH_O, math_lgamma_doc}, {"log", math_log, METH_VARARGS, math_log_doc}, Index: Modules/cmathmodule.c =================================================================== --- Modules/cmathmodule.c (revision 82559) +++ Modules/cmathmodule.c (working copy) @@ -1024,6 +1024,19 @@ Convert from polar coordinates to rectangular coordinates."); static PyObject * +cmath_isfinite(PyObject *self, PyObject *args) +{ + Py_complex z; + if (!PyArg_ParseTuple(args, "D:isfinite", &z)) + return NULL; + return PyBool_FromLong(Py_IS_FINITE(z.real) && Py_IS_FINITE(z.imag)); +} + +PyDoc_STRVAR(cmath_isfinite_doc, +"isfinite(z) -> bool\n\ +Return True if both the real and imaginary parts of z are finite, else False."); + +static PyObject * cmath_isnan(PyObject *self, PyObject *args) { Py_complex z; @@ -1065,6 +1078,7 @@ {"cos", cmath_cos, METH_VARARGS, c_cos_doc}, {"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc}, {"exp", cmath_exp, METH_VARARGS, c_exp_doc}, + {"isfinite",cmath_isfinite, METH_VARARGS, cmath_isfinite_doc}, {"isinf", cmath_isinf, METH_VARARGS, cmath_isinf_doc}, {"isnan", cmath_isnan, METH_VARARGS, cmath_isnan_doc}, {"log", cmath_log, METH_VARARGS, cmath_log_doc},