admin管理员组文章数量:1279208
When converting to a standard floating type, if the source value is outside the range of representable values for that standard floating type, then the conversion is undefined behaviour (N3220 §6.3.1.4(3) and §6.3.1.5(2), emphasis added):
6.3.1.4 Real floating and integer
[...]
- When a value of integer type is converted to a standard floating type, if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined.
6.3.1.5 Real floating types
[...]
- When a value of real floating type is converted to a standard floating type, if the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined.
I noticed that the corresponding clauses for conversions to decimal floating types (N3220 §6.3.1.4(4) and §6.3.1.5(3)) are similarly worded, except that they are silent about what happens if the source value is outside the representable range of the decimal floating type:
6.3.1.4 Real floating and integer
[...]
- When a value of integer type is converted to a decimal floating type, if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted cannot be represented exactly, the result shall be correctly rounded with exceptions raised as specified in IEC 60559.
6.3.1.5 Real floating types
[...]
- When a value of real floating type is converted to a decimal floating type, if the value being converted cannot be represented exactly, the result is correctly rounded with exceptions raised as specified in IEC 60559.
Due to §4(2), we can infer that this sort of conversion is still undefined behaviour, but I was wondering why the Standard does not explicitly state it to be so (as it does for standard floating types)?
Since all three decimal floating types (_Decimal32
, _Decimal64
and _Decimal128
) have to conform to their respective ISO/IEC 60559 formats (of decimal32, decimal64 and decimal128, respectively), I thought there might be something in Annex F that addresses the conversion of out-of-range values, but I have not been able to find anything so far.
本文标签: cConverting to a decimal floating type when the source value is out of rangeStack Overflow
版权声明:本文标题:c - Converting to a decimal floating type when the source value is out of range - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741295939a2370816.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论