• No results found

6.4 Lexical elements

6.4.3 Universal character names

6.4.4.2 Floating constants Syntax

1 floating-constant:

decimal-floating-constant hexadecimal-floating-constant

decimal-floating-constant:

fractional-constant exponent-partopt floating-suffixopt

digit-sequence exponent-part floating-suffixopt

hexadecimal-floating-constant:

hexadecimal-prefix hexadecimal-fractional-constant binary-exponent-part floating-suffixopt

hexadecimal-prefix hexadecimal-digit-sequence

binary-exponent-part floating-suffixopt

fractional-constant:

digit-sequenceopt . digit-sequence digit-sequence .

exponent-part:

e signopt digit-sequence E signopt digit-sequence

sign: one of

+

-digit-sequence:

digit

digit-sequence opt digit

hexadecimal-fractional-constant:

hexadecimal-digit-sequenceopt.hexadecimal-digit-sequence hexadecimal-digit-sequence .

binary-exponent-part:

p signopt digit-sequence P signopt digit-sequence

floating-suffix: one of

f l F L df dd dl DF DD DL

Constraints

2 A floating suffix df, dd, dl, DF, DD, or DLshall not be used in a hexadecimal floating constant.

Description

3 A floating constant has a significand part that may be followed by an exponent part and a suffix that specifies its type. The components of the significand part may include a digit sequence representing the whole-number part, followed by a period (.), followed by a digit sequence representing the fraction part. Digit separators (6.4.4.1) are ignored when determining the value of the constant. The components of the exponent part are an e, E, p, or Pfollowed by an exponent consisting of an optionally signed digit sequence. Either the whole-number part or the fraction part has to be present;

for decimal floating constants, either the period or the exponent part has to be present.

Semantics

4 The significand part is interpreted as a (decimal or hexadecimal) rational number; the digit sequence in the exponent part is interpreted as a decimal integer. For decimal floating constants, the exponent indicates the power of 10 by which the significand part is to be scaled. For hexadecimal floating constants, the exponent indicates the power of 2 by which the significand part is to be scaled. For decimal floating constants, and also for hexadecimal floating constants whenFLT_RADIXis not a power of 2, the result is either the nearest representable value, or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner. For hexadecimal floating constants whenFLT_RADIXis a power of 2, the result is correctly rounded.

5 An unsuffixed floating constant has typedouble. If suffixed by a floating suffix it has a type according to the following table:

Suffixes for floating-point constants Suffix Type

f, F float l, L long double df, DF _Decimal32 dd, DD _Decimal64 dl, DL _Decimal128

6 The values of floating constants may be represented in greater range and precision than that required by the type (determined by the suffix); the types are not changed thereby. See 5.2.4.2.2 regarding evaluation formats.80)

7 Floating constants of decimal floating type that have the same numerical value but different quantum exponents have distinguishable internal representations. The value shall be correctly rounded as specified in IEC 60559. The coefficient c and the quantum exponent q of a finite converted decimal floating-point number (see 5.2.4.2.3) are determined as follows:

— q is set to the value of signoptdigit-sequence in the exponent part, if any, or to 0, otherwise.

— If there is a fractional constant, q is decreased by the number of digits to the right of the period and the period is removed to form a digit sequence.

— c is set to the value of the digit sequence (after any period has been removed).

— Rounding required because of insufficient precision or range in the type of the result will round c to the full precision available in the type, and will adjust q accordingly within the limits of the type, provided the rounding does not yield an infinity (in which case the result is an appropriately signed internal representation of infinity). If the full precision of the type would require q to be smaller than the minimum for the type, then q is pinned at the minimum and c is adjusted through the subnormal range accordingly, perhaps to zero.

8 Floating constants are converted to internal format as if at translation-time. The conversion of a floating constant shall not raise an exceptional condition or a floating-point exception at execution time. All floating constants of the same source form81)shall convert to the same internal format with the same value.

9 EXAMPLE Following are floating constants of type_Decimal64and their values as triples (s, c, q). Note that for _Decimal64, the precision (maximum coefficient length) is 16 and the quantum exponent range is −398 ≤ q ≤ 369.

80)Hexadecimal floating constants can be used to obtain exact values in the semantic type that are independent of the evaluation format. Casts produce values in the semantic type, though depend on the rounding mode and may raise the inexact floating-point exception.

81)1.23,1.230,123e-2,123e-02, and1.23Lare all different source forms and thus need not convert to the same internal format and value.

0.dd (+1, 0, 0)

12345678901234567890.dd (+1, 1234567890123457, 4)assuming default rounding and DEC_EVAL_METHODis 0 or 182)

1234E-400dd (+1, 12, −398)assuming default rounding andDEC_EVAL_METHODis 0 or 1 1234E-402dd (+1, 0, −398)assuming default rounding andDEC_EVAL_METHODis 0 or 1

1000.dd (+1, 1000, 0)

10 The implementation should produce a diagnostic message if a hexadecimal constant cannot be represented exactly in its evaluation format; the implementation should then proceed with the translation of the program.

11 The translation-time conversion of floating constants should match the execution-time conversion of character strings by library functions, such asstrtod, given matching inputs suitable for both conversions, the same result format, and default execution-time rounding.83)

12 NOTE Floating constants do not include a sign and are negated by the unary - operator (6.5.3.3) which negates the rounded value of the constant. In contrast, the numeric conversion functions in thestrtofamily (7.22.1.5, 7.22.1.6) include the sign as part of the input value and convert and round the negated input. Negating before rounding and negating after rounding might yield different results, depending on the rounding direction and whether the results are correctly rounded. For example, the results are the same when both are correctly rounded using rounding to nearest or rounding toward zero, but the results are different when they are inexact and correctly rounded using rounding toward positive infinity or rounding toward negative infinity.

Conversions yielding exact results require no rounding, so are not affected by the order of negating and rounding. For types with radix 10, decimal floating constants expressed within the precision and range of the evaluation format convert exactly. For types whose radix is a power of 2, hexadecimal floating constants expressed within the precision and range of the evaluation format convert exactly.

Forward references: preprocessing numbers (6.4.8), numeric conversion functions (7.22.1), the strtofunction family (7.22.1.5, 7.22.1.6).

6.4.4.3 Enumeration constants