首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

floating point literal

浮点文字定义了一个编译时间常数,其值在源文件中指定.

句法

significand exponent(optional) suffix(optional)

?

?

其中的意义有以下形式之一。

digit-sequence

(1)

?

digit-sequence .

(2)

?

digit-sequence(optional) . digit-sequence

(3)

?

0x | 0X hex-digit-sequence

(4)

(since C++17)

0x | 0X hex-digit-sequence .

(5)

(since C++17)

0x | 0X hex-digit-sequence(optional) . hex-digit-sequence

(6)

(since C++17)

1%29位数列,表示没有小数分隔符的整数,在这种情况下,指数不是可选的:1e10,,,1e-5L

2%29位数列,用小数分隔符表示整数,在这种情况下,指数是可选的:1.,,,1.e-2

3%29位序列,代表小数.。指数是可选的:3.14,,,.1f,,,0.1e-1L

4%,29十六进制数字序列,表示一个没有基数分隔符的整数.。对于十六进制浮点文本,指数从来都不是可选的:0x1ffp10,,,0X0p-1

5%,29十六进制数字序列,代表一个整数,带有基数分隔符.。对于十六进制浮点文本,指数从来都不是可选的:0x1.p0,,,0xf.p-1

6%29十六进制数字序列,表示带有基数分隔符的小数.。对于十六进制浮点文本,指数从来都不是可选的:0x0.123p-1,,,0xa.bp10l

指数有形式。

e | E exponent-sign(optional) digit-sequence

(1)

?

p | P exponent-sign(optional) digit-sequence

(2)

(since C++17)

1%29十进制浮点文字的指数语法

2%29十六进制浮点文字的指数语法

如果存在指数符号,则为+-

后缀,如果存在,则为f,,,F,,,l,或L.后缀确定浮点文字的类型:

  • %28no后缀%29定义double
  • f F定义float
  • l L定义long double

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

(since C++14)

解释

使用十进制科学表示法,这意味着浮点文字的值是数字10的意义和复数。的数学意义123e4123×104...

If the significand begins with the character sequence 0x or 0X, the floating constant is a hexadecimal floating constant. Otherwise, it is a decimal floating constant. For a hexadecimal floating constant, the significand is interpreted as a hexadecimal rational number, and the digit-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 C++17)

二次

代码语言:javascript
复制
#include <iostream>
int main()
{
  std::cout << 123.456e-67 << '\n'
            << .1E4f       << '\n'
            << 58.         << '\n'
            << 4e2         << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
1.23456e-65
1000
58
400

二次

注记

十六进制浮点文字直到C++17才成为C++的一部分,尽管它们可以被I/O函数解析和打印,因为C++11:C++I/O流std::hexfloat启用C/O流:std::printf,,,std::scanf等见std::strtof用于格式描述。

另见

浮点常数的C文档

*。

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com