了解sys.float_info以获取python中的最大浮点数

I'm trying to understand the information given by sys.float_info to understand what the maximum floats in Python are. On my computer, this gives me the following:

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

The docs give the following example (https://docs.python.org/3/library/sys.html#sys.float_info):

>>> import sys
>>> sys.float_info.dig
15
>>> s = '3.14159265358979'    # decimal string with 15 significant digits
>>> format(float(s), '.15g')  # convert to float and back -> same value
'3.14159265358979'
>>> s = '9876543211234567'    # 16 significant digits is too many!
>>> format(float(s), '.16g')  # conversion changes value
'9876543211234568'

但是,以下示例对我来说也很好,即使它也有16位有效数字(?):

>>> s = '.9876543211234567' 
>>> format(float(s), '.16g')
'0.9876543211234567'

Also, sys.float_info.min yields 2.2250738585072014e-308 which is is obviously a lot smaller and also has 17 significant digits, if I'm correct? How does that work when sys.float_info.dig = 15? Am I confusing something here?

How does sys.float_info.dig = 15 relate to the attributes sys.float_info.mant_dig and sys.float_info.radix? As far as I understand, if I were to represent some decimal number as a base-2 number (since sys.float_info.radix = 2), does sys.float_info.mant_dig then give me the maximum integer of the mantissa?

抱歉,如果我有些困惑,也许我对这里的数学基础不够坚定。任何帮助深表感谢!

评论