【问题标题】:What is the range of values a float can have in Python?浮点数在 Python 中的取值范围是多少?
【发布时间】:2010-12-22 14:08:42
【问题描述】:

它在python中的最小值和最大值是什么?

【问题讨论】:

标签: python floating-point


【解决方案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)

最小的是sys.float_info.min (2.2250738585072014e-308),最大的是sys.float_info.max (1.7976931348623157e+308)。其他属性见documentation

sys.float_info.min 是标准化的最小值。您通常可以将非规范化的最小值设为sys.float_info.min * sys.float_info.epsilon。请注意,此类数字的表示精度有所降低。正如预期的那样,非规范化的最小值小于规范化的最小值。

【讨论】:

    【解决方案2】:

    Python 使用双精度浮点数,它可以保存大约 10 到 -308 到 10 到 308 次方的值。

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

    在 Python 提示符下尝试这个实验:

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

    10 到 309 的幂是溢出,但 10 到 308 不是。 QED。

    实际上,您可能可以通过denormals 获得小于 1e-308 的数字,但这会对性能造成重大影响。我发现 Python 能够处理 1e-324 但在 1e-325 上下溢并返回 0.0 作为值。

    【讨论】:

    • 那么 1e+308 应该怎么比无穷大(见问题)? ;)
    • @sfussenegger:答案“-inf and +inf”当然是对这个问题的有效回答。请将其作为单独的答案发布。
    【解决方案3】:

    从技术上讲,最小的浮动是-inf,最大的浮动是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
    

    带有* 的数字取决于机器和实现。

    【讨论】:

      【解决方案4】:

      作为对先前答案的一种理论上的补充,我想提一下“神奇”值 ±308 直接来自浮点数的二进制表示。 Double precision floats 的形式为 ±c*2**q,带有一个“小”小数值 c (~1),而 q 是一个用 11 个二进制数字(包括 1 位作为其符号)写入的整数。 2**(2**10-1) 有 308 个(十进制)数字这一事实解释了 10**±308 在极端浮点值中的出现。

      用 Python 计算:

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

      【讨论】:

        【解决方案5】:

        只是在玩;这是一种找到最小和最大正浮点数的算法方法,希望在float("+inf") 可以接受的任何python 实现中:

        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
        

        就我而言,

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

        所以使用了非正规。

        【讨论】:

          【解决方案6】:

          看到这个post

          帖子的相关部分:

          [2]中:导入种类 在 [3] 中: kind.default_float_kind.M 种类.default_float_kind.MAX 种类.default_float_kind.MIN kind.default_float_kind.MAX_10_EXP kind.default_float_kind.MIN_10_EXP kind.default_float_kind.MAX_EXP kind.default_float_kind.MIN_EXP 在 [3] 中:种类.default_float_kind.MIN 出[3]:2.2250738585072014e-308

          【讨论】:

          • 请注意,Numeric 在很大程度上已被 NumPy 取代。不过,我想知道是否存在更现代的 kind 模块等价物……
          猜你喜欢
          • 1970-01-01
          • 2023-03-25
          • 2022-01-12
          • 1970-01-01
          • 2014-09-23
          • 2011-03-29
          • 1970-01-01
          • 2020-03-16
          • 1970-01-01
          相关资源
          最近更新 更多