Re: The smallest and largest values of numeric types



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

.



Relevant Pages

  • Re: In the Matter of Herb Schildt: a Detailed Analysis of "C: The Complete Nonsense"
    ... not an int; it is an unsigned integral type, thus, one of unsigned ... know, as Seebach seems not to, that all ints are floats and all floats ... Herb seems to have remembered something more important ...
    (comp.lang.c)
  • Re: In the Matter of Herb Schildt: a Detailed Analysis of "C: The Complete Nonsense"
    ... not an int; it is an unsigned integral type, thus, one of unsigned ... know, as Seebach seems not to, that all ints are floats and all floats ... Illuminati that you must declare main as int, ...
    (comp.lang.c)
  • Re: RAD vs. performance
    ... either specific (e.g. int) or generic. ... for ints and floats is a fairly rare occurance and probably wouldn't be ... contend that assuming interfaces are equivalent and inferring the most ... Dynamic dispatch doesn't handle the case of dispatching over more ...
    (comp.lang.misc)
  • Re: Struct usage and varying sizes of h, l, etc
    ... I'm looking at the struct module for binary packing of ints and floats. ... It's been many years since I looked at C, but I seem to remember that the data type sizes were not fixed -- for example, an int might be two byes on one machine, and four bytes on the next. ... Single-precision floats are always four bytes and double-precision floats are always eight bytes. ...
    (comp.lang.python)
  • Re: dynamic vs. static: the age-old debate
    ... represented in a consistent way (e.g. there is no Int module defining ... floats without many of the semantic differences (e.g. addition is not ...
    (comp.lang.misc)