The smallest and largest values of numeric types

fumanchu fumanchu at amor.org
Tue Apr 17 17:53:46 CEST 2007


On Apr 17, 7:12 am, t... at finland.com wrote:
> How can I determine the smallest and largest values
> of numeric types (for example int) possible in my
> system? I think there exists a function for this task
> but I don't know it.

This should work for ints:

import sys
print sys.maxint


For floats, just try until it fails. This is from
http://projects.amor.org/geniusql/browser/trunk/geniusql/adapters.py#l41

# Determine max binary digits for float on this system. Crude but
effective.
maxfloat_digits = 2
while True:
    L = (2 ** (maxfloat_digits + 1)) - 1
    if int(float(L)) != L:
        break
    maxfloat_digits += 1

Note this will get you the number of binary digits, which is not a
simple matter to translate to maximum and minimum values, since floats
are inexact numerics. IEEE 754 floats are stored with one bit for the
sign, some bits for the exponent (which is biased) and some for the
significand. See http://babbage.cs.qc.edu/IEEE-754/32bit.html for a
visual example of how it works.


Robert Brewer
System Architect
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list