Skip to content
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

Cython differs from CPython: unexpected integer overflow #2866

Open
mbuzdalov opened this Issue Feb 26, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@mbuzdalov
Copy link

mbuzdalov commented Feb 26, 2019

The following purely Python code produces different answers in CPython and Cython:

def countingsort(list_to_sort):
    help = [10 ** _ for _ in range(0, 30)]
    with open('output.txt', 'wt') as ttt:
        ttt.write(str(sum(help)))

def main():
    countingsort([1])

main()

CPython produces the expected answer, 111111111111111111111111111111. However Cython produces a different answer: 119304647 on Windows (where long is a 32-bit type) and 12861934301395841479 on Linux (with a 64-bit long).

The problem is supposedly in the list generator, where 10 is not promoted to a big integer before raising to a very large power.

Relevant software versions:

  • Cython version 0.29.5
  • Python 3.7.2
  • gcc (GCC) 8.2.1 20181127

Note that with Python 2 this problem is not reproduced.

@scoder

This comment has been minimized.

Copy link
Contributor

scoder commented Feb 26, 2019

Thanks for the report. IIRC, ** is already considered an unsafe integer operation that defaults to Python object operations if no types are provided, so maybe this goes wrong due to the integer range loop. Maybe the integer range-loop optimision should only be handled in type inference and not enforced anywhere else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.