C++标准模板(STL)- 类型支持 (数值极限,round_style,is_iec559,is_bounded)

数值极限


定义于头文件 <limits>

定义于头文件 <limits>

template< class T > class numeric_limits;

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

鉴别类型所用的舍入模式

std::numeric_limits<T>::round_style

static const std::float_round_style round_style;

(C++11 前)

static constexpr std::float_round_style round_style;

(C++11 起)

标准特化

Tstd::numeric_limits<T>::round_style 的值
/* non-specialized */std::round_toward_zero
boolstd::round_toward_zero
charstd::round_toward_zero
signed charstd::round_toward_zero
unsigned charstd::round_toward_zero
wchar_tstd::round_toward_zero
char8_tstd::round_toward_zero
char16_tstd::round_toward_zero
char32_tstd::round_toward_zero
shortstd::round_toward_zero
unsigned shortstd::round_toward_zero
intstd::round_toward_zero
unsigned intstd::round_toward_zero
longstd::round_toward_zero
unsigned longstd::round_toward_zero
long longstd::round_toward_zero
unsigned long longstd::round_toward_zero
float通常为 std::round_to_nearest
double通常为 std::round_to_nearest
long double通常为 std::round_to_nearest

注意

这些值是常量,且不反映 std::fesetround 所做的舍入模式更改。被更改的值可从 FLT_ROUNDS 或 std::fegetround 获得。

示例

十进制值 0.1 不能表示成二进制浮点类型。在存储于 IEEE-745 double 时,它落入 0x1.9999999999999*2-4与 0x1.999999999999a*2-4之间。舍入到最近可表示结果导致 0x1.999999999999a*2-4。同样地,十进制值 0.3 在 0x1.3333333333333*2-2与 0x1.3333333333334*2-2
之间,舍入到最近值后存储为 0x1.3333333333333*2-2。

 调用示例

#include <iostream>
#include <string>
#include <limits>

struct SName
{
};

//偏特化
struct SPartSpec
{
};

namespace std
{
template<>
struct numeric_limits<SPartSpec>
{
    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;
};
}

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::numeric_limits<bool>::round_style:                 "
              << std::numeric_limits<bool>::round_style << std::endl;
    std::cout << "std::numeric_limits<char>::round_style:                 "
              << std::numeric_limits<char>::round_style << std::endl;
    std::cout << "std::numeric_limits<signed char>::round_style:          "
              << std::numeric_limits<signed char>::round_style << std::endl;
    std::cout << "std::numeric_limits<unsigned char>::round_style:        "
              << std::numeric_limits<unsigned char>::round_style << std::endl;
    std::cout << "std::numeric_limits<wchar_t>::round_style:              "
              << std::numeric_limits<wchar_t>::round_style << std::endl;
    std::cout << "std::numeric_limits<char16_t>::round_style:             "
              << std::numeric_limits<char16_t>::round_style << std::endl;
    std::cout << "std::numeric_limits<char32_t>::round_style:             "
              << std::numeric_limits<char32_t>::round_style << std::endl;
    std::cout << "std::numeric_limits<short>::round_style:                "
              << std::numeric_limits<short>::round_style << std::endl;
    std::cout << "std::numeric_limits<unsigned short>::round_style:       "
              << std::numeric_limits<unsigned short>::round_style << std::endl;
    std::cout << "std::numeric_limits<int>::round_style:                  "
              << std::numeric_limits<int>::round_style << std::endl;
    std::cout << "std::numeric_limits<unsigned int>::round_style:         "
              << std::numeric_limits<unsigned int>::round_style << std::endl;
    std::cout << "std::numeric_limits<long>::round_style:                 "
              << std::numeric_limits<long>::round_style << std::endl;
    std::cout << "std::numeric_limits<unsigned long>::round_style:        "
              << std::numeric_limits<unsigned long>::round_style << std::endl;
    std::cout << "std::numeric_limits<long long>::round_style:            "
              << std::numeric_limits<long long>::round_style << std::endl;
    std::cout << "std::numeric_limits<unsigned long long>::round_style:   "
              << std::numeric_limits<unsigned long long>::round_style << std::endl;
    std::cout << "std::numeric_limits<float>::round_style:                "
              << std::numeric_limits<float>::round_style << std::endl;
    std::cout << "std::numeric_limits<double>::round_style:               "
              << std::numeric_limits<double>::round_style << std::endl;
    std::cout << "std::numeric_limits<long double>::round_style:          "
              << std::numeric_limits<long double>::round_style << std::endl;
    std::cout << "std::numeric_limits<std::string>::round_style:          "
              << std::numeric_limits<std::string>::round_style << std::endl;
    std::cout << "std::numeric_limits<SName>::round_style:                "
              << std::numeric_limits<SName>::round_style << std::endl;
    std::cout << "std::numeric_limits<SPartSpec>::round_style:            "
              << std::numeric_limits<SPartSpec>::round_style << std::endl;
    return 0;
}

输出

鉴别 IEC 559/IEEE 754 浮点类型

std::numeric_limits<T>::is_iec559

static const bool is_iec559;

(C++11 前)

static constexpr bool is_iec559;

(C++11 起)

 std::numeric_limits<T>::is_iec559 的值对于所有符合 IEC 559 ( IEEE 754 )标准要求的浮点类型 T 为 true 。若 std::numeric_limits<T>::is_iec559 为 true ,则 std::numeric_limits<T>::has_infinity 、 std::numeric_limits<T>::has_quiet_NaN 与 std::numeric_limits<T>::has_signaling_NaN 亦为 true 。

标准特化

Tstd::numeric_limits<T>::is_iec559 的值
/* non-specialized */false
boolfalse
charfalse
signed charfalse
unsigned charfalse
wchar_tfalse
char8_tfalse
char16_tfalse
char32_tfalse
shortfalse
unsigned shortfalse
intfalse
unsigned intfalse
longfalse
unsigned longfalse
long longfalse
unsigned long longfalse
float通常为 true
double通常为 true
long double通常为 true

调用示例

#include <iostream>
#include <string>
#include <limits>

struct SName
{
};

//偏特化
struct SPartSpec
{
};

namespace std
{
template<>
struct numeric_limits<SPartSpec>
{
    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;
};
}

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::numeric_limits<bool>::is_iec559:                 "
              << std::numeric_limits<bool>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<char>::is_iec559:                 "
              << std::numeric_limits<char>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<signed char>::is_iec559:          "
              << std::numeric_limits<signed char>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<unsigned char>::is_iec559:        "
              << std::numeric_limits<unsigned char>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<wchar_t>::is_iec559:              "
              << std::numeric_limits<wchar_t>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<char16_t>::is_iec559:             "
              << std::numeric_limits<char16_t>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<char32_t>::is_iec559:             "
              << std::numeric_limits<char32_t>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<short>::is_iec559:                "
              << std::numeric_limits<short>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<unsigned short>::is_iec559:       "
              << std::numeric_limits<unsigned short>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<int>::is_iec559:                  "
              << std::numeric_limits<int>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<unsigned int>::is_iec559:         "
              << std::numeric_limits<unsigned int>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<long>::is_iec559:                 "
              << std::numeric_limits<long>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<unsigned long>::is_iec559:        "
              << std::numeric_limits<unsigned long>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<long long>::is_iec559:            "
              << std::numeric_limits<long long>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<unsigned long long>::is_iec559:   "
              << std::numeric_limits<unsigned long long>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<float>::is_iec559:                "
              << std::numeric_limits<float>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<double>::is_iec559:               "
              << std::numeric_limits<double>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<long double>::is_iec559:          "
              << std::numeric_limits<long double>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<std::string>::is_iec559:          "
              << std::numeric_limits<std::string>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<SName>::is_iec559:                "
              << std::numeric_limits<SName>::is_iec559 << std::endl;
    std::cout << "std::numeric_limits<SPartSpec>::is_iec559:            "
              << std::numeric_limits<SPartSpec>::is_iec559 << std::endl;
    return 0;
}

 输出

鉴别表示有限值集合的类型

std::numeric_limits<T>::is_bounded

static const bool is_bounded;

(C++11 前)

static constexpr bool is_bounded;

(C++11 起)

 std::numeric_limits<T>::is_bounded 对所有表示有限的值集合的算术类型 T 为 true 。所有基础类型均有界,不过此常量会在 std::numeric_limits 对库提供的任意精度算术类型的特化中为 false 。

标准特化

Tstd::numeric_limits<T>::is_bounded 的值
/* non-specialized */false
booltrue
chartrue
signed chartrue
unsigned chartrue
wchar_ttrue
char8_ttrue
char16_ttrue
char32_ttrue
shorttrue
unsigned shorttrue
inttrue
unsigned inttrue
longtrue
unsigned longtrue
long longtrue
unsigned long longtrue
floattrue
doubletrue
long doubletrue

调用示例

#include <iostream>
#include <string>
#include <limits>

struct SName
{
};

//偏特化
struct SPartSpec
{
};

namespace std
{
template<>
struct numeric_limits<SPartSpec>
{
    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;
};
}

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::numeric_limits<bool>::is_bounded:                 "
              << std::numeric_limits<bool>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<char>::is_bounded:                 "
              << std::numeric_limits<char>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<signed char>::is_bounded:          "
              << std::numeric_limits<signed char>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<unsigned char>::is_bounded:        "
              << std::numeric_limits<unsigned char>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<wchar_t>::is_bounded:              "
              << std::numeric_limits<wchar_t>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<char16_t>::is_bounded:             "
              << std::numeric_limits<char16_t>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<char32_t>::is_bounded:             "
              << std::numeric_limits<char32_t>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<short>::is_bounded:                "
              << std::numeric_limits<short>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<unsigned short>::is_bounded:       "
              << std::numeric_limits<unsigned short>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<int>::is_bounded:                  "
              << std::numeric_limits<int>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<unsigned int>::is_bounded:         "
              << std::numeric_limits<unsigned int>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<long>::is_bounded:                 "
              << std::numeric_limits<long>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<unsigned long>::is_bounded:        "
              << std::numeric_limits<unsigned long>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<long long>::is_bounded:            "
              << std::numeric_limits<long long>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<unsigned long long>::is_bounded:   "
              << std::numeric_limits<unsigned long long>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<float>::is_bounded:                "
              << std::numeric_limits<float>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<double>::is_bounded:               "
              << std::numeric_limits<double>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<long double>::is_bounded:          "
              << std::numeric_limits<long double>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<std::string>::is_bounded:          "
              << std::numeric_limits<std::string>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<SName>::is_bounded:                "
              << std::numeric_limits<SName>::is_bounded << std::endl;
    std::cout << "std::numeric_limits<SPartSpec>::is_bounded:            "
              << std::numeric_limits<SPartSpec>::is_bounded << std::endl;
    return 0;
}

 输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值