Re: The smallest and largest values of numeric types
- From: fumanchu <fumanchu@xxxxxxxx>
- Date: 17 Apr 2007 08:53:46 -0700
On Apr 17, 7:12 am, t...@xxxxxxxxxxx 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@xxxxxxxx
.
- Follow-Ups:
- Re: The smallest and largest values of numeric types
- From: Michael Hoffman
- Re: The smallest and largest values of numeric types
- References:
- Prev by Date: Re: subprocess confusion
- Next by Date: Re: rewrite for achieving speedup
- Previous by thread: Re: The smallest and largest values of numeric types
- Next by thread: Re: The smallest and largest values of numeric types
- Index(es):
Relevant Pages
|