-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
gh-143006: Fix and optimize mixed comparison of float and int #143084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
gh-143006: Fix and optimize mixed comparison of float and int #143084
Conversation
When comparing negative non-integer float and int with the same number of bits in the integer part, __neg__() in the int subclass returning not an int caused an assertion error. Now the integer is no longer negated. Also, reduced the number of temporary created Python objects.
skirpichev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach looks better for me. Few minor nits.
| /* Convert w to a double if it fits. In particular, 0 fits. */ | ||
| int64_t nbits64 = _PyLong_NumBits(w); | ||
| assert(nbits64 >= 0); | ||
| assert(!PyErr_Occurred()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks useless.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It checks that _PyLong_NumBits() does not fail. It newer fails now.
Objects/floatobject.c
Outdated
| } | ||
| /* v and w have the same number of bits before the radix | ||
| * point. Construct two ints that have the same comparison | ||
| * outcome. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now this comment is outdated.
| if (vsign < 0) { | ||
| op = Py_GT; // v >= w <=> trunc(v) > w | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, it might worth to do sign canonicalization, as before, to get rid of such cases. But I think we have to expose long_neg() for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will only remove 2 of 6 cases and simplify other 2. The code for negation of both arguments is more complex.
We could also use two tables, this would make the code smaller: op = table[vsign > 0][op].
When comparing negative non-integer float and int with the same number of bits in the integer part,
__neg__()in the int subclass returning not an int caused an assertion error.Now the integer is no longer negated. Also, reduced the number of temporary created Python objects.
float_richcomparevia re-entrant__neg__#143006