• C++标准模板(STL)- 类型支持 (数值极限,min_exponent10,max_exponent,max_exponent10)


    数值极限

    std::numeric_limits

    定义于头文件

    定义于头文件

    template< class T > class numeric_limits;

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

    10 的该数次幂是合法正规浮点值的最小负数

    std::numeric_limits<T>::min_exponent10

    static const int min_exponent10;

    (C++11 前)

    static constexpr int min_exponent10;

    (C++11 起)

    std::numeric_limits::min_exponent10 的值是满足 10n是浮点类型 T 的合法正规值的最低负数 n

    标准特化

    Tstd::numeric_limits::min_exponent10 的值
    /* non-specialized */​0​
    bool​0​
    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​
    floatFLT_MIN_10_EXP
    doubleDBL_MIN_10_EXP
    long doubleLDBL_MIN_10_EXP

    调用示例

    1. #include <iostream>
    2. #include <string>
    3. #include <limits>
    4. #include <cstdint>
    5. #include <cfloat>
    6. struct SName
    7. {
    8. };
    9. //偏特化
    10. struct SPartSpec
    11. {
    12. };
    13. namespace std
    14. {
    15. template<>
    16. struct numeric_limits<SPartSpec>
    17. {
    18. static _GLIBCXX_USE_CONSTEXPR bool is_specialized = true;
    19. static _GLIBCXX_USE_CONSTEXPR bool is_signed = true;
    20. static _GLIBCXX_USE_CONSTEXPR bool is_integer = true;
    21. static _GLIBCXX_USE_CONSTEXPR bool is_exact = true;
    22. static _GLIBCXX_USE_CONSTEXPR bool has_infinity = true;
    23. static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN = true;
    24. static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;
    25. static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm = denorm_present;
    26. static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss = true;
    27. static _GLIBCXX_USE_CONSTEXPR float_round_style round_style = round_toward_neg_infinity;
    28. static _GLIBCXX_USE_CONSTEXPR bool is_iec559 = true;
    29. static _GLIBCXX_USE_CONSTEXPR bool is_bounded = true;
    30. static _GLIBCXX_USE_CONSTEXPR bool is_modulo = true;
    31. static _GLIBCXX_USE_CONSTEXPR int digits = CHAR_BIT;
    32. static _GLIBCXX_USE_CONSTEXPR int digits10 = CHAR_BIT;
    33. static _GLIBCXX_USE_CONSTEXPR int max_digits10 = DECIMAL_DIG;
    34. static _GLIBCXX_USE_CONSTEXPR int radix = FLT_RADIX;
    35. static _GLIBCXX_USE_CONSTEXPR int min_exponent = FLT_MIN_EXP;
    36. static _GLIBCXX_USE_CONSTEXPR int min_exponent10 = FLT_MIN_10_EXP;
    37. };
    38. }
    39. int main()
    40. {
    41. std::cout << std::boolalpha;
    42. std::cout << "std::numeric_limits::min_exponent10: "
    43. << std::numeric_limits<bool>::min_exponent10 << std::endl;
    44. std::cout << "std::numeric_limits::min_exponent10: "
    45. << std::numeric_limits<char>::min_exponent10 << std::endl;
    46. std::cout << "std::numeric_limits::min_exponent10: "
    47. << std::numeric_limits<signed char>::min_exponent10 << std::endl;
    48. std::cout << "std::numeric_limits::min_exponent10: "
    49. << std::numeric_limits<unsigned char>::min_exponent10 << std::endl;
    50. std::cout << "std::numeric_limits::min_exponent10: "
    51. << std::numeric_limits<wchar_t>::min_exponent10 << std::endl;
    52. std::cout << "std::numeric_limits::min_exponent10: "
    53. << std::numeric_limits<char16_t>::min_exponent10 << std::endl;
    54. std::cout << "std::numeric_limits::min_exponent10: "
    55. << std::numeric_limits<char32_t>::min_exponent10 << std::endl;
    56. std::cout << "std::numeric_limits::min_exponent10: "
    57. << std::numeric_limits<short>::min_exponent10 << std::endl;
    58. std::cout << "std::numeric_limits::min_exponent10: "
    59. << std::numeric_limits<unsigned short>::min_exponent10 << std::endl;
    60. std::cout << "std::numeric_limits::min_exponent10: "
    61. << std::numeric_limits<int>::min_exponent10 << std::endl;
    62. std::cout << "std::numeric_limits::min_exponent10: "
    63. << std::numeric_limits<unsigned int>::min_exponent10 << std::endl;
    64. std::cout << "std::numeric_limits::min_exponent10: "
    65. << std::numeric_limits<long>::min_exponent10 << std::endl;
    66. std::cout << "std::numeric_limits::min_exponent10: "
    67. << std::numeric_limits<unsigned long>::min_exponent10 << std::endl;
    68. std::cout << "std::numeric_limits::min_exponent10: "
    69. << std::numeric_limits<long long>::min_exponent10 << std::endl;
    70. std::cout << "std::numeric_limits::min_exponent10: "
    71. << std::numeric_limits<unsigned long long>::min_exponent10 << std::endl;
    72. std::cout << "std::numeric_limits::min_exponent10: "
    73. << std::numeric_limits<float>::min_exponent10 << std::endl;
    74. std::cout << "std::numeric_limits::min_exponent10: "
    75. << std::numeric_limits<double>::min_exponent10 << std::endl;
    76. std::cout << "std::numeric_limits::min_exponent10: "
    77. << std::numeric_limits<long double>::min_exponent10 << std::endl;
    78. std::cout << "std::numeric_limits::min_exponent10: "
    79. << std::numeric_limits<std::string>::min_exponent10 << std::endl;
    80. std::cout << "std::numeric_limits::min_exponent10: "
    81. << std::numeric_limits<SName>::min_exponent10 << std::endl;
    82. std::cout << "std::numeric_limits::min_exponent10: "
    83. << std::numeric_limits<SPartSpec>::min_exponent10 << std::endl;
    84. return 0;
    85. }

    输出

    底的该数次幂是合法有限浮点值的最大整数加一

    std::numeric_limits<T>::max_exponent

    static const int max_exponent;

    (C++11 前)

    static constexpr int max_exponent;

    (C++11 起)

    std::numeric_limits::max_exponent 的值是满足 rn-1是浮点类型 T 的可表示有限值最大正整数 n ,其中 r 是 std::numeric_limits::radix 。

    标准特化

    Tstd::numeric_limits::max_exponent 的值
    /* non-specialized */​0​
    bool​0​
    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​
    floatFLT_MAX_EXP
    doubleDBL_MAX_EXP
    long doubleLDBL_MAX_EXP

    调用示例

    1. #include <iostream>
    2. #include <string>
    3. #include <limits>
    4. #include <cstdint>
    5. #include <cfloat>
    6. struct SName
    7. {
    8. };
    9. //偏特化
    10. struct SPartSpec
    11. {
    12. };
    13. namespace std
    14. {
    15. template<>
    16. struct numeric_limits<SPartSpec>
    17. {
    18. static _GLIBCXX_USE_CONSTEXPR bool is_specialized = true;
    19. static _GLIBCXX_USE_CONSTEXPR bool is_signed = true;
    20. static _GLIBCXX_USE_CONSTEXPR bool is_integer = true;
    21. static _GLIBCXX_USE_CONSTEXPR bool is_exact = true;
    22. static _GLIBCXX_USE_CONSTEXPR bool has_infinity = true;
    23. static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN = true;
    24. static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;
    25. static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm = denorm_present;
    26. static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss = true;
    27. static _GLIBCXX_USE_CONSTEXPR float_round_style round_style = round_toward_neg_infinity;
    28. static _GLIBCXX_USE_CONSTEXPR bool is_iec559 = true;
    29. static _GLIBCXX_USE_CONSTEXPR bool is_bounded = true;
    30. static _GLIBCXX_USE_CONSTEXPR bool is_modulo = true;
    31. static _GLIBCXX_USE_CONSTEXPR int digits = CHAR_BIT;
    32. static _GLIBCXX_USE_CONSTEXPR int digits10 = CHAR_BIT;
    33. static _GLIBCXX_USE_CONSTEXPR int max_digits10 = DECIMAL_DIG;
    34. static _GLIBCXX_USE_CONSTEXPR int radix = FLT_RADIX;
    35. static _GLIBCXX_USE_CONSTEXPR int min_exponent = FLT_MIN_EXP;
    36. static _GLIBCXX_USE_CONSTEXPR int min_exponent10 = FLT_MIN_10_EXP;
    37. static _GLIBCXX_USE_CONSTEXPR int max_exponent = FLT_MAX_EXP;
    38. };
    39. }
    40. int main()
    41. {
    42. std::cout << std::boolalpha;
    43. std::cout << "std::numeric_limits::max_exponent: "
    44. << std::numeric_limits<bool>::max_exponent << std::endl;
    45. std::cout << "std::numeric_limits::max_exponent: "
    46. << std::numeric_limits<char>::max_exponent << std::endl;
    47. std::cout << "std::numeric_limits::max_exponent: "
    48. << std::numeric_limits<signed char>::max_exponent << std::endl;
    49. std::cout << "std::numeric_limits::max_exponent: "
    50. << std::numeric_limits<unsigned char>::max_exponent << std::endl;
    51. std::cout << "std::numeric_limits::max_exponent: "
    52. << std::numeric_limits<wchar_t>::max_exponent << std::endl;
    53. std::cout << "std::numeric_limits::max_exponent: "
    54. << std::numeric_limits<char16_t>::max_exponent << std::endl;
    55. std::cout << "std::numeric_limits::max_exponent: "
    56. << std::numeric_limits<char32_t>::max_exponent << std::endl;
    57. std::cout << "std::numeric_limits::max_exponent: "
    58. << std::numeric_limits<short>::max_exponent << std::endl;
    59. std::cout << "std::numeric_limits::max_exponent: "
    60. << std::numeric_limits<unsigned short>::max_exponent << std::endl;
    61. std::cout << "std::numeric_limits::max_exponent: "
    62. << std::numeric_limits<int>::max_exponent << std::endl;
    63. std::cout << "std::numeric_limits::max_exponent: "
    64. << std::numeric_limits<unsigned int>::max_exponent << std::endl;
    65. std::cout << "std::numeric_limits::max_exponent: "
    66. << std::numeric_limits<long>::max_exponent << std::endl;
    67. std::cout << "std::numeric_limits::max_exponent: "
    68. << std::numeric_limits<unsigned long>::max_exponent << std::endl;
    69. std::cout << "std::numeric_limits::max_exponent: "
    70. << std::numeric_limits<long long>::max_exponent << std::endl;
    71. std::cout << "std::numeric_limits::max_exponent: "
    72. << std::numeric_limits<unsigned long long>::max_exponent << std::endl;
    73. std::cout << "std::numeric_limits::max_exponent: "
    74. << std::numeric_limits<float>::max_exponent << std::endl;
    75. std::cout << "std::numeric_limits::max_exponent: "
    76. << std::numeric_limits<double>::max_exponent << std::endl;
    77. std::cout << "std::numeric_limits::max_exponent: "
    78. << std::numeric_limits<long double>::max_exponent << std::endl;
    79. std::cout << "std::numeric_limits::max_exponent: "
    80. << std::numeric_limits<std::string>::max_exponent << std::endl;
    81. std::cout << "std::numeric_limits::max_exponent: "
    82. << std::numeric_limits<SName>::max_exponent << std::endl;
    83. std::cout << "std::numeric_limits::max_exponent: "
    84. << std::numeric_limits<SPartSpec>::max_exponent << std::endl;
    85. return 0;
    86. }

     输出

    10 的该数次幂是合法有限浮点值的最大整数

    std::numeric_limits<T>::max_exponent10

    std::numeric_limits::max_exponent10 的值是满足 10n是浮点类型 T 的可表示有限值的最大正整数 n

    标准特化

    Tstd::numeric_limits::max_exponent10 的值
    /* non-specialized */​0​
    bool​0​
    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​
    floatFLT_MAX_10_EXP
    doubleDBL_MAX_10_EXP
    long doubleLDBL_MAX_10_EXP

    调用示例

    1. #include <iostream>
    2. #include <string>
    3. #include <limits>
    4. #include <cstdint>
    5. #include <cfloat>
    6. struct SName
    7. {
    8. };
    9. //偏特化
    10. struct SPartSpec
    11. {
    12. };
    13. namespace std
    14. {
    15. template<>
    16. struct numeric_limits<SPartSpec>
    17. {
    18. static _GLIBCXX_USE_CONSTEXPR bool is_specialized = true;
    19. static _GLIBCXX_USE_CONSTEXPR bool is_signed = true;
    20. static _GLIBCXX_USE_CONSTEXPR bool is_integer = true;
    21. static _GLIBCXX_USE_CONSTEXPR bool is_exact = true;
    22. static _GLIBCXX_USE_CONSTEXPR bool has_infinity = true;
    23. static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN = true;
    24. static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;
    25. static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm = denorm_present;
    26. static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss = true;
    27. static _GLIBCXX_USE_CONSTEXPR float_round_style round_style = round_toward_neg_infinity;
    28. static _GLIBCXX_USE_CONSTEXPR bool is_iec559 = true;
    29. static _GLIBCXX_USE_CONSTEXPR bool is_bounded = true;
    30. static _GLIBCXX_USE_CONSTEXPR bool is_modulo = true;
    31. static _GLIBCXX_USE_CONSTEXPR int digits = CHAR_BIT;
    32. static _GLIBCXX_USE_CONSTEXPR int digits10 = CHAR_BIT;
    33. static _GLIBCXX_USE_CONSTEXPR int max_digits10 = DECIMAL_DIG;
    34. static _GLIBCXX_USE_CONSTEXPR int radix = FLT_RADIX;
    35. static _GLIBCXX_USE_CONSTEXPR int min_exponent = FLT_MIN_EXP;
    36. static _GLIBCXX_USE_CONSTEXPR int min_exponent10 = FLT_MIN_10_EXP;
    37. static _GLIBCXX_USE_CONSTEXPR int max_exponent = FLT_MAX_EXP;
    38. static _GLIBCXX_USE_CONSTEXPR int max_exponent10 = FLT_MAX_EXP;
    39. };
    40. }
    41. int main()
    42. {
    43. std::cout << std::boolalpha;
    44. std::cout << "std::numeric_limits::max_exponent10: "
    45. << std::numeric_limits<bool>::max_exponent10 << std::endl;
    46. std::cout << "std::numeric_limits::max_exponent10: "
    47. << std::numeric_limits<char>::max_exponent10 << std::endl;
    48. std::cout << "std::numeric_limits::max_exponent10: "
    49. << std::numeric_limits<signed char>::max_exponent10 << std::endl;
    50. std::cout << "std::numeric_limits::max_exponent10: "
    51. << std::numeric_limits<unsigned char>::max_exponent10 << std::endl;
    52. std::cout << "std::numeric_limits::max_exponent10: "
    53. << std::numeric_limits<wchar_t>::max_exponent10 << std::endl;
    54. std::cout << "std::numeric_limits::max_exponent10: "
    55. << std::numeric_limits<char16_t>::max_exponent10 << std::endl;
    56. std::cout << "std::numeric_limits::max_exponent10: "
    57. << std::numeric_limits<char32_t>::max_exponent10 << std::endl;
    58. std::cout << "std::numeric_limits::max_exponent10: "
    59. << std::numeric_limits<short>::max_exponent10 << std::endl;
    60. std::cout << "std::numeric_limits::max_exponent10: "
    61. << std::numeric_limits<unsigned short>::max_exponent10 << std::endl;
    62. std::cout << "std::numeric_limits::max_exponent10: "
    63. << std::numeric_limits<int>::max_exponent10 << std::endl;
    64. std::cout << "std::numeric_limits::max_exponent10: "
    65. << std::numeric_limits<unsigned int>::max_exponent10 << std::endl;
    66. std::cout << "std::numeric_limits::max_exponent10: "
    67. << std::numeric_limits<long>::max_exponent10 << std::endl;
    68. std::cout << "std::numeric_limits::max_exponent10: "
    69. << std::numeric_limits<unsigned long>::max_exponent10 << std::endl;
    70. std::cout << "std::numeric_limits::max_exponent10: "
    71. << std::numeric_limits<long long>::max_exponent10 << std::endl;
    72. std::cout << "std::numeric_limits::max_exponent10: "
    73. << std::numeric_limits<unsigned long long>::max_exponent10 << std::endl;
    74. std::cout << "std::numeric_limits::max_exponent10: "
    75. << std::numeric_limits<float>::max_exponent10 << std::endl;
    76. std::cout << "std::numeric_limits::max_exponent10: "
    77. << std::numeric_limits<double>::max_exponent10 << std::endl;
    78. std::cout << "std::numeric_limits::max_exponent10: "
    79. << std::numeric_limits<long double>::max_exponent10 << std::endl;
    80. std::cout << "std::numeric_limits::max_exponent10: "
    81. << std::numeric_limits<std::string>::max_exponent10 << std::endl;
    82. std::cout << "std::numeric_limits::max_exponent10: "
    83. << std::numeric_limits<SName>::max_exponent10 << std::endl;
    84. std::cout << "std::numeric_limits::max_exponent10: "
    85. << std::numeric_limits<SPartSpec>::max_exponent10 << std::endl;
    86. return 0;
    87. }

    输出

  • 相关阅读:
    Linux快速修改ip地址
    软件开发人员 Kubernetes 入门指南|Part 1
    【scikit-learn基础】--『监督学习』之 层次聚类
    剑指offer!年薪300万P9大佬揭秘Spring全家桶手册,刷完入职字节
    pg分组过滤
    VS生成exe软件和软件内部页面带图标
    Edexcel ALevel数学P2考题解析
    C++滑动窗口求最大值问题(单调队列,multiset,分块dp)
    【Spring专题】「技术原理」从源码角度去深入分析关于Spring的异常处理ExceptionHandler的实现原理
    如何用虚拟仿真实训室提质增效?
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/133829458