[Python-checkins] r60836 - in python/branches/trunk-math: Doc/library/stdtypes.rst Lib/test/test_builtin.py Lib/test/test_complex.py Objects/complexobject.c Objects/floatobject.c Objects/intobject.c Objects/longobject.c
christian.heimes
python-checkins at python.org
Fri Feb 15 16:39:08 CET 2008
Author: christian.heimes
Date: Fri Feb 15 16:39:08 2008
New Revision: 60836
Modified:
python/branches/trunk-math/Doc/library/stdtypes.rst
python/branches/trunk-math/Lib/test/test_builtin.py
python/branches/trunk-math/Lib/test/test_complex.py
python/branches/trunk-math/Objects/complexobject.c
python/branches/trunk-math/Objects/floatobject.c
python/branches/trunk-math/Objects/intobject.c
python/branches/trunk-math/Objects/longobject.c
Log:
Added is_finite to int, long, float and complex
Modified: python/branches/trunk-math/Doc/library/stdtypes.rst
==============================================================================
--- python/branches/trunk-math/Doc/library/stdtypes.rst (original)
+++ python/branches/trunk-math/Doc/library/stdtypes.rst Fri Feb 15 16:39:08 2008
@@ -315,6 +315,8 @@
+--------------------+---------------------------------+--------+
| ``x ** y`` | *x* to the power *y* | \(7) |
+--------------------+---------------------------------+--------+
+| ``x.is_finite()`` | test for finite value | \(8) |
++--------------------+---------------------------------+--------+
.. index::
triple: operations on; numeric; types
@@ -370,6 +372,11 @@
Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for
programming languages.
+(8)
+ :class:`int` and :class:`long` are always finite. A :class:`real` is finite
+ when it's neither infinite nor NaN. A :class:`complex` is finite when both
+ its real and its imaginary part is finite.
+
All :class:`numbers.Real` types (:class:`int`, :class:`long`, and
:class:`float`) also include the following operations:
Modified: python/branches/trunk-math/Lib/test/test_builtin.py
==============================================================================
--- python/branches/trunk-math/Lib/test/test_builtin.py (original)
+++ python/branches/trunk-math/Lib/test/test_builtin.py Fri Feb 15 16:39:08 2008
@@ -938,6 +938,8 @@
self.assertEqual(int('2br45qc', 35), 4294967297L)
self.assertEqual(int('1z141z5', 36), 4294967297L)
+ self.assertEqual(int(0).is_finite(), True)
+
def test_intconversion(self):
# Test __int__()
class ClassicMissingMethods:
@@ -1261,6 +1263,8 @@
self.assertEqual(long('2br45qc', 35), 4294967297)
self.assertEqual(long('1z141z5', 36), 4294967297)
+ self.assertEqual(long(0).is_finite(), True)
+
def test_longconversion(self):
# Test __long__()
Modified: python/branches/trunk-math/Lib/test/test_complex.py
==============================================================================
--- python/branches/trunk-math/Lib/test/test_complex.py (original)
+++ python/branches/trunk-math/Lib/test/test_complex.py Fri Feb 15 16:39:08 2008
@@ -373,6 +373,16 @@
except (OSError, IOError):
pass
+ def test_finite(self):
+ self.assert_(complex(23, 42).is_finite())
+ self.assert_(complex(0, 0).is_finite())
+ self.failIf(complex(INF, -2).is_finite())
+ self.failIf(complex(1, INF).is_finite())
+ self.failIf(complex(INF, INF).is_finite())
+ self.failIf(complex(NAN, -2).is_finite())
+ self.failIf(complex(1, NAN).is_finite())
+ self.failIf(complex(NAN, NAN).is_finite())
+
if float.__getformat__("double").startswith("IEEE"):
def test_plus_minus_0j(self):
# test that -0j and 0j literals are not identified
Modified: python/branches/trunk-math/Objects/complexobject.c
==============================================================================
--- python/branches/trunk-math/Objects/complexobject.c (original)
+++ python/branches/trunk-math/Objects/complexobject.c Fri Feb 15 16:39:08 2008
@@ -796,9 +796,25 @@
return Py_BuildValue("(D)", &v->cval);
}
+static PyObject *
+complex_is_finite(PyObject *self)
+{
+ Py_complex c;
+ c = ((PyComplexObject *)self)->cval;
+ return PyBool_FromLong((long)(Py_IS_FINITE(c.real) &&
+ Py_IS_FINITE(c.imag)));
+}
+
+PyDoc_STRVAR(complex_is_finite_doc,
+"complex.is_finite() -> bool\n"
+"\n"
+"Returns True if the real and the imaginary part is finite.");
+
static PyMethodDef complex_methods[] = {
{"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
complex_conjugate_doc},
+ {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS,
+ complex_is_finite_doc},
{"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
{NULL, NULL} /* sentinel */
};
Modified: python/branches/trunk-math/Objects/floatobject.c
==============================================================================
--- python/branches/trunk-math/Objects/floatobject.c (original)
+++ python/branches/trunk-math/Objects/floatobject.c Fri Feb 15 16:39:08 2008
@@ -1171,6 +1171,15 @@
}
static PyObject *
+float_is_finite(PyObject *v)
+{
+ double x = PyFloat_AsDouble(v);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyBool_FromLong((long)Py_IS_FINITE(x));
+}
+
+static PyObject *
float_trunc(PyObject *v)
{
double x = PyFloat_AsDouble(v);
@@ -1498,6 +1507,8 @@
"Returns True if the float is an integer."},
{"is_inf", (PyCFunction)float_is_inf, METH_NOARGS,
"Returns True if the float is positive or negative infinite."},
+ {"is_finite", (PyCFunction)float_is_finite, METH_NOARGS,
+ "Returns True if the float is finite, neither infinite nor NaN."},
{"is_nan", (PyCFunction)float_is_nan, METH_NOARGS,
"Returns True if the float is not a number (NaN)."},
{"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
Modified: python/branches/trunk-math/Objects/intobject.c
==============================================================================
--- python/branches/trunk-math/Objects/intobject.c (original)
+++ python/branches/trunk-math/Objects/intobject.c Fri Feb 15 16:39:08 2008
@@ -1108,9 +1108,17 @@
return PyString_FromStringAndSize(p, &buf[sizeof(buf)] - p);
}
+static PyObject *
+int_is_finite(PyObject *v)
+{
+ Py_RETURN_TRUE;
+}
+
static PyMethodDef int_methods[] = {
{"conjugate", (PyCFunction)int_int, METH_NOARGS,
"Returns self, the complex conjugate of any int."},
+ {"is_finite", (PyCFunction)int_is_finite, METH_NOARGS,
+ "Returns always True."},
{"__trunc__", (PyCFunction)int_int, METH_NOARGS,
"Truncating an Integral returns itself."},
{"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
Modified: python/branches/trunk-math/Objects/longobject.c
==============================================================================
--- python/branches/trunk-math/Objects/longobject.c (original)
+++ python/branches/trunk-math/Objects/longobject.c Fri Feb 15 16:39:08 2008
@@ -3380,9 +3380,17 @@
return PyLong_FromLong((intptr_t)context);
}
+static PyObject *
+long_is_finite(PyObject *v)
+{
+ Py_RETURN_TRUE;
+}
+
static PyMethodDef long_methods[] = {
{"conjugate", (PyCFunction)long_long, METH_NOARGS,
"Returns self, the complex conjugate of any long."},
+ {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS,
+ "Returns always True."},
{"__trunc__", (PyCFunction)long_long, METH_NOARGS,
"Truncating an Integral returns itself."},
{"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
More information about the Python-checkins
mailing list