在线文档教程
C++
应用 | Utilities

std::numeric_limits

STD::数字[医]限值

Defined in header
template< class T > class numeric_limits;

大numeric_limits类模板提供了一种标准的方法来查询算术类型%28的各种属性。类型的最大可能值。int是std::numeric_limits<int>::max()29%。

这些信息是通过numeric_limits模板。标准库为所有算术类型提供了可用的专门化:

Defined in header
template<> class numeric_limits<bool>; template<> class numeric_limits<char>; template<> class numeric_limits<signed char>; template<> class numeric_limits<unsigned char>; template<> class numeric_limits<wchar_t>; template<> class numeric_limits<char16_t>; // C++11 feature template<> class numeric_limits<char32_t>; // C++11 feature template<> class numeric_limits<short>; template<> class numeric_limits<unsigned short>; template<> class numeric_limits<int>; template<> class numeric_limits<unsigned int>; template<> class numeric_limits<long>; template<> class numeric_limits<unsigned long>; template<> class numeric_limits<long long>; template<> class numeric_limits<unsigned long long>; template<> class numeric_limits<float>; template<> class numeric_limits<double>; template<> class numeric_limits<long double>;

此外,每种算术类型的每个cv限定版本都存在一个专门化,与不合格的专门化相同。std::numeric_limits<const int>,,,std::numeric_limits<volatile int>,和std::numeric_limits<const volatile int>提供,并相当于std::numeric_limits<int>...

标准库类型,它是算术类型%28的别名,如std::size_tstd::streamsize5月29日%亦会与std::numeric_limits类型特征。

非算术标准类型,如std::complex<T>或std::nullptr_t,没有专门性。

实现可以提供以下方面的专门化:std::numeric_limits对于实现-特定类型:例如gcc提供std::numeric_limits<__int128>非标准图书馆可添加专门化为图书馆提供的类型,例如。OpenEXR提供std::numeric_limits<half>16位浮点类型。

模板参数

T-a type to retrieve numeric properties for

成员常数

is_specialized staticidentifies types for which std::numeric_limits is specialized (public static member constant)
is_signed staticidentifies signed types (public static member constant)
is_integer staticidentifies integer types (public static member constant)
is_exact staticidentifies exact types (public static member constant)
has_infinity staticidentifies floating-point types that can represent the special value "positive infinity" (public static member constant)
has_quiet_NaN staticidentifies floating-point types that can represent the special value "quiet not-a-number" (NaN) (public static member constant)
has_signaling_NaN staticidentifies floating-point types that can represent the special value "signaling not-a-number" (NaN) (public static member constant)
has_denorm staticidentifies the denormalization style used by the floating-point type (public static member constant)
has_denorm_loss staticidentifies the floating-point types that detect loss of precision as denormalization loss rather than inexact result (public static member constant)
round_style staticidentifies the rounding style used by the type (public static member constant)
is_iec559 staticidentifies the IEC 559/IEEE 754 floating-point types (public static member constant)
is_bounded staticidentifies types that represent a finite set of values (public static member constant)
is_modulo staticidentifies types that handle overflows with modulo arithmetic (public static member constant)
digits staticnumber of radix digits that can be represented without change (public static member constant)
digits10 staticnumber of decimal digits that can be represented without change (public static member constant)
max_digits10 staticnumber of decimal digits necessary to differentiate all values of this type (public static member constant)
radix staticthe radix or integer base used by the representation of the given type (public static member constant)
min_exponent staticone more than the smallest negative power of the radix that is a valid normalized floating-point value (public static member constant)
min_exponent10 staticthe smallest negative power of ten that is a valid normalized floating-point value (public static member constant)
max_exponent staticone more than the largest integer power of the radix that is a valid finite floating-point value (public static member constant)
max_exponent10 staticthe largest integer power of 10 that is a valid finite floating-point value (public static member constant)
traps staticidentifies types which can cause arithmetic operations to trap (public static member constant)
tinyness_before staticidentifies floating-point types that detect tinyness before rounding (public static member constant)

成员函数

min staticreturns the smallest finite value of the given type (public static member function)
lowest staticreturns the lowest finite value of the given type (public static member function)
max staticreturns the largest finite value of the given type (public static member function)
epsilon staticreturns the difference between 1.0 and the next representable value of the given floating-point type (public static member function)
round_error staticreturns the maximum rounding error of the given floating-point type (public static member function)
infinity staticreturns the positive infinity value of the given floating-point type (public static member function)
quiet_NaN staticreturns a quiet NaN value of the given floating-point type (public static member function)
signaling_NaN staticreturns a signaling NaN value of the given floating-point type (public static member function)
denorm_min staticreturns the smallest positive subnormal value of the given floating-point type (public static member function)

帮助者类

float_round_styleindicates floating-point rounding modes (enum)
float_denorm_styleindicates floating-point denormalization modes (enum)

与C库宏常数的关系

SpecializationMembers
min()lowest()(C++11)
numeric_limits< bool >
numeric_limits< char >CHAR_MIN
numeric_limits< signed char >SCHAR_MIN
numeric_limits< unsigned char >​0​
numeric_limits< wchar_t >WCHAR_MIN
numeric_limits< char16_t >​0​
numeric_limits< char32_t >​0​
numeric_limits< short >SHRT_MIN
numeric_limits< signed short >
numeric_limits< unsigned short >​0​
numeric_limits< int >INT_MIN
numeric_limits< signed int >
numeric_limits< unsigned int >​0​
numeric_limits< long >LONG_MIN
numeric_limits< signed long >
numeric_limits< unsigned long >​0​
numeric_limits< long long >LLONG_MIN
numeric_limits< signed long long >
numeric_limits< unsigned long long >​0​
numeric_limits< float >FLT_MIN
numeric_limits< double >DBL_MIN
numeric_limits< long double >LDBL_MIN

二次

#include <limits> #include <iostream> int main() { std::cout << "type\tlowest\thighest\n"; std::cout << "int\t" << std::numeric_limits<int>::lowest() << '\t' << std::numeric_limits<int>::max() << '\n'; std::cout << "float\t" << std::numeric_limits<float>::lowest() << '\t' << std::numeric_limits<float>::max() << '\n'; std::cout << "double\t" << std::numeric_limits<double>::lowest() << '\t' << std::numeric_limits<double>::max() << '\n'; }

二次

可能的产出:

二次

type lowest highest int -2147483648 2147483647 float -3.40282e+38 3.40282e+38 double -1.79769e+308 1.79769e+308

二次

另见

  • 算术类型

  • C++型系统概述

  • 类型支持%28基本类型,RTTI,类型性状%29

© cppreference.com

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

http://en.cppreference.com/w/cpp/type/NUMERIC[医]限值