FP:无效操作:C (UB) 和 IEEE 754 (WDB) 之间的矛盾?
FP: invalid operation: contradiction between C (UB) and IEEE 754 (WDB)?
N2479 C17..C2x 工作草案 — 2020 年 2 月 5 日 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.
即C 表示 行为未定义 而 IEEE 754 表示 应发出无效操作异常信号 。意思是w.r.t。异常信号在未定义的行为 (C) 中存在明确定义的行为 (IEEE 754)。 C和IEEE 754之间是否矛盾?请解释。
我没有C draft N2479,但我预计相关部分与C 2018正式版相比变化不大。它将“未定义的行为”定义为:
… behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this document imposes no requirements…
因此,不可能与 IEEE 754 存在任何矛盾或冲突。由于 C 标准没有对当“整数部分的值不能用整数类型表示”时发生的情况施加 any 要求,因此 C 实现不违反 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标准所使用的那样,它only表示C标准是沉默的。它是缺乏一种东西,而不是一种东西。这是缺乏任何要求,而不是要求缺乏控制或知识。当 C 标准留下空白时,其他标准和其他规范可能会填补该空白。
当 C 标准未定义行为时,该行为可能部分或全部由其他标准、编译器文档、硬件规范、其他软件、操作系统等定义。
在这种情况下,如果 C 实现选择同时符合 C 标准和 IEEE 754 标准,那么它可以不矛盾地这样做,因为实现 IEEE 754 要求的行为遵守 IEEE 754 的规则并遵守C标准的规则。
N2479 C17..C2x 工作草案 — 2020 年 2 月 5 日 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.
即C 表示 行为未定义 而 IEEE 754 表示 应发出无效操作异常信号 。意思是w.r.t。异常信号在未定义的行为 (C) 中存在明确定义的行为 (IEEE 754)。 C和IEEE 754之间是否矛盾?请解释。
我没有C draft N2479,但我预计相关部分与C 2018正式版相比变化不大。它将“未定义的行为”定义为:
… behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this document imposes no requirements…
因此,不可能与 IEEE 754 存在任何矛盾或冲突。由于 C 标准没有对当“整数部分的值不能用整数类型表示”时发生的情况施加 any 要求,因此 C 实现不违反 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标准所使用的那样,它only表示C标准是沉默的。它是缺乏一种东西,而不是一种东西。这是缺乏任何要求,而不是要求缺乏控制或知识。当 C 标准留下空白时,其他标准和其他规范可能会填补该空白。
当 C 标准未定义行为时,该行为可能部分或全部由其他标准、编译器文档、硬件规范、其他软件、操作系统等定义。
在这种情况下,如果 C 实现选择同时符合 C 标准和 IEEE 754 标准,那么它可以不矛盾地这样做,因为实现 IEEE 754 要求的行为遵守 IEEE 754 的规则并遵守C标准的规则。