Submitter: Fred Tydeman (USA)
Submission Date: 2004-08-26
Source: WG 14
Subject:Translation-time expresssion evaluation
The standard does not require translation-time expression evaluation to produce "obvious" values. For example, there is no requirement that static const double d = 1.23/4.56 - 1.23/4.56; be zero.
184.108.40.206.2p7 discusses FLT_EVAL_FORMAT, range and precision of floating-point constants and operations.
220.127.116.11p5 discusses translation time conversion of floating-point constants to internal format.
6.6p5 discusses translation time evaluation of floating-point constant expressions.
F.7.2p2 and footnote 306 discusses conversion of floating-point constants to internal representation.
F.7.5p3 discusses initialization and internal representation of floating-point constants.
Currently, if FLT_EVAL_METHOD is negative, there is no requirement that the first 1.23 and the second 1.23 convert to the same internal representation; one could have more precision than the other. While the same range and precision is a requirement when FLT_EVAL_METHOD is 0, 1, or 2, there is no requirement that those two identical constants convert to the same value.
Currently, if FLT_EVAL_METHOD is negative, there is no requirement that the first '/' produce a result with the same precision as the second '/' (for the same operands). This is a requirement when FLT_EVAL_METHOD is 0, 1, or 2.
Without these two requirements, the best one can do to try to get zero is: static const double d = (double)((double)1.23/(double)4.56) - (double)((double)1.23/(double)4.56); and even that could fail if the first 1.23 and the second 1.23 convert to different values.
For translation-time expression evaluation to produce the "obvious" value, two requirments must be met: the same decimal constants must result in the same value and internal representation, and operators must use the same precision.
Suggested Technical Corrigendum
18.104.22.168 Floating constants: Add to paragraph 5: All floating-constants of the same source form in the same translation unit shall convert to the same internal format with the same value. Footnote: 1.23, 1.230, 123e-2, 123e-02, 1.23L are all different source forms, so need not convert to the same internal format and value.
Add to 22.214.171.124p7, All floating-constants with the same mathematical value and type in the same translation unit should convert to the same internal format and value. Footnote: 1.23, 1.230, 123e-2 and 123e-02 have the same mathematical value, so should convert to the same internal format and value.
6.6: Constant expression: Add a 12th paragraph: All operators with the same type(s) of floating-point operand(s) in the same translation unit shall use the same precision.
Committee DiscussionThe Committee consensus is this is not a defect, and that this DR imposes a new requirement on implementations.
This is not really a requirement, but an area to be investigated that could be addressed in a future revision of the C Standard.
Previous Defect Report < - > Next Defect Report