Skip to content

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Dec 22, 2025

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.

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.
Copy link
Contributor

@skirpichev skirpichev left a 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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks useless.

Copy link
Member Author

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.

}
/* v and w have the same number of bits before the radix
* point. Construct two ints that have the same comparison
* outcome.
Copy link
Contributor

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.

Comment on lines +474 to +476
if (vsign < 0) {
op = Py_GT; // v >= w <=> trunc(v) > w
}
Copy link
Contributor

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.

Copy link
Member Author

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].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants