Discussion:
[Tutor] Can someone explain this to me please
Jon Paris
2015-08-21 18:04:18 UTC
Permalink
This code:

import sys
x = sys.maxsize
print ("Max size is: ", x)
y = (x + 1)
print ("y is", type(y), "with a value of", y)

Produces this result:

Max size is: 9223372036854775807
y is <class 'int'> with a value of 9223372036854775808

I was expecting it to error out but instead it produces a value greeter than the supposed maximum while still keeping it as an int. I’m confused. If sys.maxsize _isn’t_ the largest possible value then how do I determine what is?


Jon Paris
***@gmail.com



_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-08-21 19:24:15 UTC
Permalink
Post by Jon Paris
import sys
x = sys.maxsize
print ("Max size is: ", x)
y = (x + 1)
print ("y is", type(y), "with a value of", y)
Max size is: 9223372036854775807
y is <class 'int'> with a value of 9223372036854775808
I was expecting it to error out but instead it produces a value greeter than the supposed maximum while still keeping it as an int. I’m confused. If sys.maxsize _isn’t_ the largest possible value then how do I determine what is?
Jon Paris
If you go over sys.maxsize, Python will just get you a long.
The idea is to do away with the distinction between long and int.
see: https://www.python.org/dev/peps/pep-0237/

Laura
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://
Alan Gauld
2015-08-21 19:37:23 UTC
Permalink
Post by Jon Paris
Max size is: 9223372036854775807
y is <class 'int'> with a value of 9223372036854775808
I was expecting it to error out
The documentation says:
-------------------
sys.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 from your output we can deduce that Python ints (in version 3)
are not stored in a Py_ssize_t variable. (In Python 2 they are
so what you expect is almost what you would get - except
Python converts int to long int automatically.)

Python 3 ints are long ints, which the documentation says means
they have "infinite precision" In practice their max size depends
on how much memory you have!

There are others here who can give you the inner details of how
it is all done, but for most programmers that's all you normally
need to know.

Where you can run into issues is where you use modules that,
in turn, use the underlying C libraries to do calculations.
Passing a long int to them can result in errors.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/lis
eryksun
2015-08-22 12:00:11 UTC
Permalink
Post by Jon Paris
import sys
x = sys.maxsize
print ("Max size is: ", x)
y = (x + 1)
print ("y is", type(y), "with a value of", y)
Max size is: 9223372036854775807
y is <class 'int'> with a value of 9223372036854775808
I was expecting it to error out but instead it produces a value greeter than the
supposed maximum while still keeping it as an int. I’m confused. If
sys.maxsize _isn’t_ the largest possible value then how do I determine what is?
sys.maxsize is the "maximum size lists, strings, dicts, and many other
containers can have". This value is related to the theoretical maximum
of Python's built-in arbitrary precision integer type (i.e. long in
Python 2 and int in Python 3), which can be thought of as a
'container' for 15-bit or 30-bit "digits". For example, in a 64-bit
version of Python 3 that's compiled to use 30-bit digits in its int
objects, the limit is about (sys.maxsize bytes) // (4 bytes /
30bit_digit) * (9 decimal_digits / 30bit_digit) ==
20752587082923245559 decimal digits. In practice you'll get a
MemoryError (or probably a human impatience KeyboardInterrupt) long
before that.

sys.maxint (only in Python 2) is the largest positive value for Python
2's fixed-precision int type. In pure Python code, integer operations
seamlessly transition to using arbitrary-precision integers, so you
have no reason to worry, practically speaking, about reaching the
"largest possible value". As a matter of trivia, sys.maxint in CPython
corresponds to the maximum value of a C long int. In a 64-bit Windows
process, a C long int is 32-bit, which means sys.maxint is 2**31 - 1.
In every other supported OS, sys.maxint is 2**63 - 1 in a 64-bit
process.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org

Loading...