<i id='y65RX'><tr id='y65RX'><dt id='y65RX'><q id='y65RX'><span id='y65RX'><b id='y65RX'><form id='y65RX'><ins id='y65RX'></ins><ul id='y65RX'></ul><sub id='y65RX'></sub></form><legend id='y65RX'></legend><bdo id='y65RX'><pre id='y65RX'><center id='y65RX'></center></pre></bdo></b><th id='y65RX'></th></span></q></dt></tr></i><div id='y65RX'><tfoot id='y65RX'></tfoot><dl id='y65RX'><fieldset id='y65RX'></fieldset></dl></div>
      <legend id='y65RX'><style id='y65RX'><dir id='y65RX'><q id='y65RX'></q></dir></style></legend>

        <tfoot id='y65RX'></tfoot>
      1. <small id='y65RX'></small><noframes id='y65RX'>

          <bdo id='y65RX'></bdo><ul id='y65RX'></ul>
      2. 为什么在 python 中非常大的浮点值之间的比较会失败?

        Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
      3. <tfoot id='Rh2hx'></tfoot>

          • <bdo id='Rh2hx'></bdo><ul id='Rh2hx'></ul>

            1. <small id='Rh2hx'></small><noframes id='Rh2hx'>

              <legend id='Rh2hx'><style id='Rh2hx'><dir id='Rh2hx'><q id='Rh2hx'></q></dir></style></legend>
                <tbody id='Rh2hx'></tbody>
                  <i id='Rh2hx'><tr id='Rh2hx'><dt id='Rh2hx'><q id='Rh2hx'><span id='Rh2hx'><b id='Rh2hx'><form id='Rh2hx'><ins id='Rh2hx'></ins><ul id='Rh2hx'></ul><sub id='Rh2hx'></sub></form><legend id='Rh2hx'></legend><bdo id='Rh2hx'><pre id='Rh2hx'><center id='Rh2hx'></center></pre></bdo></b><th id='Rh2hx'></th></span></q></dt></tr></i><div id='Rh2hx'><tfoot id='Rh2hx'></tfoot><dl id='Rh2hx'><fieldset id='Rh2hx'></fieldset></dl></div>
                1. 本文介绍了为什么在 python 中非常大的浮点值之间的比较会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  据我了解,sys.float_info.max 是最大可能的浮点值.但是,似乎无法比较如此大的值.

                  In my understanding, sys.float_info.max is the largest possible float value. However, it seems that comparing such large values fail.

                  import math
                  import sys
                  
                  m = sys.float_info.max                        # type 'float'
                  
                  m == m                                        # True
                  m < m                                         # False
                  m > m                                         # False
                  
                  m == m-1.0                                    # True
                  m < m-1.0                                     # False
                  m > m-1.0                                     # False
                  
                  m == m-1e100                                  # True
                  m < m-1e100                                   # False
                  m > m-1e100                                   # False
                  
                  m == m-1e300                                  # False
                  m > m-1e300                                   # True
                  m < m-1e300                                   # False
                  

                  我认为这是因为精度有限?如果可以,在什么数值范围内可以安全操作?

                  I assume that's because of the limited precision? If so, in what numerical range can i operate safely?

                  以上代码使用 Python 3.5.2 运行.

                  推荐答案

                  在运行 Python 的典型机器上,有 53 位精度可用于 Python 浮点数.如果您尝试更进一步,Python 会消除最小的部分,以便正确表示数字.

                  On a typical machine running Python, there are 53 bits of precision available for a Python float. If you try to go further, Python will eliminate the smallest part so the number can be properly represented.

                  因此,值 1 被吸收或取消,以便能够表示您尝试计算的高值.

                  So the value 1 is absorbed or cancelled to be able to represent the high value you're trying to compute.

                  限制是通过减去(或添加)乘以float epsilon的值来获得的.

                  The limit is obtained by subtracting (or adding) the value multiplied by float epsilon.

                  在我的机器上:

                  maxfloat == 1.7976931348623157e+308
                  epsilon == 2.220446049250313e-16
                  

                  示例测试代码

                  import math
                  import sys
                  
                  m = sys.float_info.max                        # type 'float'
                  eps = sys.float_info.epsilon
                  
                  print(m == m-(m*(eps/10)))   # True
                  print(m == m-(m*eps))        # False
                  

                  m*eps 是您必须减去以使比较失败的最小值.它总是相对于 m 值.

                  m*eps is the smallest value you have to subtract to make comparison fail. It's always relative to the m value.

                  这篇关于为什么在 python 中非常大的浮点值之间的比较会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                  removing an instance of an object in python list(删除python列表中的对象实例)
                    • <bdo id='UGPji'></bdo><ul id='UGPji'></ul>
                          <tbody id='UGPji'></tbody>

                          <small id='UGPji'></small><noframes id='UGPji'>

                          <tfoot id='UGPji'></tfoot>

                          <legend id='UGPji'><style id='UGPji'><dir id='UGPji'><q id='UGPji'></q></dir></style></legend>
                          • <i id='UGPji'><tr id='UGPji'><dt id='UGPji'><q id='UGPji'><span id='UGPji'><b id='UGPji'><form id='UGPji'><ins id='UGPji'></ins><ul id='UGPji'></ul><sub id='UGPji'></sub></form><legend id='UGPji'></legend><bdo id='UGPji'><pre id='UGPji'><center id='UGPji'></center></pre></bdo></b><th id='UGPji'></th></span></q></dt></tr></i><div id='UGPji'><tfoot id='UGPji'></tfoot><dl id='UGPji'><fieldset id='UGPji'></fieldset></dl></div>