diff -r d115dc671f52 Doc/library/numbers.rst --- a/Doc/library/numbers.rst Sun Oct 13 11:34:01 2013 +0100 +++ b/Doc/library/numbers.rst Sun Oct 13 14:04:16 2013 +0200 @@ -52,6 +52,12 @@ Real also provides defaults for :func:`complex`, :attr:`~Complex.real`, :attr:`~Complex.imag`, and :meth:`~Complex.conjugate`. + .. versionchanged: 3.4 + + :meth:`~Real.is_infinite`, :meth:`~Real.is_finite` and + :meth:`~Real.is_nan` added. + + .. class:: Rational diff -r d115dc671f52 Doc/library/stdtypes.rst --- a/Doc/library/stdtypes.rst Sun Oct 13 11:34:01 2013 +0100 +++ b/Doc/library/stdtypes.rst Sun Oct 13 14:04:16 2013 +0200 @@ -534,6 +534,24 @@ .. versionadded:: 3.2 +.. method:: int.is_finite() + + Always returns ``True`` + + .. versionadded:: 3.4 + +.. method:: int.is_infinite() + + Always returns ``False`` + + .. versionadded:: 3.4 + +.. method:: int.is_nan() + + Always returns ``False`` + + .. versionadded:: 3.4 + Additional Methods on Float --------------------------- @@ -558,6 +576,27 @@ >>> (3.2).is_integer() False +.. method:: float.is_finite() + + Return ``True`` if *float* is neither an infinity nor a NaN, and + ``False`` otherwise. (Note that ``0.0`` *is* considered finite.) + + .. versionadded:: 3.4 + +.. method:: float.is_infinite() + + Return ``True`` if *float* is a positive or negative infinity, and + ``False`` otherwise. + + .. versionadded:: 3.4 + +.. method:: float.is_nan() + + Return ``True`` if *float* is a NaN (not a number), and ``False`` otherwise. + + .. versionadded:: 3.4 + + Two methods support conversion to and from hexadecimal strings. Since Python's floats are stored internally as binary numbers, converting a float to or from a diff -r d115dc671f52 Lib/numbers.py --- a/Lib/numbers.py Sun Oct 13 11:34:01 2013 +0100 +++ b/Lib/numbers.py Sun Oct 13 14:04:16 2013 +0200 @@ -266,6 +266,21 @@ """Conjugate is a no-op for Reals.""" return +self + #@abstractmethod + def is_infinite(self): + """Return True if the float is positive or negative infinite.""" + raise NotImplementedError + + #@abstractmethod + def is_finite(self): + """RReturn True if the float is finite, neither infinite nor NaN.""" + raise NotImplementedError + + #@abstractmethod + def is_nan(self): + """Return True if the float is not a number (NaN).""" + raise NotImplementedError + Real.register(float) diff -r d115dc671f52 Lib/test/test_float.py --- a/Lib/test/test_float.py Sun Oct 13 11:34:01 2013 +0100 +++ b/Lib/test/test_float.py Sun Oct 13 14:04:16 2013 +0200 @@ -419,6 +419,24 @@ #self.assertTrue(0.0 < pow_op(2.0, -1047) < 1e-315) #self.assertTrue(0.0 > pow_op(-2.0, -1047) > -1e-315) + @support.requires_IEEE_754 + def test_float_is(self): + for f in (0., -0., 1., -1., sys.float_info.max, sys.float_info.min): + self.assertTrue(f.is_finite()) + self.assertFalse(f.is_infinite()) + self.assertFalse(f.is_nan()) + + self.assertFalse(NAN.is_finite()) + self.assertFalse(NAN.is_infinite()) + self.assertTrue(NAN.is_nan()) + self.assertFalse(INF.is_finite()) + self.assertTrue(INF.is_infinite()) + self.assertFalse(INF.is_nan()) + NINF = -INF + self.assertFalse(NINF.is_finite()) + self.assertTrue(NINF.is_infinite()) + self.assertFalse(NINF.is_nan()) + @requires_setformat class FormatFunctionsTestCase(unittest.TestCase): diff -r d115dc671f52 Lib/test/test_int.py --- a/Lib/test/test_int.py Sun Oct 13 11:34:01 2013 +0100 +++ b/Lib/test/test_int.py Sun Oct 13 14:04:16 2013 +0200 @@ -209,6 +209,12 @@ self.assertEqual(int('2br45qc', 35), 4294967297) self.assertEqual(int('1z141z5', 36), 4294967297) + def test_int_is(self): + for i in (0, 1, -1, 10**100000): + self.assertTrue(i.is_finite()) + self.assertFalse(i.is_infinite()) + self.assertFalse(i.is_nan()) + @support.cpython_only def test_small_ints(self): # Bug #3236: Return small longs from PyLong_FromString diff -r d115dc671f52 Objects/floatobject.c --- a/Objects/floatobject.c Sun Oct 13 11:34:01 2013 +0100 +++ b/Objects/floatobject.c Sun Oct 13 14:04:16 2013 +0200 @@ -797,9 +797,8 @@ return o; } -#if 0 static PyObject * -float_is_inf(PyObject *v) +float_is_infinite(PyObject *v) { double x = PyFloat_AsDouble(v); if (x == -1.0 && PyErr_Occurred()) @@ -824,7 +823,6 @@ return NULL; return PyBool_FromLong((long)Py_IS_FINITE(x)); } -#endif static PyObject * float_trunc(PyObject *v) @@ -1740,14 +1738,12 @@ METH_NOARGS, float_hex_doc}, {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS, "Return True if the float is an integer."}, -#if 0 - {"is_inf", (PyCFunction)float_is_inf, METH_NOARGS, + {"is_infinite", (PyCFunction)float_is_infinite, METH_NOARGS, "Return True if the float is positive or negative infinite."}, {"is_finite", (PyCFunction)float_is_finite, METH_NOARGS, "Return True if the float is finite, neither infinite nor NaN."}, {"is_nan", (PyCFunction)float_is_nan, METH_NOARGS, "Return True if the float is not a number (NaN)."}, -#endif {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS}, {"__getformat__", (PyCFunction)float_getformat, METH_O|METH_CLASS, float_getformat_doc}, diff -r d115dc671f52 Objects/longobject.c --- a/Objects/longobject.c Sun Oct 13 11:34:01 2013 +0100 +++ b/Objects/longobject.c Sun Oct 13 14:04:16 2013 +0200 @@ -4689,14 +4689,35 @@ >>> (37).bit_length()\n\ 6"); -#if 0 +static PyObject * +long_is_infinite(PyObject *v) +{ + if (!PyLong_Check(v)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return NULL; + } + Py_RETURN_FALSE; +} + static PyObject * long_is_finite(PyObject *v) { + if (!PyLong_Check(v)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return NULL; + } Py_RETURN_TRUE; } -#endif - + +static PyObject * +long_is_nan(PyObject *v) +{ + if (!PyLong_Check(v)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return NULL; + } + Py_RETURN_FALSE; +} static PyObject * long_to_bytes(PyLongObject *v, PyObject *args, PyObject *kwds) @@ -4883,10 +4904,12 @@ "Returns self, the complex conjugate of any int."}, {"bit_length", (PyCFunction)long_bit_length, METH_NOARGS, long_bit_length_doc}, -#if 0 - {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS, + {"is_infinite", (PyCFunction)long_is_infinite, METH_NOARGS, + "Returns always False."}, + {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS, "Returns always True."}, -#endif + {"is_nan", (PyCFunction)long_is_nan, METH_NOARGS, + "Returns always False."}, {"to_bytes", (PyCFunction)long_to_bytes, METH_VARARGS|METH_KEYWORDS, long_to_bytes_doc}, {"from_bytes", (PyCFunction)long_from_bytes,