[Python-checkins] r60832 - in python/branches/trunk-math: Doc/library/stdtypes.rst Lib/test/test_builtin.py Lib/test/test_float.py Misc/NEWS Objects/floatobject.c
christian.heimes
python-checkins at python.org
Fri Feb 15 11:38:36 CET 2008
Author: christian.heimes
Date: Fri Feb 15 11:38:35 2008
New Revision: 60832
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_float.py
python/branches/trunk-math/Misc/NEWS
python/branches/trunk-math/Objects/floatobject.c
Log:
Added methods is_inf, is_nan and is_integer to float type
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 11:38:35 2008
@@ -390,6 +390,8 @@
.. XXXJH exceptions: overflow (when? what operations?) zerodivision
+.. XXX float.is_integer, isnan, isinf, as_integer_ratio
+
.. _bitstring-ops:
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 11:38:35 2008
@@ -721,6 +721,12 @@
self.assertRaises(OverflowError, float('-inf').as_integer_ratio)
self.assertRaises(ValueError, float('nan').as_integer_ratio)
+ def test_float_is_integer(self):
+ for f in (0., -1., 1., 23., 1E100, -1E300):
+ self.assert_(f.is_integer(), f)
+ for f in (0.1, 1E-10, -10.5, float("inf"), float("nan")):
+ self.failIf(f.is_integer(), f)
+
def test_getattr(self):
import sys
self.assert_(getattr(sys, 'stdout') is sys.stdout)
Modified: python/branches/trunk-math/Lib/test/test_float.py
==============================================================================
--- python/branches/trunk-math/Lib/test/test_float.py (original)
+++ python/branches/trunk-math/Lib/test/test_float.py Fri Feb 15 11:38:35 2008
@@ -6,6 +6,9 @@
from math import isinf, isnan
import operator
+INF = float("inf")
+NAN = float("nan")
+
class FormatFunctionsTestCase(unittest.TestCase):
def setUp(self):
@@ -203,6 +206,17 @@
self.assertEqual(str(1e300 * 1e300 * 0), "nan")
self.assertEqual(str(-1e300 * 1e300 * 0), "nan")
+ def test_float_nan(self):
+ self.assert_(NAN.is_nan())
+ self.failIf(INF.is_nan())
+ self.failIf((0.).is_nan())
+
+ def test_float_inf(self):
+ self.assert_(INF.is_inf())
+ self.failIf(NAN.is_inf())
+ self.failIf((0.).is_inf())
+
+
class IEEE754TestCase(unittest.TestCase):
def setUp(self):
Modified: python/branches/trunk-math/Misc/NEWS
==============================================================================
--- python/branches/trunk-math/Misc/NEWS (original)
+++ python/branches/trunk-math/Misc/NEWS Fri Feb 15 11:38:35 2008
@@ -20,6 +20,8 @@
without the four functions and copysign in libm were added to a new file
Python/pymath.c.
+- Added methods is_inf, is_nan and is_integer to float type.
+
- Fixed repr() and str() of complex numbers with infinity or nan as real or
imaginary part.
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 11:38:35 2008
@@ -1131,6 +1131,46 @@
}
static PyObject *
+float_is_integer(PyObject *v)
+{
+ double x = PyFloat_AsDouble(v);
+ PyObject *o;
+
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ if (!Py_IS_FINITE(x))
+ Py_RETURN_FALSE;
+ PyFPE_START_PROTECT("is_integer", return 0)
+ o = (floor(x) == x) ? Py_True : Py_False;
+ PyFPE_END_PROTECT(x)
+ if (errno != 0) {
+ PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
+ PyExc_ValueError);
+ return NULL;
+ }
+ Py_INCREF(o);
+ return o;
+}
+
+static PyObject *
+float_is_inf(PyObject *v)
+{
+ double x = PyFloat_AsDouble(v);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyBool_FromLong((long)Py_IS_INFINITY(x));
+}
+
+static PyObject *
+float_is_nan(PyObject *v)
+{
+ double x = PyFloat_AsDouble(v);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyBool_FromLong((long)Py_IS_NAN(x));
+}
+
+static PyObject *
float_trunc(PyObject *v)
{
double x = PyFloat_AsDouble(v);
@@ -1448,12 +1488,18 @@
}
static PyMethodDef float_methods[] = {
- {"conjugate", (PyCFunction)float_float, METH_NOARGS,
+ {"conjugate", (PyCFunction)float_float, METH_NOARGS,
"Returns self, the complex conjugate of any float."},
{"__trunc__", (PyCFunction)float_trunc, METH_NOARGS,
"Returns the Integral closest to x between 0 and x."},
{"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS,
float_as_integer_ratio_doc},
+ {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS,
+ "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_nan", (PyCFunction)float_is_nan, METH_NOARGS,
+ "Returns True if the float is not a number (NaN)."},
{"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
{"__getformat__", (PyCFunction)float_getformat,
METH_O|METH_CLASS, float_getformat_doc},
More information about the Python-checkins
mailing list