Is floating point addition commutative in C++?(C ++中的浮点加法是否可交换?)
问题描述
对于浮点值,是否保证a + b
与1 b + a
相同?
For floating point values, is it guaranteed that a + b
is the same as1 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"是也是关联的,而浮点加法绝对是 not 关联的.所以,在我看来,我们不能得出结论,和"的交换性是在数学中意味着这句话指定了 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++.
脚注 1:
一样"如按位相同,例如 memcmp
而不是 ==
,以区分 +0 和 -0.IEEE754 将 +0.0 == -0.0
视为真,但也对有符号零有特定规则.+0 + -0
和 -0 + +0
在 IEEE754 中都产生 +0
,对于添加具有相等幅度的相反符号值也是如此.遵循 IEEE 语义的 ==
将隐藏有符号零的非交换性(如果这是标准).
Footnote 1:
"Same" as in bitwise identical, like memcmp
rather than ==
, to distinguish +0 from -0. IEEE754 treats +0.0 == -0.0
as true, but also has specific rules for signed zero. +0 + -0
and -0 + +0
both produce +0
in IEEE754, same for addition of opposite-sign values with equal magnitude. An ==
that followed IEEE semantics would hide non-commutativity of signed-zero if that was the criterion.
此外,如果任一输入为 NaN,则 a+b == b+a
在 IEEE754 数学中为假.memcmp
将说明两个 NaN 是否具有相同的位模式(包括有效负载),尽管我们可以将 NaN 传播规则与有效数学运算的交换性分开考虑.
Also, a+b == b+a
is false with IEEE754 math if either input is NaN.
memcmp
will say whether two NaNs have the same bit-pattern (including payload), although we can consider NaN propagation rules separately from commutativity of valid math operations.
推荐答案
甚至不需要 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 ++中的浮点加法是否可交换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C ++中的浮点加法是否可交换?


基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01