倍频四舍五入和评估顺序 [英] Octave Rounding and Evaluation Order

查看:47
本文介绍了倍频四舍五入和评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在八度音阶中获得

1 - 0.05 -0.95 = 0

1 - 0.95 -0.05 = 4.1633e-17

我了解这是由于评估顺序与的近似二进制表示相结合引起的0.05作为0.00(0011)和0.95为0.11(1100)有人可以给我讲整个故事还是给我看一个解释它的链接?

I understand that it is caused by the order of evaluation combined with the approximate binary representation of 0.05 as 0.00(0011) and 0.95 as 0.11(1100) Could someone please give me the whole story or show me a link explaining it?

---这个问题不是为什么24.0000不等于MATLAB中的24.0000?,其他人认为这是可能的重复.后者处理数字的舍入表示.前者要求提供详细的机制,通过该机制,计算的执行顺序会影响结果的精度.

--- This question is not a duplicate of Why is 24.0000 not equal to 24.0000 in MATLAB?, which was identified by others as a possible duplicate. The latter deals with the rounded representation of a number. The former is asking for the details of the mechanism by which the order of execution of a calculation affects the precision of the result.

推荐答案

Matzeri与浮点算术权威资源的链接确实是该问题的权威答案.但是,为了完成:

Matzeri's link to the definitive resource on floating point arithmetic is indeed the definitive answer to this question. However, for completion:

octave:34> fprintf("%.80f\n%.80f\n", 0.95, 1 - 0.05)
0.94999999999999995559107901499373838305473327636718750000000000000000000000000000
0.94999999999999995559107901499373838305473327636718750000000000000000000000000000

octave:35> fprintf("%.80f\n%.80f\n", 0.05, 1 - 0.95)
0.05000000000000000277555756156289135105907917022705078125000000000000000000000000
0.05000000000000004440892098500626161694526672363281250000000000000000000000000000

换句话说,使用0.95很难精确地表示为浮点数,因此第一步中涉及0.95(作为输入或输出)的任何计算必然比仅使用0.05的计算精度低.

In other words, 0.95 is less easy to represent precisely in floating point, so any calculation in the first step that involves 0.95 (either as an input or as an output) is necessarily less precise than one that only uses 0.05.

因此:

1 - 0.05 = 0.95 (imprecise, due to intrinsic floating-point representation)
(1 - 0.05) - 0.95 = exactly 0 (since both are represented identically imprecisely)

vs

1 - 0.95 = imprecise 0.05 (due to involvement of 0.95 in calculation)
(imprecise 0.05) - (precise 0.05) = not exactly 0 (due to difference in precisions)

如何.应当指出,这种精度差异远低于机器公差(由我的机器上的 eps -2.2204e-16返回).因此,对于所有实际应用,4.1633e-17 .如果此处的实际要点是测试计算结果是否有效,0,实际上,在处理浮点计算时,应该始终考虑机器精度,或者最好找到一种方法来重新构造问题,从而完全避免进行相等性测试.

HOWEVER. It should be pointed out that this difference in precision is well below the machine tolerance (as returned by eps -- 2.2204e-16 on my machine). Therefore, for all practical applications, 4.1633e-17 is 0. If the practical point here is testing whether the result of a calculation is effectively 0, then in practical terms one should always take machine precision into account when dealing with floating point calculations, or preferably find a way to reformulate your problem such that it avoids the need for equality testing altogether.

这篇关于倍频四舍五入和评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆