FP: invalid operation: contradiction between C (UB) and IEEE 754 (WDB)?

北城以北 提交于 2021-01-07 01:06:06

问题


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

6.3.1.4 Real floating and integer:

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:

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

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.

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.


回答1:


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…

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.

Regarding this paragraph in the question:

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.

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.

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.

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.



来源:https://stackoverflow.com/questions/65106621/fp-invalid-operation-contradiction-between-c-ub-and-ieee-754-wdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!