Title
Compile-time evaluation of floating-point expressions
Status
cd2
Section
7.7 [expr.const]
Submitter
Jens Maurer

Created on 2007-10-03.00:00:00 last changed 179 months ago

Messages

Date: 2009-03-15.00:00:00

[Voted into the WP at the March, 2009 meeting.]

Date: 2008-08-15.00:00:00

Proposed resolution (August, 2008):

  1. Delete bullet 6 of 7.7 [expr.const] paragraph 2:

    • a type conversion from a floating-point type to an integral type (7.3.11 [conv.fpint]) unless the conversion is directly applied to a floating-point literal;
  2. Add a new paragraph after 7.7 [expr.const] paragraph 3:

  3. [Note: Although in some contexts constant expressions must be evaluated during program translation, others may be evaluated during program execution. Since this International Standard imposes no restrictions on the accuracy of floating-point operations, it is unspecified whether the evaluation of a floating-point expression during translation yields the same result as the evaluation of the same expression (or the same operations on the same values) during program execution. [Footnote: Nonetheless, implementations are encouraged to provide consistent results, irrespective of whether the evaluation was actually performed during translation or during program execution. —end footnote] [Example:

      bool f() {
        char array[1 + int(1 + 0.2 - 0.1 - 0.1)];  // Must be evaluated during translation
        int size = 1 + int(1 + 0.2 - 0.1 - 0.1);   // May be evaluated at runtime
        return sizeof(array) == size;
      }
    

    It is unspecified whether the value of f() will be true or false. —end example] —end note]

Date: 2008-06-15.00:00:00

Notes from the June, 2008 meeting:

The CWG agreed with the intent of this issue, that floating-point calculations should be permitted in constant expressions, but acknowledged that this opens the possibility of differing results between compile time and run time. Such issues should be addressed non-normatively, e.g., via a “recommended practice” note like that of C99's 6.4.4.2 or in a technical report.

Date: 2008-06-15.00:00:00

Proposed resolution (June, 2008):

Delete bullet 6 of 7.7 [expr.const] paragraph 2:

  • a type conversion from a floating-point type to an integral type (7.3.11 [conv.fpint]) unless the conversion is directly applied to a floating-point literal;
Date: 2007-10-03.00:00:00

It was the intention of the constexpr proposal that implementations be required to evaluate floating-point expressions at compile time. This intention is not reflected in the actual wording of 7.7 [expr.const] paragraph 2, bullet 5:

  • a type conversion from a floating-point type to an integral type (7.3.11 [conv.fpint]) unless the conversion is directly applied to a floating-point literal;

This restriction has the effect of forbidding the use of floating-point expressions in integral constant expressions.

History
Date User Action Args
2010-03-29 00:00:00adminsetstatus: wp -> cd2
2009-08-03 00:00:00adminsetstatus: dr -> wp
2009-03-23 00:00:00adminsetmessages: + msg2021
2009-03-23 00:00:00adminsetstatus: ready -> dr
2008-10-05 00:00:00adminsetstatus: review -> ready
2008-08-25 00:00:00adminsetmessages: + msg1749
2008-08-25 00:00:00adminsetstatus: drafting -> review
2008-06-29 00:00:00adminsetmessages: + msg1709
2008-06-29 00:00:00adminsetmessages: + msg1708
2008-06-29 00:00:00adminsetstatus: open -> drafting
2007-10-03 00:00:00admincreate