static constexpr int max_digits10
|
(C++11 起) | |
std::numeric_limits<T>::max_digits10 的值是唯一地表示所有类型 T
值的底 10 位数,例如序列化/反序列化到文本所需。此常量对所有浮点类型有意义。
目录 |
T
|
std::numeric_limits<T>::max_digits10 的值 |
/* non-specialized */ | 0 |
bool | 0 |
char | 0 |
signed char | 0 |
unsigned char | 0 |
wchar_t | 0 |
char16_t | 0 |
char32_t | 0 |
short | 0 |
unsigned short | 0 |
int | 0 |
unsigned int | 0 |
long | 0 |
unsigned long | 0 |
long long | 0 |
unsigned long long | 0 |
float | FLT_DECIMAL_DIG or std::ceil(std::numeric_limits<float>::digits * std::log10(2) + 1) |
double | DBL_DECIMAL_DIG orstd::ceil(std::numeric_limits<double>::digits * std::log10(2) + 1) |
long double | DECIMAL_DIG or LDBL_DECIMAL_DIG orstd::ceil(std::numeric_limits<long double>::digits * std::log10(2) + 1) |
不同于多数数学运算,只要用至少 max_digits10
(对于 float 为 9 ,对于 double 为 17 )位,则从浮点值转换到文本并转换回来是准确的:保证产生同一浮点值,即使不确定的文本表示不准确。以小数点记法,可能需要超过一百个十进制小数位表示 float 的精确值。
本节未完成 原因:压缩到仅一个数,并演示平稳舍入,以令此有意义 |
#include <limits> #include <sstream> #include <ios> // std::scientific #include <iomanip> // std::setprecision() #include <cmath> // std::nextafter() #include <iostream> template< typename FloatingType > std::string to_string_complete( FloatingType value ) { constexpr auto max_digits10 = std::numeric_limits<FloatingType>::max_digits10; constexpr auto one_digit10_in_integer_part = 1; constexpr auto max_fractional_digits10 = max_digits10 - one_digit10_in_integer_part; std::ostringstream oss; oss << std::scientific << std::setprecision( max_fractional_digits10 ) << value; return oss.str(); } int main() { std::cout << "f -42: " << to_string_complete( -42.0f ) << "\n" << "f min: " << to_string_complete( std::numeric_limits<float>::min() ) << "\n" << "f nxmin: " << to_string_complete( std::nextafter( std::numeric_limits<float>::min(), 1.0f ) ) << "\n" << "f small: " << to_string_complete( 0.000000000000000123456789f ) << "\n" << "f 0: " << to_string_complete( 0.0f ) << "\n" << "f nx 0: " << to_string_complete( std::nextafter( 0.0f, 1.0f ) ) << "\n" << "f 42: " << to_string_complete( 42.0f ) << "\n" << "f nx 42: " << to_string_complete( std::nextafter( 42.0f, 43.0f ) ) << "\n" << "f pi: " << to_string_complete( 3.14159265358979323f ) << "\n" << "f big: " << to_string_complete( 3.14159265358979323e+30f ) << "\n" << "f max: " << to_string_complete( std::numeric_limits<float>::max() ) << "\n" << "\n"; std::cout << "d -42: " << to_string_complete( -42.0 ) << "\n" << "d min: " << to_string_complete( std::numeric_limits<double>::min() ) << "\n" << "d nxmin: " << to_string_complete( std::nextafter( std::numeric_limits<double>::min(), 1.0 ) ) << "\n" << "d small: " << to_string_complete( 0.000000000000000123456789 ) << "\n" << "d 0: " << to_string_complete( 0.0 ) << "\n" << "d nx 0: " << to_string_complete( std::nextafter( 0.0, 1.0 ) ) << "\n" << "d 42: " << to_string_complete( 42.0 ) << "\n" << "d nx 42: " << to_string_complete( std::nextafter( 42.0, 43.0 ) ) << "\n" << "d pi: " << to_string_complete( 3.14159265358979323 ) << "\n" << "d big: " << to_string_complete( 3.14159265358979323e+300 ) << "\n" << "d max: " << to_string_complete( std::numeric_limits<double>::max() ) << "\n" << "\n"; std::cout << "D -42: " << to_string_complete( -42.0L ) << "\n" << "D min: " << to_string_complete( std::numeric_limits<long double>::min() ) << "\n" << "D nxmin: " << to_string_complete( std::nextafter( std::numeric_limits<long double>::min(), 1.0L ) ) << "\n" << "D small: " << to_string_complete( 0.000000000000000123456789L ) << "\n" << "D 0: " << to_string_complete( 0.0L ) << "\n" << "D nx 0: " << to_string_complete( std::nextafter( 0.0L, 1.0L ) ) << "\n" << "D 42: " << to_string_complete( 42.0L ) << "\n" << "D nx 42: " << to_string_complete( std::nextafter( 42.0L, 43.0L ) ) << "\n" << "D pi: " << to_string_complete( 3.141592653589793238462L ) << "\n" << "D big: " << to_string_complete( 3.141592653589793238462e+4000L ) << "\n" << "D max: " << to_string_complete( std::numeric_limits<long double>::max() ) << "\n" << "\n"; }
输出:
f -42: -4.20000000e+01 f min: 1.17549435e-38 f nxmin: 1.17549449e-38 f small: 1.23456786e-16 f 0: 0.00000000e+00 f nx 0: 1.40129846e-45 f 42: 4.20000000e+01 f nx 42: 4.20000038e+01 f pi: 3.14159274e+00 f big: 3.14159261e+30 f max: 3.40282347e+38 d -42: -4.2000000000000000e+01 d min: 2.2250738585072014e-308 d nxmin: 2.2250738585072019e-308 d small: 1.2345678900000000e-16 d 0: 0.0000000000000000e+00 d nx 0: 4.9406564584124654e-324 d 42: 4.2000000000000000e+01 d nx 42: 4.2000000000000007e+01 d pi: 3.1415926535897931e+00 d big: 3.1415926535897931e+300 d max: 1.7976931348623157e+308 D -42: -4.20000000000000000000e+01 D min: 3.36210314311209350626e-4932 D nxmin: 3.36210314311209350663e-4932 D small: 1.23456789000000000000e-16 D 0: 0.00000000000000000000e+00 D nx 0: 3.64519953188247460253e-4951 D 42: 4.20000000000000000000e+01 D nx 42: 4.20000000000000000035e+01 D pi: 3.14159265358979323851e+00 D big: 3.14159265358979323854e+4000 D max: 1.18973149535723176502e+4932
[静态]
|
给定类型的表示所用的基或整数底 (公开静态成员常量) |
[静态]
|
能无更改地表示的 radix 位数 (公开静态成员常量) |
[静态]
|
能无更改地表示的十进制位数 (公开静态成员常量) |
[静态]
|
底的该数次幂是合法正规浮点值的最小负数加一 (公开静态成员常量) |
[静态]
|
底的该数次幂是合法有限浮点值的最大整数加一 (公开静态成员常量) |