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
ABS(x) == 0 can be simplified to x == 0 #52905
Comments
I noticed that in longobject.c, there are a few spots that take the absolute value of an int before comparing the int to 0. There's no -0 for C integers, so the absolute value isn't necessary. I traced back through the commit history, and it looks like they're an artifact of the original one's-complement longobject.c (which COULD have -0). longobject was switched to two's-complement in 1992. ;-) |
Thanks! Fixed in r80961 (trunk) and r80962 (py3k). BTW, are there any particular commit messages or news entries that indicate a switch to two's complement? I've looked for these in the past, but not found them. |
I found:
in Misc/HISTORY, for the Python 0.9.6 release. That's different, though: it's about Python's semantics, not C's. It's orthogonal to the question of whether the underlying C implementation uses two's complement, ones' complement or sign-magnitude. I can't find any explicit indication of a decision to assume that the hardware integers are two's complement anywhere; as far as I know, no such decision was ever taken. But of course your simplifications are valid regardless of the integer representation of the machine. |
Ah, now I understand :) r2604 introduced a scheme where for a negative PyLongObject x with n digits, the value stored in x->ob_size was -1-n. A little like ones' complement, but unrelated to anything to do with the platform integer representation. So at that stage there were two possible internal representations of 0L, which is why all the ZABS() stuff was necessary to make sure that those two representations of 0L compared equal. r2751 (in 1992, indeed!) reversed this, using -n instead of -1-n for this case, but didn't remove the extra tests for the two different representations of 0L. r2751 also changed the semantics of long bitwise operations for negative numbers, but that's not relevant to this issue. Sorry for being slow. It's impressive that this piece of redundant code has survived this long. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: