Subject: [cfp-interest 3951] Re: Weird Casting Error

From: Damian McGuckin <[email-masked]>
Date: Thu, 11 Jun 2026 06:53:08 +1000 (AEST)

On Wed, 10 Jun 2026, Joshua Cranmer wrote:

> With Annex F, the resulting value is unspecified (see F.4). Without 
> Annex F, the rule is given in 6.3.14:
>
> On 6/10/2026 11:50, Vincent Lefevre wrote:
>
>>  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.

Thanks for finding that clause Joshua. My fishing skills sometimes fail.

Sorry, Fred, Vincent and Joshua - I should have been more explicit. I was 
talking in the context of Annex F, not the main body of the C standard. I 
was wondering whether we needed a Recommended Practice paragraph or a NOTE 
or a footnote about dealing with casting to unsigned numbers in Annex F.

I was also asking the question in the context of some discussion going on 
within IEEE 754R at the moment and was looking for some discussion to help 
clarify my own thoughts.

Most modern CPUs return the indefinite integer (which is neither a C 
standard concept nor an IUEEE 754 (ISO 60559 concept) when either (as 
Vincent mentioned)

a) "... 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" or
b) "... 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

Thanks for the feedback - Damian

P.S. Thanks David for getting the mailing list wordking again. My own
apologies to those to whom I sent some extra noise during its downtime.

---

From: "Fred J. Tydeman" <[email-masked]>
Date: Thu, 11 Jun 2026 10:31:23 +0800 (SGT)

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
>  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
[email-masked]      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.