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

[...]

  1. 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

[...]

  1. 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

[...]

  1. 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

[...]

  1. 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