[cfp-interest 3952] Re: Weird Casting Error

Fred J. Tydeman tydeman at tybor.com
Wed Jun 10 19:31:23 PDT 2026


On Wed, 10 Jun 2026 17:50:41 +0200 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.

Compilers do a poor job of conforming to that requirement of
raising Invalid.  See:

http://www.tybor.com/tflt2int.c

for a test case and results of many compilers.


---
Fred J. Tydeman        Tydeman Consulting
tydeman at tybor.com      Testing, numerics, programming
+1 (702) 608-6093      Vice-chair of INCITS/C (ANSI "C")
Sample C17+FPCE tests: http://www.tybor.com
Savers sleep well, investors eat well, spenders work forever.



More information about the cfp-interest mailing list