Fixed width floating-point types (since C++23)
From cppreference.net
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Type support
| Basic types | |||||||||||||||||||||
| Fixed width integer types (C++11) | |||||||||||||||||||||
| Fixed width floating-point types (C++23) | |||||||||||||||||||||
|
|||||||||||||||||||||
| Numeric limits | |||||||||||||||||||||
| C numeric limits interface | |||||||||||||||||||||
| Runtime type information | |||||||||||||||||||||
|
|||||||||||||||||||||
如果实现支持以下任意一种 ISO 60559 类型作为扩展浮点类型,则:
- 对应的宏被定义为 1 以表示支持,
- 对应的 浮点数字面量 后缀可用,且
- 提供了对应的类型别名:
|
类型
定义于头文件 <stdfloat> |
字面量后缀 | 预定义宏 | C语言类型 | 类型属性 | |||
|---|---|---|---|---|---|---|---|
| 存储位数 | 精度位数 | 指数位数 | 最大指数 | ||||
| float16_t | f16 或 F16 | __STDCPP_FLOAT16_T__ |
_Float16
|
16 | 11 | 5 | 15 |
| float32_t | f32 或 F32 | __STDCPP_FLOAT32_T__ |
_Float32
|
32 | 24 | 8 | 127 |
| float64_t | f64 或 F64 | __STDCPP_FLOAT64_T__ |
_Float64
|
64 | 53 | 11 | 1023 |
| float128_t | f128 或 F128 | __STDCPP_FLOAT128_T__ |
_Float128
|
128 | 113 | 15 | 16383 |
| bfloat16_t | bf16 或 BF16 | __STDCPP_BFLOAT16_T__ | (N/A) | 16 | 8 | 8 | 127 |
目录 |
注释
类型
std::bfloat16_t
被称为
脑浮点数
。
与 固定宽度整数类型 (可能是 标准整数类型 的别名)不同,固定宽度浮点类型必须是扩展浮点类型的别名(而非 float / double / long double ),因此不能直接替代标准浮点类型。
示例
运行此代码
#include <stdfloat> #if __STDCPP_FLOAT64_T__ != 1 #error "64-bit float type required" #endif int main() { std::float64_t f = 0.1f64; }
参考文献
- C++23 标准 (ISO/IEC 14882:2024):
-
- 6.8.3 可选扩展浮点类型 [basic.extended.fp]