python - 如何在纯Python中添加/减去浮点值的最小可能值[解释有何不同]?

标签 python python-2.7

这个问题仅针对 Python 程序员。这个问题不重复不起作用 Increment a python floating point value by the smallest possible amount请参阅底部说明。

<小时/>

我想为任何浮点添加/减去一些最小值,这将改变此浮点值大约 mantissa/significant part 的一位。如何在纯Python中有效地计算这么小的数字。

例如我有这样的 x 数组:

xs = [1e300, 1e0, 1e-300]

它产生最小值的函数是什么?所有断言都应该有效。

for x in xs:
  assert x < x + smallestChange(x)
  assert x > x - smallestChange(x)

考虑一下 1e308 + 1 == 1e308 因为 1 对于尾数来说确实意味着 0 所以 `smallestChange' 应该是动态的。

纯Python解决方案将是最好的。

<小时/>

为什么这与 Increment a python floating point value by the smallest possible amount 不重复 - 两个简单的测试证明了这一点,结果无效。

(1) Increment a python floating point value by the smallest possible amount 中未回答该问题区别:

<强> Increment a python floating point value by the smallest possible amount只是不行试试这个代码:

import math
epsilon  = math.ldexp(1.0, -53) # smallest double that 0.5+epsilon != 0.5
maxDouble = float(2**1024 - 2**971)  # From the IEEE 754 standard
minDouble  = math.ldexp(1.0, -1022) # min positive normalized double
smallEpsilon  = math.ldexp(1.0, -1074) # smallest increment for doubles < minFloat
infinity = math.ldexp(1.0, 1023) * 2

def nextafter(x,y):    
    """returns the next IEEE double after x in the direction of y if possible"""
    if y==x:
       return y         #if x==y, no increment

    # handle NaN
    if x!=x or y!=y:
        return x + y       

    if x >= infinity:
        return infinity

    if x <= -infinity:
        return -infinity

    if -minDouble < x < minDouble:
        if y > x:
            return x + smallEpsilon
        else:
            return x - smallEpsilon  

    m, e = math.frexp(x)        
    if y > x:
        m += epsilon
    else:
        m -= epsilon

    return math.ldexp(m,e)  
print nextafter(0.0, -1.0), 'nextafter(0.0, -1.0)'
print nextafter(-1.0, 0.0), 'nextafter(-1.0, 0.0)'

Increment a python floating point value by the smallest possible amount的结果无效:

>>> nextafter(0.0, -1)
0.0

应该是非零。

>>> nextafter(-1,0)
-0.9999999999999998

应为“-0.9999999999999999”。

(2) 没有被问到如何加/减最小值,而是被问到如何在特定方向上加/减值 - 提出的解决方案是需要知道 x 和 y。这里只要求知道x。

(3) 在 Increment a python floating point value by the smallest possible amount 中提出解决方案在边界条件下不起作用。

最佳答案

>>> (1.0).hex()
'0x1.0000000000000p+0'
>>> float.fromhex('0x0.0000000000001p+0')
2.220446049250313e-16
>>> 1.0 + float.fromhex('0x0.0000000000001p+0')
1.0000000000000002
>>> (1.0 + float.fromhex('0x0.0000000000001p+0')).hex()
'0x1.0000000000001p+0'

只需使用相同的符号和指数即可。

关于python - 如何在纯Python中添加/减去浮点值的最小可能值[解释有何不同]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20254934/

相关文章:

python - 2to3-2.7 和 2to3-3.1 有什么区别?

php - Python 对 PHP 生成并存储在 Mysql 中的盐进行乱码

python - "SyntaxError: Non-ASCII character"在运行 Python 代码

python - 为什么Python日志记录模块的源代码在try中引发异常?

Python:如何在列表列表中将相似列表分组在一起?

python - 如何在 dask 数据框中设置(计算)分区?

python - 将额外的字段添加到现有的 namedtuple 对象并解封

python - 在 Python 中用任意值替换命名的捕获组

python - 在python中使用scrapy执行Javascript提交表单函数

python - Mongoengine ...查询不在 ListField 中的内容?