0

I don’t understand how my code can produce integers bigger than the local max_int! The fibonacci(100) below produces an integer greater than sys.maxsize.

    known={0:0,1:1}
    def fibonacci(n):
    if n in known:
        return known[n]
    res = fibonacci(n-1) + fibonacci(n-2)
    known[n]=res
    return res
    print(fibonacci(100))

354224848179261915075

    type(known[100])

int

    import sys
    print(sys.int_info)

sys.int_info(bits_per_digit=30, sizeof_digit=4)

    print(sys.maxsize)

9223372036854775807

Source: Think Python, version 2.0.17, p107, 2012 Allen Downey.

  • 1
    So why do you think that `sys.maxsize` and the `int` type should be connected? – quamrana Apr 27 '22 at 20:16
  • 1
    You can read an interesting discussion about the maximum value a variable can hold in Python here: https://stackoverflow.com/questions/9860588/maximum-value-for-long-integer In short - numbers are converted to `long`s once they go over the `int` limit. From there they don't hold an explicitly defined limit. – Itai Dagan Apr 27 '22 at 20:24

1 Answers1

0

As you may have noticed, Python3 does not have maxint. And the documentation clearly states for maxsize.

An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2**31 - 1 on a 32-bit platform and 2**63 - 1 on a 64-bit platform.

So this has nothing to do with the maximum integer size in Python

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
Frank Yellin
  • 9,127
  • 1
  • 12
  • 22
  • Wow! And thank you. So … ```print(fibonacci(500))``` **139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125** This just amazes me. – John McCullough Apr 27 '22 at 22:25