是C ++中的浮点加法交换吗? [英] Is floating point addition commutative in C++?

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

问题描述

对于浮点值,是否保证 a + b == b + a

For floating point values, is it guaranteed that a + b == b + a ?

相信这在IEEE754中是保证的,然而C ++标准没有规定必须使用IEEE754。唯一相关的文本似乎来自[expr.add]#3:

I believe this is guaranteed in IEEE754, however the C++ standard does not specify that IEEE754 must be used. The only relevant text seems to be from [expr.add]#3:


二进制+运算符的结果是操作数。

The result of the binary + operator is the sum of the operands.

数学运算sum是可交换的。然而,数学运算sum也是关联的,而浮点加法绝对不是关联的。因此,在我看来,我们不能得出结论,数学中和的交换性意味着这个引用指定C ++中的交换性。

The mathematical operation "sum" is commutative. However, the mathematical operation "sum" is also associative, whereas floating point addition is definitely not associative. So, it seems to me that we cannot conclude that the commutativity of "sum" in mathematics means that this quote specifies commutativity in C++.

推荐答案

甚至不需要 a + b == a + b 。子表达式之一可以比另一个更精确地保持加法的结果,例如当多个加法的使用要求将一个子表达式临时存储在存储器中时,当另一个子表达式可以保存在寄存器中时

It is not even required that a + b == a + b. One of the subexpressions may hold the result of the addition with more precision than the other one, for example when the use of multiple additions requires one of the subexpressions to be temporarily stored in memory, when the other subexpression can be kept in a register (with higher precision).

如果 a + b == a + b 无法保证, a + b == b + a 不能如果 a + b 不必每次都返回相同的值,并且值不同,那么其中一个必然将不等于 b + a 的一个特定评估。

If a + b == a + b is not guaranteed, a + b == b + a cannot be guaranteed. If a + b does not have to return the same value each time, and the values are different, one of them necessarily will not be equal to one particular evaluation of b + a.

这篇关于是C ++中的浮点加法交换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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