FP:无效操作:C(UB)与IEEE 754(WDB)之间存在矛盾? [英] FP: invalid operation: contradiction between C (UB) and IEEE 754 (WDB)?

查看:77
本文介绍了FP:无效操作:C(UB)与IEEE 754(WDB)之间存在矛盾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N2479 C17..C2x工作草案— 2020年2月5日ISO/IEC 9899:202x(E):

N2479 C17..C2x working draft — February 5, 2020 ISO/IEC 9899:202x (E):

6.3.1.4实数浮点数和整数:

6.3.1.4 Real floating and integer:

1当将标准浮点型的有限值转换为_Bool以外的整数类型时,小数部分将被丢弃(即,该值将被截断为零).如果整数部分的值不能用整数类型表示,则行为是不确定的.

1 When a finite value of standard floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.

IEEE 754-2019:

IEEE 754-2019:

5.8从浮点格式转换为整数格式的详细信息:

5.8 Details of conversions from floating-point to integer formats:

当不能以目标格式表示NaN或无限操作数并且不能另外指示时,应发出无效操作异常的信号.当数值操作数将转换为目标格式范围之外的整数时,如果无法以其他方式表示这种情况,则应发出无效操作异常的信号.

When a NaN or infinite operand cannot be represented in the destination format and this cannot otherwise be indicated, the invalid operation exception shall be signaled. When a numeric operand would convert to an integer outside the range of the destination format, the invalid operation exception shall be signaled if this situation cannot otherwise be indicated.

即C表示行为未定义,而IEEE 754表示无效操作异常应发出信号.意思是w.r.t.异常信号表示未定义行为(C)内有定义明确的行为(IEEE 754).这是C与IEEE 754之间的矛盾吗?请解释.

I.e. the C says the behavior is undefined while IEEE 754 says the invalid operation exception shall be signaled. Meaning that w.r.t. exception signalling there is well-defined behavior (IEEE 754) inside the undefined behavior (C). Is it a contradiction between C and IEEE 754? Please explain.

推荐答案

我没有C草案N2479,但我希望相关部分与C 2018正式版没有太大变化.它将未定义的行为"定义为:

I do not have C draft N2479, but I expect the relevant parts are little changed from the C 2018 official version. It defines "undefined behavior" as:

…在使用非便携式或错误程序构造或错误数据时的行为,对此文档没有任何要求……

… behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this document imposes no requirements…

因此,不可能与IEEE 754存在任何矛盾或冲突.由于当整数部分的值不能用整数类型表示时"会发生什么情况,因此C标准没有强加任何条件,因此C实现不违反C标准的任何要求.完全实现任何行为,无论是IEEE 754标准指定的行为还是其他行为.

It is therefore impossible for any contradiction or conflict with IEEE 754 to exist. As the C standard does not impose any requirement for what happens when "the value of the integral part cannot be represented by the integer type," a C implementation does not violate any requirement of the C standard when it implements any behavior at all, whether that is a behavior specified by the IEEE 754 standard or something else.

关于问题中的这一段:

即C表示行为未定义,而IEEE 754表示应发出无效操作异常信号.意思是w.r.t.异常信号表示未定义行为(C)内有定义明确的行为(IEEE 754).这是C与IEEE 754之间的矛盾吗?请解释.

I.e. the C says the behavior is undefined while IEEE 754 says the invalid operation exception shall be signaled. Meaning that w.r.t. exception signalling there is well-defined behavior (IEEE 754) inside the undefined behavior (C). Is it a contradiction between C and IEEE 754? Please explain.

这种存在矛盾的说法似乎是普遍存在的错误信息或对错误定义的误解.未定义的行为并非意味着该行为是疯狂的,不可控制的或不可预测的.正如C标准所使用的,表示C标准是静默的.这是一种缺少的东西,而不是一种类型的东西.缺少任何要求,而不是缺少控制或知识的要求.当C标准留下空白时,其他标准和其他规范可能会填补空白.

This assertion that there is a contradiction appears to be a consequence of pervasive misinformation or misrepresentation of what undefined behavior means. Undefined behavior is not a thing that means the behavior is wild, uncontrollable, or unpredictable. As used by the C standard, it only means the C standard is silent. It is a lack of a thing, not a type of thing. It is a lack of any requirements, not a requirement that there be any lack of control or knowledge. When the C standard leaves a gap, other standards and other specifications may fill in that gap.

当C标准未定义行为时,可以通过其他标准,编译器文档,硬件规范,其他软件,操作系统等部分或全部定义行为.

When the C standard does not define a behavior, the behavior may be defined, partially or completely, by other standards, by compiler documentation, by hardware specifications, by other software, by the operating system, and so on.

在这种情况下,如果C实现选择同时符合C标准和IEEE 754标准,则可以做到无矛盾,因为实现IEEE 754所需的行为应遵守IEEE 754的规则并遵守这些规则符合C标准.

In this case, if a C implementation chooses to conform to both the C standard and the IEEE 754 standard, it can do so without contradiction, as implementing the behavior required by IEEE 754 obeys the rules of IEEE 754 and obeys the rules of the C standard.

这篇关于FP:无效操作:C(UB)与IEEE 754(WDB)之间存在矛盾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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