Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What are its smallest and biggest values in python?

like image 293
devoured elysium Avatar asked Dec 02 '09 21:12

devoured elysium


People also ask

Is there a range for floats in Python?

The Python range() works only with integers. It doesn't support the float type, i.e., we cannot use floating-point/decimal value in any of its arguments.

What is the range of values for float?

Since the high-order bit of the mantissa is always 1, it is not stored in the number. This representation gives a range of approximately 3.4E-38 to 3.4E+38 for type float.

What is the maximum float value in Python?

We found out the maximum value that a float variable can hold with the sys. float_info. max in the code above. The output shows that the upper limit of a float variable is 1.7976931348623157e+308.

What is the value of float in Python?

Python float values are represented as 64-bit double-precision values. 1.8 X 10308 is an approximate maximum value for any floating-point number. If it exceeds or exceeds the max value, Python returns an error with string inf (infinity).


5 Answers

>>> 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.

like image 146
Denis Otkidach Avatar answered Oct 18 '22 03:10

Denis Otkidach


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 
like image 33
Juha Syrjälä Avatar answered Oct 18 '22 01:10

Juha Syrjälä


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
like image 42
Eric O Lebigot Avatar answered Oct 18 '22 01:10

Eric O Lebigot


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.

like image 7
steveha Avatar answered Oct 18 '22 02:10

steveha


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.

like image 6
Benoît P Avatar answered Oct 18 '22 02:10

Benoît P