Index: Misc/NEWS =================================================================== --- Misc/NEWS (revision 82559) +++ Misc/NEWS (working copy) @@ -1394,6 +1394,9 @@ Extension Modules ----------------- +- Add new function math.isfinite, to round out existing functions + math.isinf and math.isnan. + - In the math module, correctly lookup __trunc__, __ceil__, and __floor__ as special methods. 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 :const:`True` if *x* is neither an infinity nor a nan, and + :const:`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: 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},