Python Smallest Possible Positive Float

Question:

I got bored and used this code:

n=1
while n != 0:
    print(str(n)+" / 2 = "+str(n/2))
    n/=2
for i in range(5, 0, -1):
    print("Value of "+str(i)+"e-324:")
    print(float(str(i)+"e-324"))

The output implies that 5e-324 is the smallest positive float possible.

Am I right?

Answers:

The documentation for sys.float_info says that this is the way to get the smallest possible float:

>>> math.ulp(0.0)
5e-324

The value of sys.float_info.min (part of a previous answer that I deleted) gives the smallest normalized float, a much bigger value.

Answered By: Frank Yellin

Another way to approach this problem is to look into the details of how floats work, e.g. on Wikipedia. Acknowledging that python uses IEEE double precision floats on most platforms, and slogging through the relevant bits of https://en.wikipedia.org/wiki/Double-precision_floating-point_format#Double-precision_examples:

+2−1022 × 2−52 = 2−1074 ≈ 4.9406564584124654 × 10−324 (Min. subnormal positive double)

Answered By: Mad Physicist
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.