是浮点运算用C关联? [英] Are floating point operations in C associative?

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

问题描述

数学增加持有物业的关联:

Addition mathematically holds the associative property:

(a + b) + c = a + (b + c)

在一般情况下,这个属性不会因为他们重新在有限precision present值保持浮点数。

In the general case, this property does not hold for floating-point numbers because they represent values in a finite precision.

允许作出上述替代从C程序生成机code作为优化的一部分,当一个编译器?它在哪里正好在C标准说什么?

Is a compiler allowed to make the above substitution when generating machine code from a C program as part of an optimization? Where does it exactly say in the C standard?

推荐答案

编译器是不允许进行优化,这将导致计算出不同的值,一个比根据抽象机的语义计算。

The compiler is not allowed to perform "optimizations", which would result in a different value computed, than the one computed according to abstract machine semantics.

5.1.2.3程序执行

5.1.2.3 Program execution

[#1]在本国际的语义描述
         标准描述一个抽象机的行为
         其优化的问题是无关紧要的。

[#1] The semantic descriptions in this International Standard describe the behavior of an abstract machine in which issues of optimization are irrelevant.

[#3]在抽象机,所有前pressions进行评估
       由语义指定。

[#3] In the abstract machine, all expressions are evaluated as specified by the semantics.

[#13例5重排浮点前pressions
         往往受到限制,因为在precision作为限制
         以及范围。实现一般不能申请
         加法数学关联规则或
         乘法,也不是分配规则,因为
         舍入误差,甚至在没有溢出的和
         溢。

[#13] EXAMPLE 5 Rearrangement for floating-point expressions is often restricted because of limitations in precision as well as range. The implementation cannot generally apply the mathematical associative rules for addition or multiplication, nor the distributive rule, because of roundoff error, even in the absence of overflow and underflow.

在您的例子:

(a + b) + c

甚至没有括号:

a + b + c

我们

   +
  / \
  +  c
 / \
 a  b

和编译器需要产生code,如果 A 总结与 B ,结果总结与 C

and the compiler is required to generate code as if a is summed with b and the result is summed with c.

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

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