No overflow in variables?

P

Philip Red

Hi everyone. First of all sorry if my english is not good.
I have a question about something in Python I can not explain:
in every programming language I know (e.g. C#) if you exceed the max-value of a certain type (e.g. a long-integer) you get an overflow. Here is a simple example in C#:

static void Main(string[] args)
{
Int64 x = Int64.MaxValue;
Console.WriteLine(x); // output: 9223372036854775807
x = x * 2;
Console.WriteLine(x); // output: -2 (overflow)
Console.ReadKey();
}

Now I do the same with Python:

x = 9223372036854775807
print(type(x)) # <class 'int'>
x = x * 2 # 18446744073709551614
print(x) # <class 'int'>
print(type(x))

and I get the right output without overflow and the type is always a 'int'.
How does Python manages internally the types and their values? Where are they stored?

Thank you for your help :)
 
L

Larry Martell

Hi everyone. First of all sorry if my english is not good.
I have a question about something in Python I can not explain:
in every programming language I know (e.g. C#) if you exceed the max-value of a certain type (e.g. a long-integer) you get an overflow. Here is a simple example in C#:

static void Main(string[] args)
{
Int64 x = Int64.MaxValue;
Console.WriteLine(x); // output: 9223372036854775807
x = x * 2;
Console.WriteLine(x); // output: -2 (overflow)
Console.ReadKey();
}

Now I do the same with Python:

x = 9223372036854775807
print(type(x)) # <class 'int'>
x = x * 2 # 18446744073709551614
print(x) # <class 'int'>
print(type(x))

and I get the right output without overflow and the type is always a 'int'.
How does Python manages internally the types and their values? Where are they stored?

Thank you for your help :)

This may help you understand:

http://www.python.org/dev/peps/pep-0237/
 
C

Chris Angelico

Now I do the same with Python:

x = 9223372036854775807
print(type(x)) # <class 'int'>
x = x * 2 # 18446744073709551614
print(x) # <class 'int'>
print(type(x))

and I get the right output without overflow and the type is always a 'int'.
How does Python manages internally the types and their values? Where are they stored?

The Python integer type stores arbitrary precision. It's not a machine
word, like the C# integer types (plural, or does it have only one?
Either way), so it can store any integer you have RAM for. (Which
means, no, Python cannot represent Graham's Number in an int. Sorry
about that.)

Internally, I believe CPython uses the GNU Multiprecision Library
(GMP), which gives an efficient representation and operation format,
scaling to infinity or thereabouts. You can go to any size of integer
you like without there being any difference. There's a cost to that
(even small integers are a bit slower to work with), but it's SO
helpful to be able to work with arbitrarily large numbers that it's
worth that cost.

(Side note: In Python 2, small integers were represented by type 'int'
and those too big to fit into a machine word were automatically
promoted to type 'long'. Python 3 abolished 'int' and renamed 'long'
to 'int', giving what you see here. I'm of the opinion that
small-number arithmetic could be optimized by having small ints stored
as machine words instead of as GMP objects (which could be done
invisibly), but it may be that the complexity isn't worth it.)

I first met arbitrary-precision arithmetic in REXX, back in the 1990s.
It wasn't anything like as efficient as it is now, so for performance
it was important to set the NUMERIC DIGITS setting to just what you
need and no more. Today, thanks to GMP, any high level language should
be able to offer the same as Python does; in fact, I'd consider
infinite-precision integers to be among the fundamental and critical
aspects of any modern high level language (along with object reference
semantics, first-class arrays/mappings/functions/etc, native true
Unicode strings, BSD socket services, and cross-platform support with
a bare minimum of *ix/Win/Mac). There's just no point restricting it
to a machine word, especially since "machine word" varies from machine
to machine.

Incidentally, if you specifically *want* wrap-around behaviour, you
can perform modulo arithmetic. Store everything as unsigned, and after
every operation, take the value modulo 2**64; then for display, if you
need it to be signed, check if it's >= 2**63, and if so, subtract
2**64. (Or use 32, 31, and 32, or whatever word size you want.) That's
a decent simulation of a simple twos-comp integer.

ChrisA
 
D

Dave Angel

Philip Red said:
Hi everyone. First of all sorry if my english is not good.
I have a question about something in Python I can not explain:
in every programming language I know (e.g. C#) if you exceed the max-value of a certain type (e.g. a long-integer) you get an overflow. Here is a simple example in C#:

static void Main(string[] args)
{
Int64 x = Int64.MaxValue;
Console.WriteLine(x); // output: 9223372036854775807
x = x * 2;
Console.WriteLine(x); // output: -2 (overflow)
Console.ReadKey();
}

Now I do the same with Python:

x = 9223372036854775807
print(type(x)) # <class 'int'>
x = x * 2 # 18446744073709551614
print(x) # <class 'int'>
print(type(x))

and I get the right output without overflow and the type is always a 'int'.
How does Python manages internally the types and their values? Where are they stored?

Thank you for your help :)

In python, every value is an object. Some, like lists, can grow
over time, and there's no specific upper limit in size. Others,
like int, or string, are immutable, so the constructor can
calculate just how much space is needed.

In java, and I believe in C#, they make a distinction between
unboxed and boxed integers. The former are NOT objects, and have
a specific upper bound, generally based on some power of
2.
 
R

Roy Smith

Chris Angelico said:
The Python integer type stores arbitrary precision.

Which is not only really cool, but terribly useful for solving many
Project Euler puzzles :)
 
G

Gregory Ewing

Chris said:
(Which
means, no, Python cannot represent Graham's Number in an int. Sorry
about that.)

This is probably a good thing. I'm told that any computer
with enough RAM to hold Graham's number would, from entropy
considerations alone, have enough mass to become a black
hole.
 
R

random832

The Python integer type stores arbitrary precision. It's not a machine
word, like the C# integer types (plural, or does it have only one?

C# has the usual assortment of fixed-width integer types - though by
default they throw exceptions on overflow instead of wrapping around -
and a BigInteger type in the library.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top