]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
"Fix" (for certain configurations of the planets, including
authorMichael W. Hudson <mwh@python.net>
Thu, 19 Feb 2004 19:35:22 +0000 (19:35 +0000)
committerMichael W. Hudson <mwh@python.net>
Thu, 19 Feb 2004 19:35:22 +0000 (19:35 +0000)
recent gcc on Linux/x86)

[ 899109 ] 1==float('nan')

by implementing rich comparisons for floats.

Seems to make comparisons involving NaNs somewhat less surprising
when the underlying C compiler actually implements C99 semantics.

Misc/NEWS
Objects/floatobject.c

index 3fdef4bde2121d945d4178ae1fa64f4b4514f508..2d02ed1ca6b6c6768a7fe509f0e9441a7cc13fb8 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 2.4 alpha 1?
 Core and builtins
 -----------------
 
+- Implemented rich comparisons for floats, which seems to make
+  comparisons involving NaNs somewhat less surprising when the
+  underlying C compiler actually implements C99 semantics.
+
 - Optimized list.extend() to save memory and no longer create
   intermediate sequences.  Also, extend() now pre-allocates the
   needed memory whenever the length of the iterable is known in
index 7476ac7ca8dd88ecd7684a6ca89f2269cee740f7..3f06b4c6243992e37dfed1dd177e5665dfd40f94 100644 (file)
@@ -360,6 +360,40 @@ float_compare(PyFloatObject *v, PyFloatObject *w)
        return (i < j) ? -1 : (i > j) ? 1 : 0;
 }
 
+static PyObject*
+float_richcompare(PyObject *v, PyObject *w, int op)
+{
+       double i, j;
+       int r = 0;
+
+       CONVERT_TO_DOUBLE(v, i);
+       CONVERT_TO_DOUBLE(w, j);
+
+       PyFPE_START_PROTECT("richcompare", return NULL)
+       switch (op) {
+       case Py_EQ:
+               r = i==j;
+               break;
+       case Py_NE:
+               r = i!=j;
+               break;
+       case Py_LE:
+               r = i<=j;
+               break;
+       case Py_GE:
+               r = i>=j;
+               break;
+       case Py_LT:
+               r = i<j;
+               break;
+       case Py_GT:
+               r = i>j;
+               break;
+       }
+       PyFPE_END_PROTECT(a)
+       return PyBool_FromLong(r);
+}
+
 static long
 float_hash(PyFloatObject *v)
 {
@@ -834,7 +868,7 @@ PyTypeObject PyFloat_Type = {
        float_doc,                              /* tp_doc */
        0,                                      /* tp_traverse */
        0,                                      /* tp_clear */
-       0,                                      /* tp_richcompare */
+       (richcmpfunc)float_richcompare,         /* tp_richcompare */
        0,                                      /* tp_weaklistoffset */
        0,                                      /* tp_iter */
        0,                                      /* tp_iternext */