Closed MohammadMMoniri closed 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
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.
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
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.
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?