What is the range of values a float can have in Python?

What are its smallest and biggest values in python?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

>>> 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.2204460492503131e-16, radix=2, rounds=1)

The smallest is sys.float_info.min (2.2250738585072014e-308) and the biggest is sys.float_info.max (1.7976931348623157e+308). See documentation for other properties.

sys.float_info.min is the normalized min. You can usually get the denormalized min as sys.float_info.min * sys.float_info.epsilon. Note that such numbers are represented with a loss of precision. As expected, the denormalized min is less than the normalized min.

Method 2

See this post.

Relevant parts of the post:

In [2]: import kinds 
In [3]: kinds.default_float_kind.M 
kinds.default_float_kind.MAX         kinds.default_float_kind.MIN 
kinds.default_float_kind.MAX_10_EXP  kinds.default_float_kind.MIN_10_EXP 
kinds.default_float_kind.MAX_EXP     kinds.default_float_kind.MIN_EXP 
In [3]: kinds.default_float_kind.MIN 
Out[3]: 2.2250738585072014e-308 

Method 3

As a kind of theoretical complement to the previous answers, I would like to mention that the “magic” value ±308 comes directly from the binary representation of floats. Double precision floats are of the form ±c*2**q with a “small” fractional value c (~1), and q an integer written with 11 binary digits (including 1 bit for its sign). The fact that 2**(2**10-1) has 308 (decimal) digits explains the appearance of 10**±308 in the extreme float values.

Calculation in Python:

>>> print len(repr(2**(2**10-1)).rstrip('L'))
308

Method 4

Python uses double-precision floats, which can hold values from about 10 to the -308 to 10 to the 308 power.

http://en.wikipedia.org/wiki/Double_precision_floating-point_format

Try this experiment from the Python prompt:

>>> 1e308
1e+308
>>> 1e309
inf

10 to the 309 power is an overflow, but 10 to the 308 is not. QED.

Actually, you can probably get numbers smaller than 1e-308 via denormals, but there is a significant performance hit to this. I found that Python is able to handle 1e-324 but underflows on 1e-325 and returns 0.0 as the value.

Method 5

Technically speaking, the smallest float is -inf and the max float inf:

>>> (float('-inf')            #   negative infinity 
< -1.7976931348623157e+308    #*  smallest float that is not negative infinity 
< -4.9406564584124654e-324    #*  biggest negative float that is not zero
< 0                           #   zero duh
< 4.9406564584124654e-324     #*  smallest positive float that is not zero
< 1.7976931348623157e+308     #*  biggest float that is not positive infinity
< float('inf'))               #   positive infinity
True

numbers with * are machine-dependent and implementation-dependent.

Method 6

Just playing around; here is an algorithmic method to find the minimum and maximum positive float, hopefully in any python implementation where float("+inf") is acceptable:

def find_float_limits():
    """Return a tuple of min, max positive numbers
    representable by the platform's float"""

    # first, make sure a float's a float
    if 1.0/10*10 == 10.0:
        raise RuntimeError("Your platform's floats aren't")

    minimum= maximum= 1.0
    infinity= float("+inf")

    # first find minimum
    last_minimum= 2*minimum
    while last_minimum > minimum > 0:
        last_minimum= minimum
        minimum*= 0.5

    # now find maximum
    operands= []
    while maximum < infinity:
        operands.append(maximum)
        try:
            maximum*= 2
        except OverflowError:
            break
    last_maximum= maximum= 0
    while operands and maximum < infinity:
        last_maximum= maximum
        maximum+= operands.pop()

    return last_minimum, last_maximum

if __name__ == "__main__":
    print (find_float_limits()) # python 2 and 3 friendly

In my case,

$ python so1835787.py
(4.9406564584124654e-324, 1.7976931348623157e+308)

so denormals are used.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x