smallest float number

Chad Netzer cnetzer at mail.arc.nasa.gov
Wed Feb 12 18:48:29 EST 2003


On Wed, 2003-02-12 at 15:13, gabor wrote:
> hi,
> 
> is there an easy way to write the smallest float number?

short answer:

inf = float( "inf" )
min_float = -inf

Okay, that's just a joke

> i just don't want to use things like
> a = -100000000000000000000.0

This isn't even close.  One way would be to write an extension in C that
wraps DBL_MIN.

Another would be to keep subtracting an amount from a number; when it
goes to "-inf", subtract smaller amounts (from the last not -inf).  When
you can't subtract anymore, you have the most negative python float that
is supported on your system (this is a hand-waving and vague algorithm
description, because I don't recommend it)

An easy suggestion, assuming you only care about systems with 64 bit
doubles, is this:

min_float = -1.79e+308

Check that this isn't -inf on your system, and you are probably good. 
It isn't super portable, though.  You could choose between a variety of
constants depending on what your system supports.

So, the best idea is probably to re-evaluate why you need this.  If it
is critical, you will need to put more care into choosing the constant,
and should probably change your algorithms to not need it.

If it isn't critical, just use the above constant (it is at least as
good as what you have been using, for example)

> thanks,
> gabor

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)







More information about the Python-list mailing list