473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

maximum value for long?

Hello all,

I have been programming in Python only for a month or so and have stumbled
over a "problem" that I could not solve through rtfm.

$ python
Python 2.2.2 (#1, Nov 6 2003, 09:19:47)
[GCC 3.3] on irix646
Type "help", "copyright", "credits" or "license" for more information.
ulong_max=18446744073709551615
type(ulong_max) <type 'long'> print ulong_max 18446744073709551615 very_long_long=18446744073709551615184467440737095 51615
type(very_long_long) <type 'long'> print very_long_long 1844674407370955161518446744073709551615 result=very_long_long/15
type(result) <type 'long'> print result 122978293824730344101229782938247303441


I have set ulong_max above to the C compilers ULONG_MAX definition from limits.h
I would have expected Python to complain when setting the value of the variable
very_long_long to anything greater than ULONG_MAX.
Since I am currently doing computations with integer values *much* bigger than
ULONG_MAX, this behaviour suites me, but I am wondering if this is a "standard"
Python behaviour since I do not want my scripts to become dependant on some
obscure non-standard feature...

Could any kind Python-Guru please shed some light on this?
Also, if Python really can handle longs that are bigger than the machines native
longs, the interpreter has to do some sort of arbitrary precision math
somewhere. Could you please point me to the according interpreter's source file(s)?

TIA + Regards,
S.Susnjar
Jul 18 '05 #1
3 17822

S> I have set ulong_max above to the C compilers ULONG_MAX definition
S> from limits.h I would have expected Python to complain when setting
S> the value of the variable very_long_long to anything greater than
S> ULONG_MAX. Since I am currently doing computations with integer
S> values *much* bigger than ULONG_MAX, this behaviour suites me, but I
S> am wondering if this is a "standard" Python behaviour since I do not
S> want my scripts to become dependant on some obscure non-standard
S> feature...

Python has a builtin unbounded long type. In 2.3, most (all?) operations on
integers will return longs if the op would overflow the machine's int
(typically 32- or 64-bits):
print 17**100

11088993727807836413061117158750949664360171676498 79524402769841278887580501366697712424694256005093 589248451503068397608001

S> Also, if Python really can handle longs that are bigger than the
S> machines native longs, the interpreter has to do some sort of
S> arbitrary precision math somewhere. Could you please point me to the
S> according interpreter's source file(s)?

Look at Objects/longobject.c in the source distribution.

Skip

Jul 18 '05 #2

"S.Susnjar" <fo***@gmx.de> wrote in message
news:ba*************************@posting.google.co m...
Hello all,
Hi.
I have been programming in Python only for a month or so and have stumbled over a "problem" that I could not solve through rtfm.


Ref Man 3.2 The standard type hierarchy
"
There are two types of integers:

Plain integers
These represent numbers in the range -2147483648 through 2147483647.
(The range may be larger on machines with a larger natural word size,
but not smaller.) When the result of an operation would fall outside
this range, the exception OverflowError is raised. For the purpose of
shift and mask operations, integers are assumed to have a binary, 2's
complement notation using 32 or more bits, and hiding no bits from the
user (i.e., all 4294967296 different bit patterns correspond to
different values).

Long integers
These represent numbers in an unlimited range, subject to available
(virtual) memory only. For the purpose of shift and mask operations, a
binary representation is assumed, and negative numbers are represented
in a variant of 2's complement which gives the illusion of an infinite
string of sign bits extending to the left.
"
In short, Python ints are C longs, while Python longs are extended or
arbitrary precision, and hence truly long. Your confusion over the
two meanings of 'long' is not unique among people with a C background
;-).

Terry J. Reedy

Jul 18 '05 #3
On Tue, 18 Nov 2003 21:45:45 -0500, "Terry Reedy" <tj*****@udel.edu> wrote:

"S.Susnjar" <fo***@gmx.de> wrote in message
news:ba*************************@posting.google.c om...
Hello all,
Hi.
I have been programming in Python only for a month or so and have

stumbled
over a "problem" that I could not solve through rtfm.


Ref Man 3.2 The standard type hierarchy
"
There are two types of integers:

Plain integers
These represent numbers in the range -2147483648 through 2147483647.
(The range may be larger on machines with a larger natural word size,
but not smaller.) When the result of an operation would fall outside
this range, the exception OverflowError is raised. For the purpose of


Seems out of date re version 2.3.2:
x = 2**30
x 1073741824 type(x) <type 'int'> x+x 2147483648L type(x+x)

<type 'long'>

shift and mask operations, integers are assumed to have a binary, 2's
complement notation using 32 or more bits, and hiding no bits from the
user (i.e., all 4294967296 different bit patterns correspond to
different values).

Long integers
These represent numbers in an unlimited range, subject to available
(virtual) memory only. For the purpose of shift and mask operations, a
binary representation is assumed, and negative numbers are represented
in a variant of 2's complement which gives the illusion of an infinite
string of sign bits extending to the left.
" Though <rant>hex of negative numbers is slated for ugliness (IMO) in version 2.4
in the form of '-' prefixing the hex of the absolute value -- making it useless
for the normal bit pattern visualization that one would like when looking at bitwise
operations. Ugh! Barf!</rant>.
In short, Python ints are C longs, while Python longs are extended or
arbitrary precision, and hence truly long. Your confusion over the
two meanings of 'long' is not unique among people with a C background
;-).


Regards,
Bengt Richter
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Fred L. Kleinschmidt | last post by:
I need to know the largets value representable in a variable. However, I do not know the variable's true type - only that it is some kind of int. It may be any of the following: #typedef Newtype...
1
by: Net Virtual Mailing Lists | last post by:
Hello, Lets say I have data like this: value1|value2|value3|value4|....|value(N) ------|------|------|------|----|-------- 100 | 200 | 300 | 400 | | 10 | 20 | | 40 | ...
5
by: Sunny123 | last post by:
hello i am trying to find the maximum value of a function and where it occur. i.e there is an array x |y ============= | |
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
3
by: Madmartigan | last post by:
Hello I have the following task but am battling with the final output. How do I keep two different vectors in sync and how would I retrieve the index for the maximum value of one of the vectors??...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
6
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm using the VScrollBar and set it as follow: m_vScrollBar.Minimum = -19602; m_vScrollBar.Maximum = 0; m_vScrollBar.SmallChange = 1; m_vScrollBar.LargeChange = 1089; m_vScrollBar.Value =...
2
choke
by: choke | last post by:
I need to write a function in devc++ that creates an array of n integers, each element is equal to n*n+i*i-n*i where i is from 0 to n-1 as the array index. Within the same function I need to find...
0
by: Fredrik Lundh | last post by:
maurizio wrote: What kind of file is it? Did you pick numpy because you want to do matrix operations (beyond just finding a maximum value), or was it just the first thing you stumbled upon...
1
by: maurizio | last post by:
thank you for your answer actually i've to do some statistics (maximum,minimum,mean,standard deviation,....) of a file of data in which each column is a particular type of data. (the file is a tab...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.