Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64519 > unrolled thread

No overflow in variables?

Started byPhilip Red <filippo.biolcati@googlemail.com>
First post2014-01-22 10:09 -0800
Last post2014-01-23 20:36 +1100
Articles 11 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  No overflow in variables? Philip Red <filippo.biolcati@googlemail.com> - 2014-01-22 10:09 -0800
    Re: No overflow in variables? Larry Martell <larry.martell@gmail.com> - 2014-01-22 11:18 -0700
    Re:No overflow in variables? Dave Angel <davea@davea.name> - 2014-01-22 13:26 -0500
    Re: No overflow in variables? Philip Red <filippo.biolcati@googlemail.com> - 2014-01-22 10:32 -0800
    Re: No overflow in variables? Chris Angelico <rosuav@gmail.com> - 2014-01-23 05:26 +1100
      Re: No overflow in variables? Roy Smith <roy@panix.com> - 2014-01-22 15:55 -0500
      Re: No overflow in variables? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-01-23 11:22 +1300
    Re: No overflow in variables? Philip Red <filippo.biolcati@googlemail.com> - 2014-01-22 10:48 -0800
    Re: No overflow in variables? random832@fastmail.us - 2014-01-22 18:13 -0500
    Re: No overflow in variables? Christian Heimes <christian@python.org> - 2014-01-23 10:14 +0100
    Re: No overflow in variables? Chris Angelico <rosuav@gmail.com> - 2014-01-23 20:36 +1100

#64519 — No overflow in variables?

FromPhilip Red <filippo.biolcati@googlemail.com>
Date2014-01-22 10:09 -0800
SubjectNo overflow in variables?
Message-ID<dd6a72bf-b1c4-4868-9cfe-76dfac8b8787@googlegroups.com>
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 :)

[toc] | [next] | [standalone]


#64520

FromLarry Martell <larry.martell@gmail.com>
Date2014-01-22 11:18 -0700
Message-ID<mailman.5844.1390415068.18130.python-list@python.org>
In reply to#64519
On Wed, Jan 22, 2014 at 11:09 AM, Philip Red
<filippo.biolcati@googlemail.com> wrote:
> 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/

[toc] | [prev] | [next] | [standalone]


#64521

FromDave Angel <davea@davea.name>
Date2014-01-22 13:26 -0500
Message-ID<mailman.5845.1390415074.18130.python-list@python.org>
In reply to#64519
 Philip Red <filippo.biolcati@googlemail.com> Wrote in message:
> 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.


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#64523

FromPhilip Red <filippo.biolcati@googlemail.com>
Date2014-01-22 10:32 -0800
Message-ID<77843ceb-716e-4925-a183-f769c1b630ad@googlegroups.com>
In reply to#64519
Thank you for your answers!

[toc] | [prev] | [next] | [standalone]


#64524

FromChris Angelico <rosuav@gmail.com>
Date2014-01-23 05:26 +1100
Message-ID<mailman.5846.1390415644.18130.python-list@python.org>
In reply to#64519
On Thu, Jan 23, 2014 at 5:09 AM, Philip Red
<filippo.biolcati@googlemail.com> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#64534

FromRoy Smith <roy@panix.com>
Date2014-01-22 15:55 -0500
Message-ID<roy-49F575.15553522012014@news.panix.com>
In reply to#64524
In article <mailman.5846.1390415644.18130.python-list@python.org>,
 Chris Angelico <rosuav@gmail.com> wrote:

> The Python integer type stores arbitrary precision.

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

[toc] | [prev] | [next] | [standalone]


#64539

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-01-23 11:22 +1300
Message-ID<bkaukoFb7f6U1@mid.individual.net>
In reply to#64524
Chris Angelico wrote:
> (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.

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#64526

FromPhilip Red <filippo.biolcati@googlemail.com>
Date2014-01-22 10:48 -0800
Message-ID<734002b1-c400-4f11-b7d7-71d701f6a773@googlegroups.com>
In reply to#64519
Thank you ChrisA

[toc] | [prev] | [next] | [standalone]


#64542

Fromrandom832@fastmail.us
Date2014-01-22 18:13 -0500
Message-ID<mailman.5852.1390432414.18130.python-list@python.org>
In reply to#64519
On Wed, Jan 22, 2014, at 13:26, Chris Angelico wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#64585

FromChristian Heimes <christian@python.org>
Date2014-01-23 10:14 +0100
Message-ID<mailman.5877.1390468486.18130.python-list@python.org>
In reply to#64519
On 22.01.2014 19:26, Chris Angelico wrote:
> 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.

Small correction: Python isn't using GMP. Python uses its own
implementation.

[toc] | [prev] | [next] | [standalone]


#64587

FromChris Angelico <rosuav@gmail.com>
Date2014-01-23 20:36 +1100
Message-ID<mailman.5879.1390469782.18130.python-list@python.org>
In reply to#64519
On Thu, Jan 23, 2014 at 8:14 PM, Christian Heimes <christian@python.org> wrote:
> On 22.01.2014 19:26, Chris Angelico wrote:
>> 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.
>
> Small correction: Python isn't using GMP. Python uses its own
> implementation.

Okay, wasn't sure. I've seen others that use GMP (including Pike,
which can also use arbitrary-precision floats if you wish). Wrong in
the specifics, right in the concept. Thanks for the correction.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web