【问题标题】:Precision of the quotient of two double precision numbers两个双精度数的商的精度
【发布时间】:2018-06-08 12:39:16
【问题描述】:

假设我想计算 f(x) = cos(x) 在 x = pi/2 处的一阶导数。 df/dx = -sin(x),因此是 df/dx=-1。为此,我使用最简单的公式:

df/dx = (f(x+h)-f(x))/h + O(h)。

这里 O(h) 是误差,它与 h 成正比,因此在数学上 O(h) 随着 h 变为零而变为零。我知道在计算机中事情是不同的。

我是否应该期望如果我使用双精度,这应该给我 15 到 17 个十进制有效数字,我应该能够通过 ~10^-15 接近精确结果 df/dx = -1.0? IE。我应该找到 (df/dx)_numerical + 1.0 ~ 10^-15 吗?

这是我发现的不同 h 值的结果:

h 值:

[0.0001, 1e-05, 1e-06, 1e-07, 1e-08, 1e-09, 1e-10]

(df/dx)_数值:

[-0.9999999983332231,
-0.9999999999898844,
-0.9999999999175667,
-1.0000000005838656,
-0.999999993922529,
-1.000000082740371,
-1.000000082740371]

这是预期的吗?为什么?为什么 h = 10^-5 时得到最好的结果?

【问题讨论】:

    标签: floating-point double precision


    【解决方案1】:

    使用有限差分计算导数很容易出现loss of significance。问题是你减去的上下文,而不是你正在做的除法。基本上,当cos(x) 和cos(x+delta) 都接近于零时,cos(x)-cos(x+delta) 可以正常工作......但随着它们远离零(但仍然接近彼此),精度结果将急剧下降。在某一点上,使用较小的 delta 所带来的精度提高将被显着性损失导致的精度降低所抵消。对您而言,这似乎发生在 1e-5 左右,但这不是根本性的(在那个区域,错误往往会反弹很多)。

    关于如何进行数值稳定的有限差分已经写了很多文章,但第一条规则是“不要对你的差异太小太贪心”。如需更深入(且不那么模糊)的信息,我可以推荐 Forman Acton 的(通常)有效的数值方法。

    编辑:

    对不起,我忘了提到最重要的一点。分子中的显着性损失很重要,但x+h 中的显着性损失更是如此。这部分实际上很容易修复。

    随着x 变大,x+h 的近似值变得更糟(同样,失去意义)。基本上,您用于评估的步长与分母中的步长不匹配。但!由于您并不真正关心h 的精确 值,因此您只需计算出在x+h 舍入后最终使用的h 值,并将其用于分母以及。本质上,您计算x2=x+h,然后计算h'=x2-x。 h' 的计算与 x >> h(Sterbenz 定理)时的计算完全相同,消除了这种特殊的重要性损失。

    示例代码:

    import math
    
    def calcDerivAt_orig(f, x, h):
        x1 = x
        x2 = x+h
        y1 = f(x1)
        y2 = f(x2)
        return (y2-y1)/h
    
    def calcDerivAt_fixed(f, x, h):
        x1 = x
        x2 = x+h
        y1 = f(x1)
        y2 = f(x2)
        return (y2-y1)/(x2-x1)
    
    for h in [0.0001, 1e-05, 1e-06, 1e-07, 1e-08, 1e-09, 1e-10]:
        dOrig = calcDerivAt_orig(math.cos, math.pi/2, h)
        origErr = abs(-1 - dOrig)
        dFixed = calcDerivAt_fixed(math.cos, math.pi/2, h)
        fixedErr = abs(-1 - dFixed)
        print("h = {}: origErr = {}, fixedErr = {}".format(h, origErr, fixedErr))
    

    产生:

    h = 0.0001: origErr = 1.66677693869e-09, fixedErr = 1.66666680457e-09
    h = 1e-05: origErr = 1.01155750443e-11, fixedErr = 1.6666779068e-11
    h = 1e-06: origErr = 8.24332824223e-11, fixedErr = 1.66644475996e-13
    h = 1e-07: origErr = 5.83865622517e-10, fixedErr = 1.55431223448e-15
    h = 1e-08: origErr = 6.07747097092e-09, fixedErr = 0.0
    h = 1e-09: origErr = 8.27403709991e-08, fixedErr = 0.0
    h = 1e-10: origErr = 8.27403709991e-08, fixedErr = 0.0
    

    一点也不差。当然,我们选择x=pi/2 是在作弊,因为cos(x) 在那里接近于零;在x=1.5 或其他地方,由于我最初描述的重要性的丧失,您仍然会在h 缩小时看到错误的膨胀:

    h = 0.0001: origErr = 3.53519750529e-06, fixedErr = 3.5351976152e-06
    h = 1e-05: origErr = 3.53675653653e-07, fixedErr = 3.53669118991e-07
    h = 1e-06: origErr = 3.52831192041e-08, fixedErr = 3.53651797846e-08
    h = 1e-07: origErr = 4.03034106089e-09, fixedErr = 3.44793649187e-09
    h = 1e-08: origErr = 5.5453325265e-09, fixedErr = 5.1691428915e-10
    h = 1e-09: origErr = 7.21702790862e-08, fixedErr = 1.03628251535e-08
    h = 1e-10: origErr = 1.6659127966e-08, fixedErr = 6.58739718329e-08
    

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      相关资源
      最近更新 更多