exaloop / codon

A high-performance, zero-overhead, extensible Python compiler using LLVM
https://docs.exaloop.io/codon
Other
13.96k stars 498 forks source link

Integer overflow error when calculating large Fibonacci numbers #467

Closed MohammadMMoniri closed 10 months ago

MohammadMMoniri commented 10 months ago

When testing the fib function defined in the Example section with a very large input like 10000000000000000000000000000000000000000000000000000000000000, I encountered an integer overflow error:

fib.py:7:5-67: error: integer '10000000000000000000000000000000000000000000000000000000000000' cannot fit into 64-bit integer However, when running the same code using the CPython interpreter, it executes without errors. Is there any solution for using large inputs?

nikhil-swamix commented 10 months ago

When testing the fib function defined in the Example section with a very large input like 10000000000000000000000000000000000000000000000000000000000000, I encountered an integer overflow error:

fib.py:7:5-67: error: integer '10000000000000000000000000000000000000000000000000000000000000' cannot fit into 64-bit integer However, when running the same code using the CPython interpreter, it executes without errors. Is there any solution for using large inputs?

Good Question, i have found some reference implementation which i think the codon technical geeks can implement Reference: https://stackoverflow.com/questions/30081275/why-is-1000000000000000-in-range1000000000000001-so-fast-in-python-3

Luk-ESC commented 10 months ago

Codon's int is a 64-bit signed integer, whereas Python's (after version 3) can be arbitrarily large. However Codon does support larger integers via Int[N] where N is the bit width.

marioroy commented 10 months ago

Try to specify a string if UInt[N](1000...big_integer) fails e.g. UInt[N]("1000...big_integer"). That works.

bigint = UInt[256]    # max 1024

def fib(n: bigint):
    a, b = bigint(0), bigint(1)
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()

fib(bigint("10000000000000000000000000000000000000000000000000000000000000"))
0 1 1 2 3 5 ... 7654090467756936378415884538348976340768064993978954512095813
arshajii commented 10 months ago

This is indeed because of Codon's 64-bit int -- the workaround with UInt[256] is above and we'll likely look into adding a arbitrary-width long type at some point as well.