python language: infimum and supremum of integers

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Apr 23 07:44:54 EDT 2003


Grégoire Dooms <dooms at info.ucl.ac.be> wrote in 
news:mailman.1051093416.23728.python-list at python.org:

> I need a lower and higher integer for initializing lower and higher
> bounds in an algorithm.  I know integers have no limits(maximum and
> minimum) in python and infimum and supremum would do the job too.
> 
> I empirically discovered (Python 2.2.2) that None could be considered an
> infimum and any string could be considered a supremum.
> e.g "a">int(x) is always true and None<int(x) is always false
> 
> But this fact is not stated in the language reference. 
> Citing http://www.python.org/doc/current/ref/comparisons.html : 
> "The objects need not have the same type. If both are numbers, they are
> converted to a common type. Otherwise, objects of different types always
> compare unequal, and are ordered consistently but arbitrarily."
> 
> Could/should that order be hardcoded in the language ?

Perhaps the wording should be changed 'ordered consistently but arbitrarily 
within a single instance of the interpreter'. Sorry, there are no 
guarantees what relative ordering objects of different types will have on 
different systems or even on the same system but different processes.
Builtin types may tend to keep the same relative ordering, but even that 
has changed between Python versions. (On my copy of Python 2.2.2, I just 
got "a">1 true, None<1 true which is not what you said you got.)

Perhaps you could modify your algorithm to not require pre-initialising 
with upper and lower bounds? For example, if your algorithm generates a 
series of values and you need to know the highest and lowest that are 
generated, then keeping a list of the generated values and using the 
builtin min and max functions on the list is likely to be faster than doing 
the comparisons by hand until you get to a very large set of values.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list