Document:       WG14/N1361

Date:                2009/02/27

References:      WG14/N1321, WG14/N1346

Authors:           Jim Thomas

Reply to:          Jim Thomas <[email protected]>

 

Subject:  FLT_EVAL_METHOD and constants

 

Problem: The September 2008 meeting minutes (WG14/N1346), item 4.21, record:

 

Straw Poll:

1. Accept N1321 to the WP, removing const from FLT_EVAL_METHOD 8‐1‐8 YES

 

Removing constants from the FLT_EVAL_METHOD would cause silent changes in the behavior of C99 strictly conforming programs. For example, 5.2.4.2.2 #8 says that if FLT_EVAL_METHOD equals 1, then float constants are evaluated to the range and precision of the double type. Annex F says that if the implementation defines __STDC_IEC_559__, then the double type and its basic arithmetic is fully specified by the floating-point standard and the conversion from decimal character sequences (of length up to DECIMAL_DIG) to double is correctly rounded. Therefore, the following program should never print “non-conforming implementation”:

 

#include <float.h>

#include <stdio.h>

Int main(void)

{

#if defined(__STDC_IEC_559__) && (FLT_EVAL_METHOD == 1)

            If (0.1F != (1.F / 10.F)) printf(“non-conforming implementation\n”);

#endif

}

 

However, it would print “non-conforming implementation” if the #if conditions were satisfied but the constant 0.1F were not widened to double like the divide operation.

 

The reason C99 included constants in FLT_EVAL_METHOD is the following. Commonly, programs use a given floating type because the type was assumed (without benefit of error analysis) to be wide enough, not because wider precision would be incorrect. That assumption might be incorrect for unanticipated data sets. Wide evaluation can make these programs more robust. Constants, which amount to translation-time decimal-to-binary conversion operations, can be widened like arithmetic operations and contribute to the overall positive effects of wide evaluation.

 

On the other hand, there are arguments against including constants in FLT_EVAL_METHOD. Constants wider than their suffix type might surprise some users. Also, some special programs require constants rounded to a specific width (and not wider). The hexadecimal form can be used for floating constants whose value is independent of the evaluation method, though this option may be unfamiliar to some users.

 

Recommendation: Don’t remove constants from the current standard evaluation methods, namely those for FLT_EVAL_METHOD values 0, 1, or 2.

 

If standard-supported evaluation methods that widen operations but not constants are deemed desirable then these could be defined by the standard and specified by positive FLT_EVAL_METHOD values other than 1 or 2.