Home > Net >  Python Smallest Possible Positive Float
Python Smallest Possible Positive Float

Time:07-23

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?

CodePudding user response:

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.

CodePudding user response:

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)

  • Related