Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3b33b5

Browse files
author
Michael W. Hudson
committed
"Fix" (for certain configurations of the planets, including
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.
1 parent bbca8da commit d3b33b5

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 2.4 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Implemented rich comparisons for floats, which seems to make
16+
comparisons involving NaNs somewhat less surprising when the
17+
underlying C compiler actually implements C99 semantics.
18+
1519
- Optimized list.extend() to save memory and no longer create
1620
intermediate sequences. Also, extend() now pre-allocates the
1721
needed memory whenever the length of the iterable is known in

Objects/floatobject.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,40 @@ float_compare(PyFloatObject *v, PyFloatObject *w)
360360
return (i < j) ? -1 : (i > j) ? 1 : 0;
361361
}
362362

363+
static PyObject*
364+
float_richcompare(PyObject *v, PyObject *w, int op)
365+
{
366+
double i, j;
367+
int r = 0;
368+
369+
CONVERT_TO_DOUBLE(v, i);
370+
CONVERT_TO_DOUBLE(w, j);
371+
372+
PyFPE_START_PROTECT("richcompare", return NULL)
373+
switch (op) {
374+
case Py_EQ:
375+
r = i==j;
376+
break;
377+
case Py_NE:
378+
r = i!=j;
379+
break;
380+
case Py_LE:
381+
r = i<=j;
382+
break;
383+
case Py_GE:
384+
r = i>=j;
385+
break;
386+
case Py_LT:
387+
r = i<j;
388+
break;
389+
case Py_GT:
390+
r = i>j;
391+
break;
392+
}
393+
PyFPE_END_PROTECT(a)
394+
return PyBool_FromLong(r);
395+
}
396+
363397
static long
364398
float_hash(PyFloatObject *v)
365399
{
@@ -834,7 +868,7 @@ PyTypeObject PyFloat_Type = {
834868
float_doc, /* tp_doc */
835869
0, /* tp_traverse */
836870
0, /* tp_clear */
837-
0, /* tp_richcompare */
871+
(richcmpfunc)float_richcompare, /* tp_richcompare */
838872
0, /* tp_weaklistoffset */
839873
0, /* tp_iter */
840874
0, /* tp_iternext */

0 commit comments

Comments
 (0)