Skip to content

Commit ea1ce5d

Browse files
Add more explanation comments.
1 parent f4cd2f2 commit ea1ce5d

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Objects/floatobject.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,24 @@ float_richcompare(PyObject *v, PyObject *w, int op)
461461
fracpart = modf(i, &intpart);
462462
if (fracpart != 0.0) {
463463
switch (op) {
464+
/* Non-integer float never equals to an int. */
464465
case Py_EQ:
465466
Py_RETURN_FALSE;
466467
case Py_NE:
467468
Py_RETURN_TRUE;
469+
/* For non-integer float, v <= w <=> v < w.
470+
* If v > 0: trunc(v) < v < trunc(v) + 1
471+
* v < w => trunc(v) < w
472+
* trunc(v) < w => trunc(v) + 1 <= w => v < w
473+
* If v < 0: trunc(v) - 1 < v < trunc(v)
474+
* v < w => trunc(v) - 1 < w => trunc(v) <= w
475+
* trunc(v) <= w => v < w
476+
*/
468477
case Py_LT:
469478
case Py_LE:
470479
op = vsign > 0 ? Py_LT : Py_LE;
471480
break;
481+
/* The same as above, but with opposite directions. */
472482
case Py_GT:
473483
case Py_GE:
474484
op = vsign > 0 ? Py_GE : Py_GT;

0 commit comments

Comments
 (0)