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

前端 未结 6 1953
我在风中等你
我在风中等你 2020-11-29 03:43

What are its smallest and biggest values in python?

6条回答
  •  情深已故
    2020-11-29 04:39

    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.

提交回复
热议问题