x = float(1)
small = x
while small > 0:
real_small = small
small = small/2
print(small) # 0.0
print(real_small) # 5e-324
This will get you smallest possible(almost) float. Hope you can now figure out largest too similarly.
The best and easiest solution for finding smallest and largest possible float is:
import numpy as np
import math
>>> # Smallest
>>> np.nextafter(0, 1)
5e-324
>>> # Largest
>>> np.nextafter(math.inf, 0)
1.7976931348623157e+308
>>> np.nextafter(np.float64(math.inf), 0)
1.7976931348623157e+308
Another solution is using sys.float_info:
>>> import sys
>>> sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
All methods provides same max value, but for min value sys.float_info provides different value than others, I am not sure why, maybe someone else can enlighten me regarding this.
nextafter(x, y) source
The nextafter(), nextafterf(), and nextafterl() functions return the
next representable floating-point value following x in the direction
of y.
If y is less than x, these functions will return the largest
representable number less than x.
If x equals y, the functions return y.
numpy.nextafter(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = < ufunc 'nextafter'> source
Return the next floating-point value after x1 towards x2, element-wise.