Namespaces
Variants

Floating-point literal

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

浮動小数点リテラルは、ソースファイル内で値が指定されるコンパイル時定数を定義します。

目次

構文

digit-sequence decimal-exponent suffix  (任意) (1)
digit-sequence . decimal-exponent  (任意) suffix  (任意) (2)
digit-sequence  (任意) . digit-sequence decimal-exponent  (任意) suffix  (任意) (3)
0x | 0X hex-digit-sequence hex-exponent suffix  (任意) (4) (C++17以降)
0x | 0X hex-digit-sequence . hex-exponent suffix  (任意) (5) (C++17以降)
0x | 0X hex-digit-sequence  (任意) . hex-digit-sequence hex-exponent suffix  (任意) (6) (C++17以降)
1) digit-sequence が小数点区切りなしの整数を表す場合、指数部は省略できません: 1e10 , 1e - 5L .
2) digit-sequence が小数点区切りを持つ整数を表す場合、指数部は任意です: 1 . , 1. e -2 .
3) 小数を表す数字列 指数部は任意です: 3.14 , .1f , 0.1e - 1L .
4) 基数区切り文字なしの整数を表す16進 digit-sequence 。16進浮動小数点リテラルでは指数部が常に必須です: 0x1ffp10 0X0p - 1
5) 基数区切りを持つ整数を表す16進数 digit-sequence 。16進浮動小数点リテラルでは指数部は常に必須です: 0x1 . p0 0xf . p - 1
6) 16進数 digit-sequence による小数表現。基数区切りを持つ16進浮動小数点数リテラルでは、指数部は常に必須です: 0x0.123p - 1 , 0xa . bp10l .

decimal-exponent は次の形式を持ちます

e | E 指数符号  (オプション) 数字列

hex-exponent は以下の形式を持ちます

p | P 指数記号  (オプション) 数字列 (C++17以降)

exponent-sign が存在する場合、 + または - のいずれかです

サフィックス が存在する場合、それは f l F L f16 f32 f64 f128 bf16 F16 F32 F64 F128 BF16 (C++23以降) のいずれかです。サフィックスは浮動小数点リテラルの型を決定します:

  • (接尾辞なし)は double を定義します
  • f F float を定義します
  • l L long double を定義します
  • f16 F16 std::float16_t を定義
  • f32 F32 std::float32_t を定義
  • f64 F64 std::float64_t を定義
  • f128 F128 std::float128_t を定義
  • bf16 BF16 std::bfloat16_t を定義
(C++23以降)

オプションの単一引用符 ( ' ) を数字の間に区切り文字として挿入できます。これらはリテラルの値を決定する際に無視されます。

(C++14以降)

説明

10進数の科学的表記法が使用され、浮動小数点リテラルの値は仮数部に10の decimal-exponent 乗を掛けた値となる。例えば、 123e4 の数学的な意味は 123×10 4 である。

浮動小数点リテラルが文字シーケンス 0x または 0X で始まる場合、その浮動小数点リテラルは 16進浮動小数点リテラル です。それ以外の場合は 10進浮動小数点リテラル です。

16進浮動小数点リテラル の場合、仮数部は16進有理数として解釈され、指数部の digit-sequence は仮数部をスケーリングするための2の(10進)整数乗として解釈されます。

double d = 0x1.4p3 ; // 16進分数 1.4(10進数 1.25)を 2 3 でスケーリング、つまり 10.0

(C++17以降)

注記

16進浮動小数点リテラルはC++17までC++の一部ではありませんでしたが、C++11以降はI/O関数で解析および出力可能です: std::hexfloat が有効な場合のC++ I/Oストリームと、C I/Oストリーム: std::printf std::scanf など。書式の詳細については std::strtof を参照してください。

機能テストマクロ 規格 機能
__cpp_hex_float 201603L (C++17) 16進浮動小数点リテラル

#include <iomanip>
#include <iostream>
#include <limits>
#include <typeinfo>
#define OUT(x) '\n' << std::setw(16) << #x << x
int main()
{
    std::cout
        << "Literal" "\t" "Printed value" << std::left
        << OUT( 58.            ) // double
        << OUT( 4e2            ) // double
        << OUT( 123.456e-67    ) // double
        << OUT( 123.456e-67f   ) // float, truncated to zero
        << OUT( .1E4f          ) // float
        << OUT( 0x10.1p0       ) // double
        << OUT( 0x1p5          ) // double
        << OUT( 0x1e5          ) // integer literal, not floating-point
        << OUT( 3.14'15'92     ) // double, single quotes ignored (C++14)
        << OUT( 1.18e-4932l    ) // long double
        << std::setprecision(39)
        << OUT( 3.4028234e38f  ) // float
        << OUT( 3.4028234e38   ) // double
        << OUT( 3.4028234e38l  ) // long double
        << '\n';
    static_assert(3.4028234e38f == std::numeric_limits<float>::max());
    static_assert(3.4028234e38f ==  // ends with 4
                  3.4028235e38f);   // ends with 5
    static_assert(3.4028234e38 !=   // ends with 4
                  3.4028235e38);    // ends with 5
    // Both floating-point constants below are 3.4028234e38
    static_assert(3.4028234e38f !=  // a float (then promoted to double)
                  3.4028234e38);    // a double
}

出力例:

Literal         Printed value
58.             58
4e2             400
123.456e-67     1.23456e-65
123.456e-67f    0
.1E4f           1000
0x10.1p0        16.0625
0x1p5           32
0x1e5           485
3.14'15'92      3.14159
1.18e-4932l     1.18e-4932
3.4028234e38f   340282346638528859811704183484516925440
3.4028234e38    340282339999999992395853996843190976512
3.4028234e38l   340282339999999999995912555211526242304

参考文献

  • C++23標準 (ISO/IEC 14882:2024):
  • 5.13.4 浮動小数点リテラル [lex.fcon]
  • C++20標準 (ISO/IEC 14882:2020):
  • 5.13.4 浮動小数点リテラル [lex.fcon]
  • C++17標準 (ISO/IEC 14882:2017):
  • 5.13.4 浮動小数点リテラル [lex.fcon]
  • C++14標準 (ISO/IEC 14882:2014):
  • 2.14.4 浮動小数点リテラル [lex.fcon]
  • C++11標準 (ISO/IEC 14882:2011):
  • 2.14.4 浮動小数点リテラル [lex.fcon]
  • C++98標準 (ISO/IEC 14882:1998):
  • 2.13.3 浮動小数点リテラル [lex.fcon]

関連項目

ユーザー定義リテラル (C++11) ユーザー定義サフィックスを持つリテラル
Cドキュメント for 浮動小数点定数