C++标准模板(STL)- 类型支持 (数值极限,epsilon,round_error,infinity)

数值极限

提供查询所有基础数值类型的性质的接口

定义于头文件

template< class T > class numeric_limits;

numeric_limits 类模板提供查询各种算术类型属性的标准化方式(例如 int 类型的最大可能值是 std::numeric_limits::max() )。
 

返回 1.0 与给定类型的下个可表示值的差

std::numeric_limits::epsilon

static T epsilon() throw();

(C++11 前)

static constexpr T epsilon() noexcept;

(C++11 起)

 返回机器 epsilon ,即 1.0 与浮点类型 T 的下个可表示值的差。它仅若 std::numeric_limits::is_integer == false 才有意义。

返回值

T std::numeric_limits::epsilon()
/* non-specialized */ T()
bool false
char ​0​
signed char ​0​
unsigned char ​0​
wchar_t ​0​
char8_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_EPSILON
double DBL_EPSILON
long double LDBL_EPSILON

 调用示例

#include 
#include 
#include 
#include 
#include 

struct SName
{
};

//偏特化
struct SPartSpec
{
};

namespace std
{
template<>
struct numeric_limits
{
    static _GLIBCXX_USE_CONSTEXPR bool is_specialized   = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_signed        = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_integer       = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_exact         = true;
    static _GLIBCXX_USE_CONSTEXPR bool has_infinity     = true;
    static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN    = true;
    static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;
    static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm     = denorm_present;
    static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss  = true;
    static _GLIBCXX_USE_CONSTEXPR float_round_style round_style     = round_toward_neg_infinity;
    static _GLIBCXX_USE_CONSTEXPR bool is_iec559        = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_bounded       = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_modulo        = true;
    static _GLIBCXX_USE_CONSTEXPR int  digits           = CHAR_BIT;
    static _GLIBCXX_USE_CONSTEXPR int  digits10         = CHAR_BIT;
    static _GLIBCXX_USE_CONSTEXPR int  max_digits10     = DECIMAL_DIG;
    static _GLIBCXX_USE_CONSTEXPR int  radix            = FLT_RADIX;
    static _GLIBCXX_USE_CONSTEXPR int  min_exponent     = FLT_MIN_EXP;
    static _GLIBCXX_USE_CONSTEXPR int  min_exponent10   = FLT_MIN_10_EXP;
    static _GLIBCXX_USE_CONSTEXPR int  max_exponent     = FLT_MAX_EXP;
    static _GLIBCXX_USE_CONSTEXPR int  max_exponent10   = FLT_MAX_EXP;
    static _GLIBCXX_USE_CONSTEXPR bool traps            = true;
    static _GLIBCXX_USE_CONSTEXPR bool tinyness_before  = true;

    static _GLIBCXX_CONSTEXPR int
    min() _GLIBCXX_USE_NOEXCEPT { return  CHAR_MIN ; }
    static _GLIBCXX_CONSTEXPR int
    lowest() _GLIBCXX_USE_NOEXCEPT { return  CHAR_MIN ; }
    static _GLIBCXX_CONSTEXPR int
    max() _GLIBCXX_USE_NOEXCEPT { return  CHAR_MAX ; }
    static _GLIBCXX_CONSTEXPR int
    epsilon() _GLIBCXX_USE_NOEXCEPT { return  LDBL_EPSILON ; }
};
}

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::numeric_limits::epsilon():                 "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():                 "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():          "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():        "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():              "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():             "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():             "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():                "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():       "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():                  "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():         "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():                 "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():        "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():            "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():   "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():                "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():               "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():          "
              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():          "
              << std::numeric_limits::epsilon() << std::endl;
    //必须偏特化
//    std::cout << "std::numeric_limits::epsilon():                "
//              << std::numeric_limits::epsilon() << std::endl;
    std::cout << "std::numeric_limits::epsilon():            "
              << std::numeric_limits::epsilon() << std::endl;
    return 0;
}

输出

C++标准模板(STL)- 类型支持 (数值极限,epsilon,round_error,infinity)_第1张图片

返回给定浮点类型的最大舍入误差

std::numeric_limits::round_error

static T round_error() throw();

(C++11 前)

static constexpr T round_error() noexcept;

(C++11 起)

 返回以 ULP (最后位置单位)表示的最大可能舍入错误,其为 ISO 10967 定义,可以从 0.5 (舍入到最近位)变化到 1.0 (舍入到零或无穷大)。它仅若 std::numeric_limits::is_integer == false 才有意义。

返回值

T std::numeric_limits::round_error()
/* non-specialized */ T()
bool false
char ​0​
signed char ​0​
unsigned char ​0​
wchar_t ​0​
char8_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 0.5F
double 0.5
long double 0.5L

调用示例

#include 
#include 
#include 
#include 
#include 

struct SName
{
};

//偏特化
struct SPartSpec
{
};

namespace std
{
template<>
struct numeric_limits
{
    static _GLIBCXX_USE_CONSTEXPR bool is_specialized   = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_signed        = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_integer       = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_exact         = true;
    static _GLIBCXX_USE_CONSTEXPR bool has_infinity     = true;
    static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN    = true;
    static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;
    static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm     = denorm_present;
    static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss  = true;
    static _GLIBCXX_USE_CONSTEXPR float_round_style round_style     = round_toward_neg_infinity;
    static _GLIBCXX_USE_CONSTEXPR bool is_iec559        = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_bounded       = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_modulo        = true;
    static _GLIBCXX_USE_CONSTEXPR int  digits           = CHAR_BIT;
    static _GLIBCXX_USE_CONSTEXPR int  digits10         = CHAR_BIT;
    static _GLIBCXX_USE_CONSTEXPR int  max_digits10     = DECIMAL_DIG;
    static _GLIBCXX_USE_CONSTEXPR int  radix            = FLT_RADIX;
    static _GLIBCXX_USE_CONSTEXPR int  min_exponent     = FLT_MIN_EXP;
    static _GLIBCXX_USE_CONSTEXPR int  min_exponent10   = FLT_MIN_10_EXP;
    static _GLIBCXX_USE_CONSTEXPR int  max_exponent     = FLT_MAX_EXP;
    static _GLIBCXX_USE_CONSTEXPR int  max_exponent10   = FLT_MAX_EXP;
    static _GLIBCXX_USE_CONSTEXPR bool traps            = true;
    static _GLIBCXX_USE_CONSTEXPR bool tinyness_before  = true;

    static _GLIBCXX_CONSTEXPR int
    min() _GLIBCXX_USE_NOEXCEPT { return  CHAR_MIN ; }
    static _GLIBCXX_CONSTEXPR int
    lowest() _GLIBCXX_USE_NOEXCEPT { return  CHAR_MIN ; }
    static _GLIBCXX_CONSTEXPR int
    max() _GLIBCXX_USE_NOEXCEPT { return  CHAR_MAX ; }
    static _GLIBCXX_CONSTEXPR int
    epsilon() _GLIBCXX_USE_NOEXCEPT { return  LDBL_EPSILON ; }
    static _GLIBCXX_CONSTEXPR double
    round_error() _GLIBCXX_USE_NOEXCEPT { return  0.5F ; }
};
}

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::numeric_limits::round_error():                 "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():                 "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():          "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():        "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():              "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():             "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():             "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():                "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():       "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():                  "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():         "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():                 "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():        "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():            "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():   "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():                "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():               "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():          "
              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():          "
              << std::numeric_limits::round_error() << std::endl;
    //必须偏特化
//    std::cout << "std::numeric_limits::round_error():                "
//              << std::numeric_limits::round_error() << std::endl;
    std::cout << "std::numeric_limits::round_error():            "
              << std::numeric_limits::round_error() << std::endl;
    return 0;
}

输出

C++标准模板(STL)- 类型支持 (数值极限,epsilon,round_error,infinity)_第2张图片

返回给定类型的正无穷大值

std::numeric_limits::infinity

static T infinity() throw();

(C++11 前)

static constexpr T infinity() noexcept;

(C++11 起)

 返回浮点类型 T 所表示的特殊值“正无穷大”。仅若 std::numeric_limits::has_infinity == true 才有意义。在最常见的浮点数二进制表示 IEEE 754 中,正无穷大是所有指数位为 1 而所有尾数位为 0 的值。

返回值

T std::numeric_limits::infinity()
/* non-specialized */ T()
bool false
char ​0​
signed char ​0​
unsigned char ​0​
wchar_t ​0​
char8_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 HUGE_VALF
double HUGE_VAL
long double HUGE_VALL

调用示例

#include 
#include 
#include 
#include 
#include 
#include 

struct SName
{
};

//偏特化
struct SPartSpec
{
};

namespace std
{
template<>
struct numeric_limits
{
    static _GLIBCXX_USE_CONSTEXPR bool is_specialized   = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_signed        = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_integer       = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_exact         = true;
    static _GLIBCXX_USE_CONSTEXPR bool has_infinity     = true;
    static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN    = true;
    static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;
    static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm     = denorm_present;
    static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss  = true;
    static _GLIBCXX_USE_CONSTEXPR float_round_style round_style     = round_toward_neg_infinity;
    static _GLIBCXX_USE_CONSTEXPR bool is_iec559        = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_bounded       = true;
    static _GLIBCXX_USE_CONSTEXPR bool is_modulo        = true;
    static _GLIBCXX_USE_CONSTEXPR int  digits           = CHAR_BIT;
    static _GLIBCXX_USE_CONSTEXPR int  digits10         = CHAR_BIT;
    static _GLIBCXX_USE_CONSTEXPR int  max_digits10     = DECIMAL_DIG;
    static _GLIBCXX_USE_CONSTEXPR int  radix            = FLT_RADIX;
    static _GLIBCXX_USE_CONSTEXPR int  min_exponent     = FLT_MIN_EXP;
    static _GLIBCXX_USE_CONSTEXPR int  min_exponent10   = FLT_MIN_10_EXP;
    static _GLIBCXX_USE_CONSTEXPR int  max_exponent     = FLT_MAX_EXP;
    static _GLIBCXX_USE_CONSTEXPR int  max_exponent10   = FLT_MAX_EXP;
    static _GLIBCXX_USE_CONSTEXPR bool traps            = true;
    static _GLIBCXX_USE_CONSTEXPR bool tinyness_before  = true;

    static _GLIBCXX_CONSTEXPR int
    min() _GLIBCXX_USE_NOEXCEPT { return  CHAR_MIN ; }
    static _GLIBCXX_CONSTEXPR int
    lowest() _GLIBCXX_USE_NOEXCEPT { return  CHAR_MIN ; }
    static _GLIBCXX_CONSTEXPR int
    max() _GLIBCXX_USE_NOEXCEPT { return  CHAR_MAX ; }
    static _GLIBCXX_CONSTEXPR int
    epsilon() _GLIBCXX_USE_NOEXCEPT { return  LDBL_EPSILON ; }
    static _GLIBCXX_CONSTEXPR double
    round_error() _GLIBCXX_USE_NOEXCEPT { return  0.5F ; }
    static _GLIBCXX_CONSTEXPR double
    infinity() _GLIBCXX_USE_NOEXCEPT { return  HUGE_VAL ; }
};
}

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::numeric_limits::infinity():                 "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():                 "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():          "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():        "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():              "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():             "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():             "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():                "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():       "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():                  "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():         "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():                 "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():        "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():            "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():   "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():                "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():               "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():          "
              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():          "
              << std::numeric_limits::infinity() << std::endl;
    //必须偏特化
//    std::cout << "std::numeric_limits::infinity():                "
//              << std::numeric_limits::infinity() << std::endl;
    std::cout << "std::numeric_limits::infinity():            "
              << std::numeric_limits::infinity() << std::endl;
    return 0;
}

输出

C++标准模板(STL)- 类型支持 (数值极限,epsilon,round_error,infinity)_第3张图片

你可能感兴趣的:(C++标准库模板(STL)-,类型支持,c++,数值极限,标准库模板,epsilon,round_error,infinity)