Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Floating constant

      From cppreference.com
      <c‎ |language
       
       
       
       

      Allows values of floating type to be used directly in expressions.

      Contents

      [edit]Syntax

      A floating constant is anon-lvalue expression having the form:

      significandexponent (optional)suffix (optional)

      Where thesignificand has the form

      whole-number (optional).(optional)fraction (optional)

      Theexponent has the form

      e |Eexponent-sign (optional)digit-sequence (1)
      p |Pexponent-sign (optional)digit-sequence (2)(since C99)
      1) The exponent syntax for a decimal floating-point constant
      2) The exponent syntax for hexadecimal floating-point constant

      Optional single quotes (') can be inserted between the digits as a separator, they are ignored when compiling.

      (since C23)

      [edit]Explanation

      If thesignificand begins with the character sequence0x or0X, the floating constant is ahexadecimal floating constant. Otherwise, it is adecimal floating constant.

      For ahexadecimal floating constant, thesignificand is interpreted as a hexadecimal rational number, and thedigit-sequence of the exponent is interpreted as the integer power of 2 to which the significand has to be scaled.

      double d= 0x1.2p3;// hex fraction 1.2 (decimal 1.125) scaled by 2^3, that is 9.0
      (since C99)

      For adecimal floating constant, thesignificand is interpreted as a decimal rational number, and thedigit-sequence of the exponent is interpreted as the integer power of 10 to which the significand has to be scaled.

      double d=1.2e3;// decimal fraction 1.2 scaled by 10^3, that is 1200.0

      [edit]Suffixes

      An unsuffixed floating constant has typedouble. Ifsuffix is the letterf orF, the floating constant has typefloat. Ifsuffix is the letterl orL, the floating constant has typelongdouble.

      If the implementation predefines macro__STDC_IEC_60559_BFP__, the following suffixes and corresponding floating constants are additionally supported:

      • ifsuffix isdf orDF, the floating constant has type_Decimal32;
      • ifsuffix isdd orDD, the floating constant has type_Decimal64;
      • ifsuffix isdl orDL, the floating constant has type_Decimal128.

      Suffixes for decimal floating-point types are not allowed in hexadecimal floating constants.

      (since C23)

      [edit]Optional parts

      If the exponent is present and fractional part is not used, the decimal separator may be omitted:

      double x=1e0;// floating-point 1.0 (period not used)

      For decimal floating constants, theexponent part is optional. If it is omitted, the period is not optional, and either thewhole-number or thefraction must be present.

      double x=1.;// floating-point 1.0 (fractional part optional)double y=.1;// floating-point 0.1 (whole-number part optional)

      For hexadecimal floating constants, the exponent is not optional to avoid ambiguity resulting from anf suffix being mistaken as a hexadecimal digit.

      (since C99)

      [edit]Representable values

      The result of evaluating a floating constant 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 (in other words,default rounding direction during translation is implementation-defined).

      All floating constants of the same source form convert to the same internal format with the same value. Floating constants of different source forms, e.g.1.23 and1.230, need not to convert to the same internal format and value.

      Floating-point constants may convert to more range and precision than is indicated by their type, if indicated byFLT_EVAL_METHOD. For example, the constant0.1f may act as if it were0.1L in an expression.

      The result of evaluating a hexadecimal floating constant, ifFLT_RADIX is 2, is the exact value represented by the floating constant, correctly rounded to the target type.

      (since C99)

      Floating constants of decimal floating-point type that have the same numerical value\(\small x\)x but different quantum exponents, e.g.1230.dd,1230.0dd, and1.23e3dd, have distinguishable internal representations.

      The quantum exponent\(\small q\)q of a floating constant of a decimal floating-point type is determined in the manner that\(\small 10^q\)10q
      represents 1 at the place of last digit of thesignificand when possible. If the quantum exponent\(\small q\)q and the coefficient\(\small c = x\cdot 10^{-q}\)c=x·10-q
      determined above cannot represented exactly in the type of the floating constant,\(\small q\)q is increased as needed within the limit of the type, and\(\small c\)c is reduced correspondingly, with needed rounding. The rounding may result in zero or infinity. If (the possibly rounded)\(\small c\)c is still out of the permitted range after\(\small q\)q reaches the maximum value, the resulted floating constant has value positive infinity.

      (since C23)

      [edit]Notes

      Defaultrounding direction andprecision are in effect when the floating constants are converted into internal representations, andfloating-point exceptions are not raised even if#pragma STDC FENV_ACCESS is in effect (for execution-time conversion of character strings,strtod can be used). Note that this differs fromarithmetic constant expressions of floating type.

      Letters in the floating constants are case-insensitive, except that uppercase and lowercase cannot be both used in suffixes for decimal floating-point types(since C23):0x1.ep+3 and0X1.EP+3 represent the same floating-point value15.0.

      The decimal point specified bysetlocale has no effect on the syntax of floating constants: the decimal point character is always the period.

      Unlike integers, not every floating value can be represented directly by decimalor even hexadecimal(since C99) constant syntax: macrosNAN andINFINITY as well as functions such asnan offer ways to generate those special values(since C99). Note that0x1.FFFFFEp128f, which might appear to be an IEEE float NaN, in fact overflows to an infinity in that format.

      There are no negative floating constants; an expression such as-1.2 is thearithmetic operator unary minus applied to the floating constant1.2. Note that the special value negative zero may be constructed with-0.0.

      [edit]Example

      Run this code
      #include <stdio.h> int main(void){printf("15.0     = %a\n",15.0);printf("0x1.ep+3 = %f\n",0x1.ep+3); // Constants outside the range of type double.printf("+2.0e+308 --> %g\n",2.0e+308);printf("+1.0e-324 --> %g\n",1.0e-324);printf("-1.0e-324 --> %g\n",-1.0e-324);printf("-2.0e+308 --> %g\n",-2.0e+308);}

      Output:

      15.0     = 0x1.ep+30x1.ep+3 = 15.000000+2.0e+308 --> inf+1.0e-324 --> 0-1.0e-324 --> -0-2.0e+308 --> -inf

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.4.4.2 Floating constants (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.4.4.2 Floating constants (p: 47-48)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.4.4.2 Floating constants (p: 65-66)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.4.4.2 Floating constants (p: 57-58)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.1.3.1 Floating constants

      [edit]See also

      C++ documentation forFloating-point literal
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/floating_constant&oldid=153211"

      [8]ページ先頭

      ©2009-2025 Movatter.jp