Skip to content

ABS(x) == 0 can be simplified to x == 0 #52905

Closed

Description

@stutzbach
mannequin
BPO 8659
Nosy @mdickinson
Files
  • abs-zero.patch
  • 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:

    assignee = None
    closed_at = <Date 2010-05-08.08:05:05.781>
    created_at = <Date 2010-05-08.06:15:20.845>
    labels = ['interpreter-core', 'performance']
    title = 'ABS(x) == 0 can be simplified to x == 0'
    updated_at = <Date 2010-05-08.10:07:43.271>
    user = 'https://bugs.python.org/stutzbach'

    bugs.python.org fields:

    activity = <Date 2010-05-08.10:07:43.271>
    actor = 'mark.dickinson'
    assignee = 'stutzbach'
    closed = True
    closed_date = <Date 2010-05-08.08:05:05.781>
    closer = 'mark.dickinson'
    components = ['Interpreter Core']
    creation = <Date 2010-05-08.06:15:20.845>
    creator = 'stutzbach'
    dependencies = []
    files = ['17259']
    hgrepos = []
    issue_num = 8659
    keywords = ['patch', 'needs review']
    message_count = 4.0
    messages = ['105265', '105267', '105268', '105273']
    nosy_count = 2.0
    nosy_names = ['mark.dickinson', 'stutzbach']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue8659'
    versions = ['Python 3.2']

    Activity

    1. stutzbach commented on May 8, 2010

      stutzbachmannequin
      MannequinAuthor

      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. ;-)

    2. self-assigned this
      on May 8, 2010
    3. mdickinson commented on May 8, 2010

      @mdickinson
      Member

      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.

    4. mdickinson commented on May 8, 2010

      @mdickinson
      Member

      I found:

      • Long integer shift/mask operations now simulate 2's complement
        to give more useful results for negative operands

      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.

    5. mdickinson commented on May 8, 2010

      @mdickinson
      Member

      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.

    6. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usage

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions