float limits

Dan Bishop danb_83 at yahoo.com
Thu Jan 31 07:03:32 CET 2002


"Terry Reedy" <tjreedy at home.com> wrote in message news:<qXT58.13025$X64.4429530 at news1.rdc2.pa.home.com>...
> "Alexandre Fayolle" <alf at leo.logilab.fr> wrote in message
>  > Is there a way of knowing what the value of the smallest positive
>  float
> > available on a given machine from within python? A bit like
>  sys.maxint,
> > but for mindouble. Similarly, I'd like to know the value of the
>  biggest
> > positive float, maxfloat. If the solution is portable, that's all
> the better.
> 
> There is a standard algorithm that numerical analysts use to calculate
> machine epsilon:
> I believe its something like
> 
> eps1 = 1.0
> eps2 = 0.5
> while (1.0+eps2 != 1.0):
>    eps1 = eps2
>    eps2 /= 2.0
> 
> I think adding 0.0 instead of 1.0 and comparing might be theoretically
> just as good, but good compilers will optimize away addition.  Simply
> testing eps2 against 0.0 fails if min/2 = min instead of 0.

Adding 1.0 should give you machine epsilon; adding 0.0 will give you
the underflow level.

btw, for IEEE 754 double-precision:

machine epsilon == 1.1102230246251575E-16
underflow level == 4.9E-324
overflow level == 1.7976931348623143E308



More information about the Python-list mailing list