Document: WG14 N1391


Floating-point to int/_Bool conversions


Submitter: Fred Tydeman (USA)
Submission Date: 2009-06-29
Subject: Floating-point to int/_Bool conversions

Unlike sections 6.3.1.3, 6.3.1.4, 6.5.16.1, and G.4.2, which make an explicit exception for _Bool type, F.4 makes no mention of _Bool. I believe that that is an oversite.

In crafting words to correct this, I constructed the following tables show what should happen for various cases:

_Bool b = FP value;

Result b    FP value    FP exception flags
0           +/- 0.0     none
1           +/- finite  none
1           +/- INF     none
1           NaN         none


int i = FP value;

Result i    FP value    FP exception flags
0           +/- 0.0     none
FP          +/- int     none
trunc(FP)   +/- non-int inexact (unspecified)
unspecified +/- large   invalid
unspecified +/- INF     invalid
unspecified NaN         invalid

However, the words in C99 do not reflect that. So, I am proposing changes in the wording to match what should happen.

Proposed changes to C1x

First part of change.

In 6.3.1.2 Boolean type, add a footnote, to end up with

When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1 (footnote).

(footnote) NaNs do not compare equal to 0, so they convert to 1.

Second part of change.

In F.4 Floating to integer conversion, change paragraph 1 from

If the floating value is infinite or NaN or if the integral part of the floating value exceeds the range of the integer type, then the "invalid" floating-point exception is raised and the resulting value is unspecified. Whether conversion of non-integer floating values whose integral part is within the range of the integer type raises the "inexact" floating-point exception is unspecified.319)

to

First, if the integer type is _Bool, then 6.3.1.2 applies for the value and no floating-point exception is raised (even for NaN);

Otherwise, if the floating value is infinite or NaN or if the integral part of the floating value exceeds the range of the integer type, then the "invalid" floating-point exception is raised and the resulting value is unspecified;

Otherwise, the resulting value is determined by 6.3.1.4. Conversion of integral floating values raise no floating-point exceptions. Whether conversion of non-integer floating values raises the "inexact" floating-point exception is unspecified.319)