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

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

What are its smallest and biggest values in python?

6条回答
  •  情书的邮戳
    2020-11-29 04:37

    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
    

提交回复
热议问题