浮点算术稳定吗? [英] Is floating point arithmetic stable?

查看:70
本文介绍了浮点算术稳定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道浮点数具有精度,精度之后的数字也不可靠.

I know that floating point numbers have precision and the digits after the precision is not reliable.

但是,如果用于计算数字的方程式相同怎么办?我可以假设结果也一样吗?

But what if the equation used to calculate the number is the same? can I assume the outcome would be the same too?

例如,我们有两个浮点数xy.我们是否可以假设机器1的结果x/y与机器2的结果完全相同? IE. ==比较将返回true

for example we have two float numbers x and y. Can we assume the result x/y from machine 1 is exactly the same as the result from machine 2? I.E. == comparison would return true

推荐答案

但是,如果用于计算数字的方程式相同怎么办?我可以假设结果也一样吗?

But what if the equation used to calculate the number is the same? can I assume the outcome would be the same too?

不,不一定.

尤其是在某些情况下,准许JIT使用更准确的中间表示形式-例如当原始数据为64位时为80位-在其他情况下则为80位.如果满足以下任一条件,则可能导致看到不同的结果:

In particular, in some situations the JIT is permitted to use a more accurate intermediate representation - e.g. 80 bits when your original data is 64 bits - whereas in other situations it won't. That can result in seeing different results when any of the following is true:

  • 您的代码略有不同,例如使用局部变量而不是字段,该字段可以更改值是否存储在寄存器中. (这是一个相对明显的示例;还有其他一些更微妙的示例可能会影响事物,例如方法中存在try块...)
  • 您正在不同的处理器上执行(我曾经观察过AMD和Intel之间的差异;同一制造商的不同CPU之间也可能存在差异)
  • 您执行的优化级别不同(例如,是否在调试器下)
  • You have slightly different code, e.g. using a local variable instead of a field, which can change whether the value is stored in a register or not. (That's one relatively obvious example; there are other much more subtle ones which can affect things, such as the existence of a try block in the method...)
  • You are executing on a different processor (I used to observe differences between AMD and Intel; there can be differences between different CPUs from the same manufacturer too)
  • You are executing with different optimization levels (e.g. under a debugger or not)

来自C#5规范第4.1.6节:

From the C# 5 specification section 4.1.6:

与操作的结果类型相比,可以更高的精度执行浮点运算.例如,某些硬件体系结构支持比双精度类型具有更大范围和精度的扩展"或长双精度"浮点类型,并使用此更高精度类型隐式执行所有浮点运算.只有付出过高的性能代价,才能使此类硬件体系结构以较低的精度执行浮点运算,而不是要求实现同时丧失性能和精度,而C#允许将高精度类型用于所有浮点运算. .除了提供更精确的结果外,这几乎没有任何可测量的效果.但是,在形式为x * y / z的表达式中,乘法运算得出的结果超出了double范围,但是随后的除法将临时结果带回到double范围,该事实是以较高范围的格式对表达式进行求值的事实.可能会产生有限的结果,而不是无穷大.

Floating-point operations may be performed with higher precision than the result type of the operation. For example, some hardware architectures support an "extended" or "long double" floating-point type with greater range and precision than the double type, and implicitly perform all floating-point operations using this higher precision type. Only at excessive cost in performance can such hardware architectures be made to perform floating-point operations with less precision, and rather than require an implementation to forfeit both performance and precision, C# allows a higher precision type to be used for all floating-point operations. Other than delivering more precise results, this rarely has any measurable effects. However, in expressions of the form x * y / z, where the multiplication produces a result that is outside the double range, but the subsequent division brings the temporary result back into the double range, the fact that the expression is evaluated in a higher range format may cause a finite result to be produced instead of an infinity.

这篇关于浮点算术稳定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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