The New C Standard P5
lượt xem 4
download
The New C Standard P5
Tham khảo tài liệu 'the new c standard p5', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: The New C Standard P5
 5.2.4.2.2 Characteristics of ﬂoating types 368 binary number. The number N of radixB digits required to represent an ndigit FLT_RADIX ﬂoatingpoint number is given by the condition (after substituting C Standard values):[918] 10N −1 > FLT _RADIX LDBL_MANT _DIG (368.1) Minimizing for N , we get: LDBL_MANT _DIG N =2+ (368.2) logFLT _RADIX 10 When FLT _RADIX is 2, this simpliﬁes to: LDBL_MANT _DIG N =2+ (368.3) 3.321928095 By using fewer decimal digits, we are accepting that the decimal value may not be the one closest to the binary value. It is simply a member of the set of decimal values having the same binary value that is representable 379 DECI MAL_DIG in DECIMAL_DIG digits. For instance, the decimal fraction 0.1 is closer to the preceding binary fraction than conversion recommended any other nearby binary fractions.[1313] practice When b is not a power of 10, this value will be larger than the equivalent *_DIG macro. But not all of the possible combinations of DECIMAL_DIG decimal digits can be generated by a conversion. The number of 335 precision representable values between each power of the radix is ﬁxed. However, each successive power of 10 supports ﬂoatingpoint a greater number of representable values (see Figure 368.1). Eventually the number of representable decimal values, in a range, is greater than the number of representable p radix values. The value of DECIMAL_DIG denotes the power of 10 just before this occurs. C90 Support for the DECIMAL_DIG macro is new in C99. C++ Support for the DECIMAL_DIG macro is new in C99 and speciﬁed in the C++ Standard. Other Languages Few other languages get involved in exposing such details to the developer. Common Implementations A value of 17 would be required to support IEC 60559 double precision. A value of 9 is sufﬁcient to support IEC 60559 single precision. The format used by Apple on the P OWER PC[50] to represent the long double type is the concatenation long double Apple of two double types. Apple recommends that the difference in exponents, between the two doubles, be 54. representable binary values n+0 n+2 n+4 n+6 n+8 n+10 2 2 2 2 2 2            m+0 m+1 m+2 m+3 10 10 10 10 representable decimal values Figure 368.1: Representable powers of 10 and powers of 2 on the real line. June 24, 2009 v 1.2
 368 5.2.4.2.2 Characteristics of ﬂoating types However, it is not possible to guarantee this will always be the case, giving the representation an indeﬁnite precision. The number of decimal digits needed to ensure that a cycle of conversions delivers the original value is proportional to the difference between the exponents in the two doubles. When the least signiﬁcant double has a value of zero, the difference can be very large. The following example is based on the one given in the Apple Macintosh P OWER PC numerics documen tation.[50] If an object with type double, having the value 1.2, is assigned to an object having long double type, the least signiﬁcant bits of the signiﬁcand are given the zero value. In hexadecimal (and decimal to 34 digits of accuracy): 1 0x3FF33333 33333333 00000000 00000000 2 1.199999999999999955591079014993738 The decimal form is the closest 34digit approximation of the long double number (represented using double double). It is also the closest 34decimal digit approximation to an inﬁnitely precise binary value whose exponent is 0 and whose fractional part is represented by 13 sequences of 0011 followed by 52 binary zeros, followed by some nonzero bits. Converting this decimal representation back to a binary representation, the Apple P OWER PC Numerics library returns the closest doubledouble approximation of the inﬁnitely precise value, using all of the bits of precision available to it. It will use all 53 bits in the head and 53 bits in the tail to store nonzero values and adjust the exponent of the tail accordingly. The result is: 1 0x3FF33333 33333333 xxxyzzzz zzzzzzzz where xxx represents the sign and exponent of the tail, and yzzz... represents the start of a nonzero value. Because the tail is always nonzero, this value is guaranteed to be not equal to the original value. Implementations add additional bits to the exponent and signiﬁcand to support a greater range of values and precision, and most keep the bits representing the various components contiguous. The Unisys A Series[1422] represents the type double using the same representation as type float in the ﬁrst word, and by having additional exponent and signiﬁcand bits in a second word. The external effect is the same. But it is an example of how developer assumptions about representation, in this case bits being contiguous, can be proved wrong. Coding Guidelines One use of this macro is in calculating the amount of storage needed to hold the character representation, in decimal, of a ﬂoatingpoint value. The deﬁnition of the macro excludes any characters (digits or otherwise) that may be used to represent the exponent in any printed representation. The only portable method of transferring data having a ﬂoatingpoint type is to use a characterbased representation (e.g., a list of decimal ﬂoatingpoint numbers in character form). For a given implementation, this macro gives the minimum number of digits that must be written if that value is to be read back in without change of value. 1 printf("%#.*g", DECIMAL_DIG, float_valu); Space can be saved by writing out fewer than DECIMAL_DIG digits, provided the ﬂoatingpoint value contains less precision than the widest supported ﬂoating type. Trailing zeros may or may not be important; the issues involved are discussed elsewhere. Converting a ﬂoatingpoint number to a decimal value containing more than DECIMAL_DIG digits may, or may not, be meaningful. The implementation of the printf function may, or may not, choose to convert to the decimal value closest to the internally represented ﬂoatingpoint value, while other implementations simply produce garbage digits.[1313] Example v 1.2 June 24, 2009
 5.2.4.2.2 Characteristics of ﬂoating types 369 1 #include 2 3 /* 4 * Array big enough to hold any decimal representation (at least for one 5 * implementation). Extra characters needed for sign, decimal point, 6 * and exponent (which could be six, E1234, or perhaps even more). 7 */ 8 #if LDBL_MAX_10_EXP < 10000 9 10 char float_digits[DECIMAL_DIG + 1 + 1 + 6 + 1]; 11 12 #else 13 #error Looks like we need to handle this case 14 #endif 369 — number of decimal digits, q, such that any ﬂoatingpoint number with q decimal digits can be rounded into a *_DIG macros ﬂoatingpoint number with p radix b digits and back again without change to the q decimal digits, pmax log10 b if b is a power of 10 (p − 1) log10 b otherwise FLT_DIG 6 DBL_DIG 10 LDBL_DIG 10 Commentary The conversion process here is base10⇒baseFLT_RADIX⇒base10 (the opposite ordering is described 368 DECI elsewhere). These macros represent the maximum number of decimal digits, such that each possible digit MAL_DIG macro sequence (value) maps to a different (it need not be exact) radix b representation (value). If more than one decimal digit sequence maps to the same radix b representation, it is possible for a different decimal sequence (value) to be generated when the radix b form is converted back to its decimal representation. C++ static const int digits10; 18.2.1.2p9 Number of base 10 digits that can be represented without change. Equivalent to FLT_DIG, DBL_DIG, LDBL_DIG. Footnote 184 Header (Table 17): . . . The contents are the same as the Standard C library header . 18.2.2p4 Other Languages The Fortran 90 PRECISION inquiry function is deﬁned as INT((p1) * LOG10(b)) + k, where k is 1 is b is an integer power of 10 and 0 otherwise. Example This example contains a rare case of a 7 digit decimal number that cannot be converted to single precision IEEE 754 and back to decimal without loss of precision. June 24, 2009 v 1.2
 370 5.2.4.2.2 Characteristics of ﬂoating types 1 #include 2 3 float before_rare_case_in_float = 9.999993e4; 4 float _7_dig_rare_case_in_float = 9.999994e4; 5 float after_rare_case_in_float = 9.999995e4; 6 7 int main(void) 8 { 9 printf("9.999993e4 == %.6e\n", before_rare_case_in_float); 10 printf("9.999994e4 == %.6e\n", _7_dig_rare_case_in_float); 11 printf("9.999995e4 == %.6e\n", after_rare_case_in_float); 12 } *_MIN_EXP — minimum negative integer such that FLT_RADIX raised to one less than that power is a normalized 370 ﬂoatingpoint number, emin FLT_MIN_EXP DBL_MIN_EXP LDBL_MIN_EXP Commentary These values are essentially the minimum value of the exponent used in the internal ﬂoatingpoint representa *_MIN 378 tion. The *_MIN macros provide constant values for the respective minimum normalized ﬂoatingpoint value. macros No minimum values are given in the standard. The possible values can be calculated from the following: FLT _MIN _10 _EXP FLT _MIN _EXP = ±1 (370.1) log(FLT _RADIX ) C++ 18.2.1.2p23 static const int min_exponent; Minimum negative integer such that radix raised to that power is in the range of normalised ﬂoating point numbers.189) Equivalent to FLT_MIN_EXP, DBL_MIN_EXP, LDBL_MIN_EXP. Footnote 189 Header (Table 17): . . . The contents are the same as the Standard C library header . 18.2.2p4 Other Languages Fortran 90 contains the inquiry function MINEXPONENT which performs a similar function. Common Implementations In IEC 60559 the value for singleprecision is 125 and for doubleprecision 1021. The two missing values (available in the biased notation used to represent the exponent) are used to represent 0.0, subnormals, ﬂoating types 338 inﬁnities, and NaNs. can represent Some implementations of GCC (e.g., MAC OS X) use two contiguous doubles to represent the type long double. In this case the value of DBL_MIN_EXP is greater, negatively, than LDBL_MIN_EXP (i.e., 1021 vs. 968). v 1.2 June 24, 2009
 5.2.4.2.2 Characteristics of ﬂoating types 372 Coding Guidelines The usage of these macros in existing code is so rare that reliable information on incorrect usage is not available, making it impossible to provide any guideline recommendations. (The rare usage could also imply that a guideline recommendation would not be worthwhile). 371 — minimum negative integer such that 10 raised to that power is in the range of normalized ﬂoatingpoint *_MIN_10_EXP numbers, log10 bemin −1 FLT_MIN_10_EXP 37 DBL_MIN_10_EXP 37 LDBL_MIN_10_EXP 37 Commentary Making this information available as an integer constant allows it to be accessed in a #if preprocessing directive. These are the exponent values for normalized numbers. If subnormal numbers are supported, the smallest 338 subnormal numbers representable value is likely to have an exponent whose value is FLT_DIG, DBL_DIG, and LDBL_DIG less than (toward negative inﬁnity) these values, respectively. The Committee is being very conservative in specifying the minimum values for the exponents of the types double and long double. An implementation is permitted to deﬁne the same range of exponents for all ﬂoatingpoint types. There may be normalized numbers whose respective exponent value is smaller than the values given for these macros; for instance, the exponents appearing in the *_MIN macros. The power of 10 exponent values given for these *_MIN_10_EXP macros can be applied to any normalized signiﬁcand. C++ static const int min_exponent10; 18.2.1.2p25 Minimum negative integer such that 10 raised to that power is in the range of normalised ﬂoating point numbers.190) Equivalent to FLT_MIN_10_EXP, DBL_MIN_10_EXP, LDBL_MIN_10_EXP. Footnote 190 Header (Table 17): . . . The contents are the same as the Standard C library header . 18.2.2p4 Common Implementations The value of DBL_MIN_10_EXP is usually the same as FLT_MIN_10_EXP or LDBL_MIN_10_EXP. In the latter case a value of 307 is often seen. 372 — maximum integer such that FLT_RADIX raised to one less than that power is a representable ﬁnite ﬂoating *_MAX_EXP point number, emax FLT_MAX_EXP DBL_MAX_EXP LDBL_MAX_EXP Commentary FLT_RADIX to the power *_MAX_EXP is the smallest large number that cannot be represented (because of 370 *_MIN_EXP limited exponent range). June 24, 2009 v 1.2
 374 5.2.4.2.2 Characteristics of ﬂoating types C++ 18.2.1.2p27 static const int max_exponent; Maximum positive integer such that radix raised to the power one less than that integer is a representable ﬁnite ﬂoating point number.191) Equivalent to FLT_MAX_EXP, DBL_MAX_EXP, LDBL_MAX_EXP. Footnote 191 Header (Table 17): . . . The contents are the same as the Standard C library header . 18.2.2p4 Other Languages Fortran 90 contains the inquiry function MAXEXPONENT which performs a similar function. Common Implementations In IEC 60559 the value for singleprecision is 128 and for doubleprecision 1024. *_MAX_10_EXP — maximum integer such that 10 raised to that power is in the range of representable ﬁnite ﬂoatingpoint 373 numbers, log10 ((1 − b−p )bemax ) FLT_MAX_10_EXP +37 DBL_MAX_10_EXP +37 LDBL_MAX_10_EXP +37 Commentary 371 *_MIN_10_EXP As in choosing the *_MIN_10_EXP values, the Committee is being conservative. C++ 18.2.1.2p29 static const int max_exponent10; Maximum positive integer such that 10 raised to that power is in the range of normalised ﬂoating point numbers. Equivalent to FLT_MAX_10_EXP, DBL_MAX_10_EXP, LDBL_MAX_10_EXP. Footnote 192 Header (Table 17): . . . The contents are the same as the Standard C library header . 18.2.2 Common Implementations The value of DBL_MAX_10_EXP is usually the same as FLT_MAX_10_EXP or LDBL_MAX_10_EXP. In the latter case a value of 307 is often seen. ﬂoating values The values given in the following list shall be replaced by constant expressions with implementationdeﬁned 374 listed values that are greater than or equal to those shown: v 1.2 June 24, 2009
 5.2.4.2.2 Characteristics of ﬂoating types 375 Commentary This is a requirement on the implementation. The requirement that they be constant expressions ensures that they can be used to initialize an object having static storage duration. 822 symbolic The values listed represent a ﬂoatingpoint number. Their equivalents in the integer domain are required name to have appropriate promoted types. There is no such requirement speciﬁed for these ﬂoatingpoint values. 303 integer types sizes C90 C90 did not contain the requirement that the values be constant expressions. C++ This requirement is not speciﬁed in the C++ Standard, which refers to the C90 Standard by reference. 375 — maximum representable ﬁnite ﬂoatingpoint number, (1 − b−p )bemax FLT_MAX 1E+37 DBL_MAX 1E+37 LDBL_MAX 1E+37 Commentary There is no requirement that the type of the value of these macros match the real type whose maximum they denote. Although some implementations include a representation for inﬁnity, the deﬁnition of these macros require the value to be ﬁnite. These values correspond to a FLT_RADIX value of 10 and the exponent values 373 given by the *_MAX_10_EXP macros. *_MAX_10_EXP The HUGE_VAL macro value may compare larger than any of these values. C++ static T max() throw(); 18.2.1.2p4 Maximum ﬁnite value.182 Equivalent to CHAR_MAX, SHRT_MAX, FLT_MAX, DBL_MAX, etc. Footnote 182 Header (Table 17): . . . The contents are the same as the Standard C library header . 18.2.2p4 Other Languages The class java.lang.Float contains the member: 1 public static final float MAX_VALUE = 3.4028235e+38f The class java.lang.Double contains the member: 1 public static final double MAX_VALUE = 1.7976931348623157e+308 Fortran 90 contains the inquiry function HUGE which performs a similar function. Common Implementations Many implementations use a sufﬁx to give the value a type corresponding to what the macro represents. The IEC 60559 values of these macros are: single float FLT_MAX 3.40282347e+38 double float DBL_MAX 1.7976931348623157e+308 380 EXAMPLE minimum ﬂoating point representa tion 381 EXAMPLE IEC 60559 ﬂoating June 24, 2009 v 1.2 point
 377 5.2.4.2.2 Characteristics of ﬂoating types Coding Guidelines How many calculations ever produce a value that is anywhere near FLT_MAX? The known Universe is thought to be 3×1029 mm in diameter, 5×1019 milliseconds old, and contain 1079 atoms, while the Earth is known to have a mass of 6×1024 Kg. Floatingpoint values whose magnitude approaches DBL_MAX, or even FLT_MAX are only likely to occur as the intermediate results of calculating a ﬁnal value. Very small numbers are easily created from values that do not quite cancel. Dividing by a very small value can lead to a very large value. Very large values are thus more often a symptom of a problem, rounding errors or poor handling of values that almost cancel, than of an application meaningful value. On overﬂow some processors saturate to the maximum representable value, while others return inﬁnity. Testing whether an operation will overﬂow is one use for these macros, e.g., does adding y to x overﬂow x > LDBL_MAX  y. In C99 the isinf macro might be used, e.g., isinf(x + y). Example 1 #include 2 3 #define FALSE 0 4 #define TRUE 1 5 6 extern float f_glob; 7 8 _Bool f(float p1, float p2) 9 { 10 if (f_glob > (FLT_MAX / p1)) 11 return FALSE; 12 13 f_glob *= p1; 14 15 if (f_glob > (FLT_MAX  p2)) 16 return FALSE; 17 18 f_glob += p2; 19 20 return TRUE; 21 } The values given in the following list shall be replaced by constant expressions with implementationdeﬁned 376 (positive) values that are less than or equal to those shown: Commentary ﬂoating val 374 ues listed The previous discussion is applicable here. *_EPSILON — the difference between 1 and the least value greater than 1 that is representable in the given ﬂoating point 377 type, b1−p FLT_EPSILON 1E5 DBL_EPSILON 1E9 LDBL_EPSILON 1E9 Commentary IEC 60559 29 The Committee is being very conservative in specifying these values. Although IEC 60559 arithmetic is in common use, there are several major ﬂoatingpoint implementations of it that do not support an extended v 1.2 June 24, 2009
 5.2.4.2.2 Characteristics of ﬂoating types 377 precision. The Committee could not conﬁdently expect implementations to support the type long double containing greater accuracy than the type double. Like the *_DIG macros more signiﬁcand digits are required for the types double and long double. 369 *_DIG macros Methods for obtaining the nearest predecessor and successor of any IEEE ﬂoatingpoint value are given by Rump, Zimmermann, Boldo, Melquiond.[1210] C++ static T epsilon() throw(); 18.2.1.2p20 Machine epsilon: the difference between 1 and the least value greater than 1 that is representable.187) Equivalent to FLT_EPSILON, DBL_EPSILON, LDBL_EPSILON. Footnote 187 Header (Table 17): . . . The contents are the same as the Standard C library header . 18.2.2p4 Other Languages Fortran 90 contains the inquiry function EPSILON, which performs a similar function. Common Implementations 368 long double Some implementations (e.g., Apple) use a contiguous pair of objects having type double to represent an Apple object having type long double. Such a representation creates a second meaning for LDBL_EPSILON. This is because, in such a representation, the least value greater than 1.0 is 1.0+LDBL_MIN, a difference of LDBL_MIN (which is not the same as b(1−p) )— the correct deﬁnition of *_EPSILON. Their IEC 60559 values are: FLT_EPSILON 1.19209290e7 /* 0x1p23 */ DBL_EPSILON 2.2204460492503131e16 /* 0x1p52 */ Coding Guidelines It is a common mistake for these values to be naively used in equality comparisons: 1 #define EQUAL_DBL(x, y) ((((x)DBL_EPSILON) < (y)) && \ 2 (((x)+DBL_EPSILON) > (y))) This test will only work as expected when x is close to 1.0. The difference value not only needs to scale with x, (x + x*DBL_EPSILON), but the value DBL_EPSILON is probably too small (equality within 1 ULP is a 346 ULP very tight bound): 1 #define EQUAL_DBL(x, y) ((((x)*(1.0MY_EPSILON)) < (y)) && \ 2 (((x)*(1.0+MY_EPSILON)) > (y))) Even this test fails to work as expected if x and y are subnormal values. For instance, if x is the smallest subnormal and y is just 1 ULP bigger, y is twice x. Another, less computationally intensive, method is to subtract the values and check whether the result is within some scaled approximation of zero. 1 #include 2 3 _Bool equalish(double f_1, double f_2) June 24, 2009 v 1.2
 378 5.2.4.2.2 Characteristics of ﬂoating types 4 { 5 int exponent; 6 frexp(((fabs(f_1) > fabs(f_2)) ? f_1 : f_2), &exponent); 7 return (fabs(f_1f_2) < ldexp(MY_EPSILON, exponent)); 8 } *_MIN — minimum normalized positive ﬂoatingpoint number, bemin −1 378 macros FLT_MIN 1E37 DBL_MIN 1E37 LDBL_MIN 1E37 Commentary These values correspond to a FLT_RADIX value of 10 and the exponent values given by the *_MIN_10_EXP 371 *_MIN_10_EXP macros. There is no requirement that the type of these macros match the real type whose minimum they subnormal 338 denote. Implementations that support subnormal numbers will be able to represent smaller quantities than numbers these. C++ 18.2.1.2p1 static T min() throw(); Maximum ﬁnite value.181) Equivalent to CHAR_MIN, SHRT_MIN, FLT_MIN, DBL_MIN, etc. Footnote 181 Header (Table 17): . . . The contents are the same as the Standard C library header . 18.2.2p4 Other Languages The class java.lang.Float contains the member: 1 public static final float MIN_VALUE = 1.4e45f; The class java.lang.Double contains the member: 1 public static final double MIN_VALUE = 5e324; which are the smallest subnormal, rather than normal, values. Fortran 90 contains the inquiry function TINY which performs a similar function. Common Implementations Their IEC 60559 values are: FLT_MIN 1.17549435e38f DBL_MIN 2.2250738585072014e308 Implementations without hardware support for ﬂoating point sometimes chose the minimum required limits because of the executiontime overhead in supporting additional bits in the ﬂoatingpoint representation. v 1.2 June 24, 2009
 5.2.4.2.2 Characteristics of ﬂoating types 379 Coding Guidelines How many calculations ever produce a value that is anywhere near as small as FLT_MIN? The hydrogen atom weighs 1026 Kg and has an approximate radius of 5×1011 meters, well within limits. But current theories on the origin of the Universe start at approximately 1036 seconds, a very small number. However, writers of thirdparty libraries might not know whether their users are simulating the Big Bang, or weighing groceries. They need to ensure that all cases are handled. Given everyday physical measurements, which don’t have very small values, where can very small numbers originate? Subtracting two ﬂoatingpoint quantities that differ by 1 ULP, for instance, produces a value that is approximately 105 smaller. Such a difference can result from random ﬂuctuations in the values input to a program, or because of rounding errors in calculations. Producing a value that is close to FLT_MIN invariably requires either a very complex calculation, or an iterative algorithm using values from previous iterations. Intermediate results that are expected to produce a value of zero may in fact deliver a very small value. Subsequent tests against zero fail and the very small value is passed through into further calculations. One solution to this problem is to have a relatively wide test of zeroness. In many physical systems a value that is a factor of 106 smaller than the smallest measurable quantity would be considered to be zero. Rev 378.1 Floatingpoint comparisons against zero shall take into account the physical properties or engineering tolerances of the system being controlled or simulated. There might be some uncertainty in the interpretation of the test (abs(x) < FLT_MIN); is it an approximate test against zero, or a test for a subnormal value? The C Standard now includes the fpclassify macro for obtaining the classiﬁcation of its argument, including subnormal. Example 1 #include 2 3 #define MIN_TOLERANCE (1e9) 4 5 _Bool inline effectively_zero(float valu) 6 { 7 return (abs(valu) < MIN_TOLERANCE); 8 } Recommended practice 379 Conversion from (at least) double to decimal with DECIMAL_DIG digits and back should be the identity function. DECIMAL_DIG conversion Commentary recommended practice Why is this a recommended practice? Unfortunately many existing implementations of printf and scanf do a poor job of base conversions, and they are not the identity functions. To claim conformance to both C99 and IEC 60559 (Annex F in force), the requirements of F.5 Binary decimal conversion must be met. Just making use of IEC 60559 ﬂoatingpoint hardware is not sufﬁcient. The I/O library can still be implemented incorrectly and the conversions be wrong. When the radix b is not a power of 10, it can be difﬁcult to ﬁnd a case where a decimal number with p× log10 b Rationale digits fails. Consider a fourbit mantissa system (that is, base b = 2 and precision p = 4) used to represent onedigit decimal numbers. While four bits are enough to represent onedigit numbers, they are not enough to support the conversions of decimal to binary and back to decimal in all cases (but they are enough for most cases). Consider a power of 2 that is just under 9.5e21, for example, 273 = 9.44e21. For this number, the three consecutive onedigit numbers near that special value and their roundtonearest representations are: June 24, 2009 v 1.2
 380 5.2.4.2.2 Characteristics of ﬂoating types 9e21 1e22 2e22 0xFp69 0x8p70 0x8p71 No problems so far; but when these representations are converted back to decimal, the values as threedigit numbers and the rounded onedigit numbers are: 8.85e21 9.44e21 1.89e22 9e21 9e21 2e22 and we end up with two values the same. For this reason, fourbit mantissas are not enough to start with any onedigit decimal number, convert it to a binary ﬂoatingpoint representation, and then convert back to the same onedigit decimal number in all cases; and so p radix b digits are (just barely) not enough to allow any decimal numbers with p× log10 b digits to do the roundtrip conversion. p radix b digits are enough, however, for (p − 1)× log10 b digits in all cases. The issues involved in performing correctly rounded decimaltobinary and binarytodecimal conversions are discussed mathematically by Gay.[484] C90 The Recommended practice clauses are new in the C99 Standard. C++ There is no such macro, or requirement speciﬁed in the C++ Standard. Other Languages The speciﬁcation of the Java base conversions is poor. Common Implementations Experience with testing various translators shows that the majority don’t, at the time of this publication, implement this Recommended Practice. The extent to which vendors will improve their implementations is unknown. There is a publicly available set of tests for testing binary to decimal conversions.[1413] Coding Guidelines A Recommended Practice shall not be relied on to be followed by an implementation. EXAMPLE EXAMPLE 1 The following describes an artiﬁcial ﬂoatingpoint representation that meets the minimum 380 minimum ﬂoating point representa requirements of this International Standard, and the appropriate values in a header for type float: tion 6 x = s16e fk 16−k , −31 ≤ e ≤ +32 k=1 FLT_RADIX 16 FLT_MANT_DIG 6 FLT_EPSILON 9.53674316E07F FLT_DIG 6 FLT_MIN_EXP 31 FLT_MIN 2.93873588E39F FLT_MIN_10_EXP 38 FLT_MAX_EXP +32 FLT_MAX 3.40282347E+38F FLT_MAX_10_EXP +38 v 1.2 June 24, 2009
 5.2.4.2.2 Characteristics of ﬂoating types 382 Commentary Note that this example has a FLT_RADIX of 16, not 2. 381 EXAMPLE 2 The following describes ﬂoatingpoint representations that also meet the requirements for EXAMPLE IEC 60559 singleprecision and doubleprecision normalized numbers in IEC 60559,20) and the appropriate values in a ﬂoatingpoint header for types float and double: 24 xf = s2e fk 2−k , −125 ≤ e ≤ +128 k=1 53 xd = s2e fk 2−k , −1021 ≤ e ≤ +1024 k=1 FLT_RADIX 2 DECIMAL_DIG 17 FLT_MANT_DIG 24 FLT_EPSILON 1.19209290E07F // decimal constant FLT_EPSILON 0X1P23F // hex constant FLT_DIG 6 FLT_MIN_EXP 125 FLT_MIN 1.17549435E38F // decimal constant FLT_MIN 0X1P126F // hex constant FLT_MIN_10_EXP 37 FLT_MAX_EXP +128 FLT_MAX 3.40282347E+38F // decimal constant FLT_MAX 0X1.fffffeP127F // hex constant FLT_MAX_10_EXP +38 DBL_MANT_DIG 53 DBL_EPSILON 2.2204460492503131E16 // decimal constant DBL_EPSILON 0X1P52 // hex constant DBL_DIG 15 DBL_MIN_EXP 1021 DBL_MIN 2.2250738585072014E308 // decimal constant DBL_MIN 0X1P1022 // hex constant DBL_MIN_10_EXP 307 DBL_MAX_EXP +1024 DBL_MAX 1.7976931348623157E+308 // decimal constant DBL_MAX 0X1.fffffffffffffP1023 // hex constant DBL_MAX_10_EXP +308 If a type wider than double were supported, then DECIMAL_DIG would be greater than 17. For example, if the widest type were to use the minimalwidth IEC 60559 doubleextended format (64 bits of precision), then DECIMAL_DIG would be 21. Commentary The values given here are important in that they are the most likely values to be provided by a conforming implementation using IEC 60559, which is what the majority of modern implementations use. These values correspond to the IEC 60559 single and doubleprecision formats. This standard also deﬁnes extended single and extended double formats, which contain more bits in the signiﬁcand and greater range in the exponent. Note that this example gives the decimal and hexadecimal ﬂoatingconstant representation for some of the macro deﬁnitions. A real header will only contain one of these deﬁnitions. C90 The C90 wording referred to the ANSI/IEEE754–1985 standard. June 24, 2009 v 1.2
 384 6.1 Notation footnote 20) The ﬂoatingpoint model in that standard sums powers of b from zero, so the values of the exponent limits 382 20 are one less than shown here. Commentary Fortran counts from 1, not 0 and the much of the contents of , in C90, came from Fortran. Forward references: conditional inclusion (6.10.1), complex arithmetic (7.3), extended multibyte 383 and wide character utilities (7.24), ﬂoatingpoint environment (7.6), general utilities (7.20), input/output (7.19), mathematics (7.12). 6. Language 6.1 Notation In the syntax notation used in this clause, syntactic categories (nonterminals) are indicated by italic type, 384 and literal words and character set members (terminals) by bold type. Commentary A terminal is a token that can appear in the source code. A nonterminal is the name of a syntax rule used to group together zero or more terminals and other nonterminals. The nonterminals can be viewed as a tree. The root is the nonterminal translationunit. The terminals are the leaves of this tree. Syntax analysis is the processing of a sequence of terminals (as written in the source) via various nonterminals until the nonterminal translationunit is reached. Failure to reach this ﬁnal nonterminal, or encountering an unexpected sequence of tokens, is a violation of syntax. The syntax notation used in the C Standard is not overly formal; it is often supported by text in the semantics clause. The C syntax can be written in LALR(1) form. (Although some reorganization of the productions listed in the standard is needed), assuming the typedef issue is fudged (the only way to know whether an identiﬁer is a typedef name or not is to look it up in a symbol table, which introduces a context dependency; the alternative of syntactically treating a typedef name as an identiﬁer requires more than one token lookahead.) This also happens to be the class of grammars that can be processed by yacc and many other parser generators. 1 A(B) /* Declare B to have type A, or call function A with argument B? */ The syntax speciﬁed in the C Standard effectively describes four different grammars: preprocess 770 ing token 1. A grammar whose start symbol is preprocessingtoken; the input stream processed by this grammar syntax transla 118 contains the source characters output by translation phase 2. tion phase 2 preprocessor 1854 2. A grammar whose start symbol is preprocessingfile; the input stream processed by this grammar directives syntax contains the preprocessingtokens output by translation phase 3. transla 124 tion phase 3 3. A grammar whose start symbol is token; the input to this grammar is a single preprocessingtoken. token 770 syntax The syntax of the characters forming the preprocessingtoken need to form a valid parse of the token syntax. transla 1810 tion unit syntax 4. A grammar whose start symbol is translationunit; the input stream processed by this grammar transla 135 tion phase contains the tokens output by translation phase 6. 6 The preprocessortoken and token syntax is sometimes known as the lexical grammar of C. There are many factors that affect the decision of whether to specify language constructs using syntax or English prose in a Constraints clause. The C Standard took the approach of having a relatively simple, general syntax speciﬁcation and using wording in constraints clauses to handle the special cases. There are techniques available (e.g., twolevel grammars) for specifying the requirements (including the type rules) v 1.2 June 24, 2009
 6.1 Notation 385 through syntax. This approach was famously taken, because of its impenetrability, in the speciﬁcation of Algol 68[1440] The ﬁrst published standard for syntax notation was BS 6154:1981, Method of Deﬁning— Syntactic metalanguage. Although a British rather than an International standard this document was widely circulated. It was also used as the base document for ISO/IEC 14977,[658] which speciﬁes an extended BNF. Most compiler books limit their discussion to LR and LL related methods. For a good introduction to a variety of parsing methods see Grune and Jacobs,[535] which is now freely available online. C++ In the syntax notation used in this International Standard, syntactic categories are indicated by italic type, and 1.6p1 literal words and characters in constant width type. The C++ grammar contains signiﬁcantly more syntactic ambiguities than C. Some implementations have used mainly syntactic approaches to resolving these,[1501] while others make use of semantic information to guide the parse.[494] For instance, knowing what an identiﬁer has been declared as, simpliﬁes the parsing of the following: 1 template_name < a , b >  5 // equivalent to (template_name < a , b >)  5) 2 non_template_name < a , b >  5 // equivalent to (non_template_name < a) , (b >  5) Other Languages The ﬁrst version of many language speciﬁcations was created prior to the publication of the Extended BNF standard, ISO/IEC 14977,[658] and consequently the conventions used by them to express BNF varies.[1232] Most computer languages deﬁnitions use some form of semiformal notation to deﬁne their syntax. The availability of parser generators is an incentive to try to ensure that the syntax is, or can be, rewritten in LALR(1) form. Some languages are linebased, for instance Fortran (prior to Fortran 90). Each statement or declaration has its syntax and sequences of them can be used to build functions; there is no nesting of constructs over multiple statements. The method used to analyze the syntax of a language can be inﬂuenced by several factors. Many Pascal translators used a handwritten recursive descent parser; the original, widely available, implementation of the language uses just this technique. Many Fortran translators use ad hoc techniques to process each statement or declaration as it is encountered, the full power of a parser generator not being necessary (also, it is easier to perform error recovery in an ad hoc approach). Common Implementations Most implementations use an automated toolbased approach to analyzing some aspects of the C syntax. Although tools do exist for automating the lexical grammar (e.g., lex) a handwritten lexer offers greater ﬂexibility for handling erroneous input. The tool almost universally used to handle the language syntax is yacc, or one of its derivatives. The preprocessor syntax is usually handled in a handwritten ad hoc manner, although a few implementations use an automated tool approach. Tools that do not require the input to be consolidated into tokens, but parsed at the character sequence level, are available.[1457] Coding Guidelines It is unlikely that a coding guideline recommendation would specify language syntax unless an extension was being discussed. Coding guideline documents that place restrictions on the syntax that can be used are rare. 385 A colon (:) following a nonterminal introduces its deﬁnition. June 24, 2009 v 1.2
 389 6.2 Concepts Commentary This is a simple notation. Some syntax notations have been known to be almost as complex as the languages they describe. C++ The C++ Standard does not go into this level of detail (although it does use this notation). Other Languages The notation ::= is used in some language deﬁnitions and more formal speciﬁcations. The notation = is speciﬁed by the ISO Standard for extended BNF.[658] Alternative deﬁnitions are listed on separate lines, except when prefaced by the words “one of”. 386 Commentary The Syntax clauses in the standard are written in an informal notation. As typiﬁed by this simple, nonformal rule. Other Languages The character  often used to indicate alternative deﬁnitions. This character is used by ISO/IEC 14977. An optional symbol is indicated by the subscript “opt”, so that 387 { expressionopt } indicates an optional expression enclosed in braces. Commentary This is a more informal notation. In this example expression is optional; the braces are not optional. Other Languages Some languages use a more formal syntax notation where an empty alternative indicates that it is possible for a nonterminal to match against nothing (i.e., the symbol is optional). When syntactic categories are referred to in the main text, they are not italicized and words are separated by 388 spaces instead of hyphens. Commentary There are some places in the main text where words are separated by hyphens instead of spaces. C90 This convention was not explicitly speciﬁed in the C90 Standard. C++ The C++ Standard does not explicitly specify the conventions used. However, based on the examples given in clause 1.6 and usage within the standard, the conventions used appear to be the reverse of those used in C (i.e., syntactic categories are italicized and words are separated by hyphens). Coding Guidelines Companies may have typographical conventions for their documents which differ from those used by ISO. The issue of which typographical conventions to use in a company’s coding guideline document is outside the scope of these coding guidelines. A summary of the language syntax is given in annex A. 389 v 1.2 June 24, 2009
 6.2.1 Scopes of identiﬁers 391 C90 The summary appeared in Annex B of the C90 Standard, and this fact was not pointed out in the normative text. 6.2 Concepts Commentary The concepts introduced here are identiﬁers (and their associated attributes), objects, and types. 6.2.1 Scopes of identiﬁers 390 An identiﬁer can denote an object; scope of identiﬁers Commentary This association is created by an explicit declaration. Some objects do not have names— they are anonymous. An anonymous object can be created by a call to a memoryallocation function; an unnamed bitﬁeld can be denoted by the identiﬁer deﬁned to have its 1414 bitﬁeld unnamed containing union type. Other Languages Many languages use the term variable to denote what the C Standard calls an object. All languages provide a mechanism for declaring identiﬁers to denote objects (or variables). Coding Guidelines The use to which an identiﬁer is put has been known to inﬂuence the choice of its name. This issue is fully 792 naming discussed elsewhere. conventions allocated automatic static storage duration type object ordinary block typedef member file function name space scope function prototype tag label identifier function name linkage external internal none Figure 389.1: Attributes a C language identiﬁer can have. June 24, 2009 v 1.2
 394 6.2.1 Scopes of identiﬁers a function; 391 Commentary It is not possible to deﬁne an anonymous function within a strictly conforming program. Machine code can be copied into an object and executed on some implementations. Such copying and execution does not create something that is normally thought of as a function. Coding Guidelines Various coding guideline documents have recommended that names based on various parts of speech be used in the creating of identiﬁer names denoting function. These recommendations are invariably biased towards the parts of speech found in English. (Not speaking any other human language, your author has not read documents written in other languages but suspects they are also biased toward their respective languages.) identiﬁer 792 The general issue of identiﬁer names is discussed elsewhere. introduction a tag or a member of a structure, union, or enumeration; 392 Commentary It is possible to create anonymous structure and union types, and anonymous members within these types. It is not possible to create an anonymous member of an enumeration. Even if there are gaps in the values assigned to each enumeration constant, there are no implicit named members. C++ The C++ Standard does not deﬁne the term tag. It uses the terms enumname (7.2p1) for enumeration deﬁnitions and classname (9p1) for classes. Common Implementations Some translators support anonymous structure and union members within a structure deﬁnition; there was even a C9X revision proposal, WG14/N498 (submitted by one Ken Thompson): 1 struct S { 2 int mem1; 3 union { 4 long umem1; 5 float umem2; 6 }; /* No member name. */ 7 } s_o; 8 9 void f(void) 10 { 11 s_o.umem1=99; /* Translator deduces ’expected’ member. */ 12 } There are several implementations that support this form of declaration. It is also supported in C++. a typedef name; 393 Commentary Character sequences such as char, short and int are types, not typedef names. They are also language keywords— a different lexical category from identiﬁers. Common Implementations Typedef names are speciﬁed as being identiﬁers, not keywords. However, most implementations treat them as a different syntactic terminal from identiﬁers. Translators usually performs a symbol table lookup just transla 136 tion phase before translation phase 7, to see if an identiﬁer is currently deﬁned as a typedef name. This differentiation is 7 made to simplify the parsing of C source and not visible to the user of a translator. v 1.2 June 24, 2009
 6.2.1 Scopes of identiﬁers 397 Coding Guidelines 792 typedef The issue of naming conventions typedef names is discussed elsewhere. naming conven tions 394 a label name; label name Commentary A label name occurs in a different syntactic context to other kinds of identiﬁers, which is how a translator 440 label name space 1722 labeled deduces it is a label. statements syntax Other Languages Fortran and Pascal require that label names consist of digits only. Coding Guidelines 792 label The issue of naming conventions for label names is discussed elsewhere. naming con ventions 395 a macro name; Commentary 1974 macro An identiﬁer can only exist as a macro name during translation phases 3 and 4. deﬁnition lasts until 124 transla Coding Guidelines tion phase 3 129 transla The issue of typedef names is discussed elsewhere. tion phase 4 792 macro naming con 396 or a macro parameter. ventions identiﬁer macro parameter Commentary 124 transla An identiﬁer can only exist as a macro parameter during translation phases 3 and 4. tion phase 3 Function parameters are objects, not a special kind of identiﬁer. 129 transla tion phase 4 C++ 71 parameter The C++ Standard does not list macro parameters as one of the entities that can be denoted by an identiﬁer. Coding Guidelines 792 typedef The issue of typedef names is discussed elsewhere. naming conven tions 397 The same identiﬁer can denote different entities at different points in the program. identiﬁer denote differ ent entities Commentary The word entities is just a way of naming the different kinds of things that an identiﬁer can refer to in C. The concept of scope allows the same identiﬁer to be deﬁned in a different scope, in the same name space, to refer to another entity. The concept of name space allows the same identiﬁer to be deﬁned in a different 400 scope name space, in the same scope, to refer to another entity. 438 name space C++ The C++ Standard does not explicitly state this possibility, although it does include wording (e.g., 3.3p4) that implies it is possible. Other Languages Languages that support some kind of scoping rules usually allow the same identiﬁer to denote different entities at different points in a program. Cobol has a single scope and requires that identiﬁers be deﬁned in a declaration section. This severely limits the extent to which the same identiﬁer can be used to denote different entities. However, members of the structure data type can use the same names in different types. Some dialects of Basic have a single scope. June 24, 2009 v 1.2
 397 6.2.1 Scopes of identiﬁers external none T o T M o f L internal T M f M o File o t Block Prototype t e o Function o e f e f Label Tag Structure Macro object function typedef Identiﬁer enum const Figure 397.1: All combinations of linkage, scope, and name space that all possible kinds of identiﬁers, supported by C, can have. M refers to the members of a structure or union type. There is a separate name space for macro names and they have no linkage, but their scope has no formally speciﬁed name. Coding Guidelines Some of the costs and beneﬁts of using the same identiﬁer to denote different entities include: • costs: an increase in the number of developer miscomprehensions when reading the source. For instance, the same identiﬁer deﬁned as a ﬁle scope, static linkage, object in one source ﬁle and a ﬁle scope, extern linkage, object in another source ﬁle could mislead the unwary developer, who was not aware of the two uses of the same name, into making incorrect assumptions about an assignment to one of the objects. The confusability of one identiﬁer with another identiﬁer is one of the major issues identiﬁer 792 of identiﬁer naming and is discussed elsewhere, confusability • beneﬁts: an increase in reader recall performance through consistent use of identiﬁer names to denote identiﬁer 792 semantic as sociations the same set of semantic attributes. For instance, having the same identiﬁer denoting a member in different structures can indicate that they all have the same usage (e.g., x_cord to hold the position on member 792 naming con ventions the x axis in a graph drawing program). identiﬁer 792 The issues involved in making cost/beneﬁt tradeoffs related to identiﬁer spelling and the role played by an syntax object 1352 identiﬁer and are discussed elsewhere. role Example 1 #define SAME(SAME) SAME /* Macro and macro parameter name spaces. */ 2 /* 3 * Additional lack of clarity could be obtained by replacing v 1.2 June 24, 2009
CÓ THỂ BẠN MUỐN DOWNLOAD

The C++ Standard Library: A Tutorial and Reference  Nicolai M. Josuttis
1190 p  72  32

The New C Standard P8
100 p  43  6

The New C Standard P7
100 p  58  6

The New C Standard P15
100 p  48  6

The New C Standard P2
100 p  50  5

The New C Standard P13
100 p  41  5

The New C Standard P6
100 p  57  5

The New C Standard P11
100 p  59  5

The New C Standard P12
100 p  44  4

The New C Standard P9
100 p  43  4

The New C Standard P10
100 p  50  4

The New C Standard P16
112 p  49  4

The New C Standard P14
100 p  49  4

The New C Standard P1
100 p  42  3

The New C Standard P4
100 p  35  3

The New C Standard P3
100 p  36  3

Ebook Beyond the C++ Standard Library
225 p  18  3