Title
Undefined behavior in integral-to-floating conversions
Status
cd2
Section
7.3.11 [conv.fpint]
Submitter
Alberto Ganesh Barbati

Created on 2008-08-02.00:00:00 last changed 143 months ago

Messages

Date: 2009-07-15.00:00:00

[Voted into WP at July, 2009 meeting.]

Date: 2009-03-15.00:00:00

Proposed resolution (March, 2009):

Change 7.3.11 [conv.fpint] paragraph 2 as indicated:

An rvalue of an integer type or of an unscoped enumeration type can be converted to an rvalue of a floating point type. The result is exact if possible. Otherwise If the value being converted is in the range of values that can be represented but cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value. [Note: loss of precision occurs if the integral value cannot be represented exactly as a value of the floating type. —end note] If the value being converted is outside the range of values that can be represented, the behavior is undefined. If the source type is bool, the value false is converted to zero and the value true is converted to one.
Date: 2008-09-15.00:00:00

Notes from the September, 2008 meeting:

The CWG agreed that the C approach should be adopted.

Date: 2008-08-02.00:00:00

The current wording of 7.3.11 [conv.fpint] paragraph 2 does not specify what should happen when converting an integer value that is outside the representable range of the target floating point type. The C99 Standard covers this case explicitly in 6.3.1.4 paragraph 2:

When a value of integer type is converted to a real 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.

While the current C++ specification requires defined behavior in all cases, the C specification allows for use of NaNs and traps, if those are needed for efficiency.

History
Date User Action Args
2010-03-29 00:00:00adminsetstatus: wp -> cd2
2009-11-08 00:00:00adminsetstatus: dr -> wp
2009-08-03 00:00:00adminsetmessages: + msg2228
2009-08-03 00:00:00adminsetstatus: ready -> dr
2009-03-23 00:00:00adminsetmessages: + msg1885
2009-03-23 00:00:00adminsetstatus: drafting -> ready
2008-10-05 00:00:00adminsetmessages: + msg1775
2008-10-05 00:00:00adminsetstatus: open -> drafting
2008-08-02 00:00:00admincreate