[cfp-interest 3950] Re: Weird Casting Error
Joshua Cranmer
joshua.cranmer at intel.com
Wed Jun 10 10:41:48 PDT 2026
On 6/10/2026 11:50, Vincent Lefevre wrote:
> On 2026-06-10 11:35:56 -0400, Joshua Cranmer wrote:
>> My copy of IEEE 754 has this in section 5.8:
>>
>>> Implementations shall provide conversion operations from all supported
>>> arithmetic formats to all supported
>>> signed and unsigned integer formats.
>> IEEE 754 clearly intends that conversion from floating-point formats to
>> unsigned integers is a legal (static) operation. What happens when the
>> dynamic value is out-of-range is not specified by IEEE 754 (outside of
>> invalid exception), and C opts for the UB route there.
> IEEE 754-2019 says:
>
> 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.
>
> So I don't think that UB is correct. However, it depends on the
> bindings (there may be a way to do the conversion that is UB and
> another way that conforms to IEEE 754).
With Annex F, the resulting value is unspecified (see F.4). Without
Annex F, the rule is given in 6.3.14:
> 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.
More information about the cfp-interest
mailing list