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) 表示不带小数点分隔符的整数的 数字序列 ,此时指数部分不可省略: 1e10 1e - 5L
2) 表示带十进制分隔符的整数的 数字序列 ,此时指数部分是可选的: 1 . 1. e -2
3) 数字序列 表示小数。指数部分是可选的: 3.14 .1f 0.1e - 1L
4) 表示不带基数分隔符的整数的十六进制 数字序列 。对于十六进制浮点字面量,指数部分始终不可省略: 0x1ffp10 0X0p - 1
5) 表示带基数分隔符整数的十六进制 数字序列 。对于十六进制浮点字面量,指数部分从不省略: 0x1 . p0 0xf . p - 1
6) 表示带基数分隔符的十六进制小数位序列。十六进制浮点数字面值的指数部分始终不可省略: 0x0.123p - 1 0xa . bp10l

decimal-exponent 具有以下形式

e | E 指数符号  (可选) 数字序列

hex-exponent 具有以下形式

p | P 指数符号  (可选) 数字序列 (自 C++17 起)

指数符号 (如果存在)可以是 + -

后缀 (若存在)应为 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 起)

可在数字之间插入可选的单引号( ' )作为分隔符;在确定字面值时会忽略这些分隔符。

(since C++14)

说明

十进制科学记数法被使用,意味着浮点数字面值的值等于有效数字乘以10的 十进制指数 次幂。例如 123e4 的数学含义是 123×10 4

若浮点数字面值以字符序列 0x 0X 开头,则该浮点数字面值为 十六进制浮点数字面值 。否则,它是 十进制浮点数字面值

对于 十六进制浮点数字面值 ,其有效数被解释为十六进制有理数,而指数的 数字序列 被解释为对有效数进行缩放所需的2的(十进制整数)幂次。

double d = 0x1.4p3 ; // 十六进制分数 1.4(十进制 1.25)乘以 2 3 缩放,结果为 10.0

(C++17 起)

注释

十六进制浮点数字面量直到 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) 十六进制浮点数字面量

示例

#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 文档 关于 浮点常量