numeric limits in python?

Started by
3 comments, last by Kylotan 18 years, 7 months ago
Is there a python equivalent to std::numeric_limits<int>::max() (and min) in python? Thanks!
Advertisement
I don't think there exist such constants, no (of course, they could be hiding somewhere...)

Python ints are guaranteed to be 32 bits. When they overflow, they are automatically converted to longs, which are not limited in size (except by how much memory you have).

int_min would be -(2**31) and int_max would be 2**31-1

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Are they guaranteed to be 32 bits? Last time I checked for the C Python distribution they were implemented using longs so they are at least 32 bits, but may be greater, depending on the compiler used to build the Python interpreter.
Ok, I was hoping to use those values to initialize some data like:

def findMin( list ):
min = some_const_for_max

for s in list:
if s < min:
min = s

...

but that doesn't really make sense if there is no limits on integer numbers.

def findMin( list ):
min = list[0]

...
works just the same...


Thanks for the info

Alternatively, just use the built-in functions min() and max(). No need to write them yourself.

This topic is closed to new replies.

Advertisement