int/long unification hides bugs

Bengt Richter bokr at oz.net
Wed Oct 27 02:10:44 EDT 2004


On 25 Oct 2004 21:05:30 -0700, kartick_vaddadi at yahoo.com (kartik) wrote:

>> The question is how small is small? Less than 2**7? Less than 2**15? 
>> Less than 2**31? Less than 2**63? And what's the significance of powers 
>> of two? And what happens if you move from a 32 bit machine to a 64 bit 
>> one? (or a 1024 bit one in a hundred years time?)
>
>less than 2**31 most of the time & hardly ever greater than 2**63 - no
>matter if my machine is 32-bit, 64-bit or 1024-bit. the required range
>depends on the data   u want 2 store in the variable & not on the
>hardware.
r u pstg fm a cel fone?
Anyway, you might like Ada. Googling for ada reference manual gets

    http://www.adahome.com/rm95/
----
Examples (buried in lots of language lawyer syntax stuff, maybe there's
a lighter weight manual ;-)

(33)
    Examples of integer types and subtypes: 

(34)
       type Page_Num  is range 1 .. 2_000;
       type Line_Size is range 1 .. Max_Line_Size;
(35)
       subtype Small_Int   is Integer   range -10 .. 10;
       subtype Column_Ptr  is Line_Size range 1 .. 10;
       subtype Buffer_Size is Integer   range 0 .. Max;
(36)
       type Byte        is mod 256; -- an unsigned byte
       type Hash_Index  is mod 97;  -- modulus is prime

----

>
>>  > PEP 237 says, "It will give new Python programmers [...] one less
>>  > thing to learn [...]". i feel this is not so important as the quality
>>  > of code a programmer writes once he does learn the language.
>> 
>> The thing is, the int/long cutoff is arbitrary, determined soley by 
>> implemetation detail. 
>
>agreed, but it need not be that way. ints can be defined to be 32-bit
>(or 64-bit) on all architectures.
So what's your point? That you're used to 32 and 64 bit registers?
Is signed two's (decided that was possessive, but maybe it's plural?;-)
complement the specific flavor you like? And python should offer these
constrained integer types? Why don't you scratch your own itch and see
if it's just a passing brain mite ;-)

>
>
>> A much better idea is the judicious use of assertions.
>> 
>> assert x < 15000
>> 
>> Not only does it protect you from runaway numbers, it also documents 
>> what the expected range is, resulting in a much better "quality of code"
>
>such an assertion must be placed before avery assignment to the
>variable - & that's tedious. moreover, it can give u a false sense of
>security when u think u have it wherever needed but u've forgotten it
>somewhere.
>
>a 32-bit limit is a crude kind of assertion that u get for free, and
>one u expect should hold for most variables. for those few variables
>it doesn't, u can use a long.

If you are willing to make your variables exist in an object's attribute
name space, you can define almost any behavior you want. E.g., here's
a class that will make objects that will only allow integral values within
the limits you specify to be bound to names in the attribute space. Since it's
guaranteed on binding, the retrieval needs no test.

 >>> import sys
 >>> class LimitedIntSpace(object):
 ...     def __init__(self, lo=-sys.maxint-1, hi=sys.maxint):
 ...         self.__dict__[''] = (lo, hi)
 ...     def __setattr__(self, vname, value):
 ...         if not isinstance(value,(int, long)):
 ...             raise TypeError,'Only integral values allowed'
 ...         lo, hi = self.__dict__['']
 ...         if value < lo: raise ValueError, '%r < %r (lower limit)' %(value, lo)
 ...         if value > hi: raise ValueError, '%r > %r (high limit)' %(value, hi)
 ...         self.__dict__[vname] = value
 ...
 >>> i3_10 = LimitedIntSpace(3,10)
 >>> for i in range(16):
 ...     try: i3_10.x = i; print (i,i3_10.x),
 ...     except ValueError, e: print e
 ...
 0 < 3 (lower limit)
 1 < 3 (lower limit)
 2 < 3 (lower limit)
 (3, 3) (4, 4) (5, 5) (6, 6) (7, 7) (8, 8) (9, 9) (10, 10) 11 > 10 (high limit)
 12 > 10 (high limit)
 13 > 10 (high limit)
 14 > 10 (high limit)
 15 > 10 (high limit) 


It defaults to your favored 32-bit range ;-)

 >>> i32 = LimitedIntSpace()
 >>> i32.x = sys.maxint
 >>> i32.y = -sys.maxint-1
 >>> i32.x += 1
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 9, in __setattr__
 ValueError: 2147483648L > 2147483647 (high limit)
 >>> i32.y -= 1
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 8, in __setattr__
 ValueError: -2147483649L < -2147483648 (lower limit)


Regards,
Bengt Richter



More information about the Python-list mailing list