当前位置:   article > 正文

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

std::numeric_limits::epsilon()

数值极限

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

定义于头文件 <limits>

template< class T > class numeric_limits;

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

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

std::numeric_limits<T>::epsilon

static T epsilon() throw();

(C++11 前)

static constexpr T epsilon() noexcept;

(C++11 起)

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

返回值

Tstd::numeric_limits<T>::epsilon()
/* non-specialized */T()
boolfalse
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_EPSILON
doubleDBL_EPSILON
long doubleLDBL_EPSILON

 调用示例

  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. static _GLIBCXX_USE_CONSTEXPR bool traps = true;
  40. static _GLIBCXX_USE_CONSTEXPR bool tinyness_before = true;
  41. static _GLIBCXX_CONSTEXPR int
  42. min() _GLIBCXX_USE_NOEXCEPT { return CHAR_MIN ; }
  43. static _GLIBCXX_CONSTEXPR int
  44. lowest() _GLIBCXX_USE_NOEXCEPT { return CHAR_MIN ; }
  45. static _GLIBCXX_CONSTEXPR int
  46. max() _GLIBCXX_USE_NOEXCEPT { return CHAR_MAX ; }
  47. static _GLIBCXX_CONSTEXPR int
  48. epsilon() _GLIBCXX_USE_NOEXCEPT { return LDBL_EPSILON ; }
  49. };
  50. }
  51. int main()
  52. {
  53. std::cout << std::boolalpha;
  54. std::cout << "std::numeric_limits<bool>::epsilon(): "
  55. << std::numeric_limits<bool>::epsilon() << std::endl;
  56. std::cout << "std::numeric_limits<char>::epsilon(): "
  57. << std::numeric_limits<char>::epsilon() << std::endl;
  58. std::cout << "std::numeric_limits<signed char>::epsilon(): "
  59. << std::numeric_limits<signed char>::epsilon() << std::endl;
  60. std::cout << "std::numeric_limits<unsigned char>::epsilon(): "
  61. << std::numeric_limits<unsigned char>::epsilon() << std::endl;
  62. std::cout << "std::numeric_limits<wchar_t>::epsilon(): "
  63. << std::numeric_limits<wchar_t>::epsilon() << std::endl;
  64. std::cout << "std::numeric_limits<char16_t>::epsilon(): "
  65. << std::numeric_limits<char16_t>::epsilon() << std::endl;
  66. std::cout << "std::numeric_limits<char32_t>::epsilon(): "
  67. << std::numeric_limits<char32_t>::epsilon() << std::endl;
  68. std::cout << "std::numeric_limits<short>::epsilon(): "
  69. << std::numeric_limits<short>::epsilon() << std::endl;
  70. std::cout << "std::numeric_limits<unsigned short>::epsilon(): "
  71. << std::numeric_limits<unsigned short>::epsilon() << std::endl;
  72. std::cout << "std::numeric_limits<int>::epsilon(): "
  73. << std::numeric_limits<int>::epsilon() << std::endl;
  74. std::cout << "std::numeric_limits<unsigned int>::epsilon(): "
  75. << std::numeric_limits<unsigned int>::epsilon() << std::endl;
  76. std::cout << "std::numeric_limits<long>::epsilon(): "
  77. << std::numeric_limits<long>::epsilon() << std::endl;
  78. std::cout << "std::numeric_limits<unsigned long>::epsilon(): "
  79. << std::numeric_limits<unsigned long>::epsilon() << std::endl;
  80. std::cout << "std::numeric_limits<long long>::epsilon(): "
  81. << std::numeric_limits<long long>::epsilon() << std::endl;
  82. std::cout << "std::numeric_limits<unsigned long long>::epsilon(): "
  83. << std::numeric_limits<unsigned long long>::epsilon() << std::endl;
  84. std::cout << "std::numeric_limits<float>::epsilon(): "
  85. << std::numeric_limits<float>::epsilon() << std::endl;
  86. std::cout << "std::numeric_limits<double>::epsilon(): "
  87. << std::numeric_limits<double>::epsilon() << std::endl;
  88. std::cout << "std::numeric_limits<long double>::epsilon(): "
  89. << std::numeric_limits<long double>::epsilon() << std::endl;
  90. std::cout << "std::numeric_limits<std::string>::epsilon(): "
  91. << std::numeric_limits<std::string>::epsilon() << std::endl;
  92. //必须偏特化
  93. // std::cout << "std::numeric_limits<SName>::epsilon(): "
  94. // << std::numeric_limits<SName>::epsilon() << std::endl;
  95. std::cout << "std::numeric_limits<SPartSpec>::epsilon(): "
  96. << std::numeric_limits<SPartSpec>::epsilon() << std::endl;
  97. return 0;
  98. }

输出

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

std::numeric_limits<T>::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<T>::is_integer == false 才有意义。

返回值

Tstd::numeric_limits<T>::round_error()
/* non-specialized */T()
boolfalse
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​
float0.5F
double0.5
long double0.5L

调用示例

  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. static _GLIBCXX_USE_CONSTEXPR bool traps = true;
  40. static _GLIBCXX_USE_CONSTEXPR bool tinyness_before = true;
  41. static _GLIBCXX_CONSTEXPR int
  42. min() _GLIBCXX_USE_NOEXCEPT { return CHAR_MIN ; }
  43. static _GLIBCXX_CONSTEXPR int
  44. lowest() _GLIBCXX_USE_NOEXCEPT { return CHAR_MIN ; }
  45. static _GLIBCXX_CONSTEXPR int
  46. max() _GLIBCXX_USE_NOEXCEPT { return CHAR_MAX ; }
  47. static _GLIBCXX_CONSTEXPR int
  48. epsilon() _GLIBCXX_USE_NOEXCEPT { return LDBL_EPSILON ; }
  49. static _GLIBCXX_CONSTEXPR double
  50. round_error() _GLIBCXX_USE_NOEXCEPT { return 0.5F ; }
  51. };
  52. }
  53. int main()
  54. {
  55. std::cout << std::boolalpha;
  56. std::cout << "std::numeric_limits<bool>::round_error(): "
  57. << std::numeric_limits<bool>::round_error() << std::endl;
  58. std::cout << "std::numeric_limits<char>::round_error(): "
  59. << std::numeric_limits<char>::round_error() << std::endl;
  60. std::cout << "std::numeric_limits<signed char>::round_error(): "
  61. << std::numeric_limits<signed char>::round_error() << std::endl;
  62. std::cout << "std::numeric_limits<unsigned char>::round_error(): "
  63. << std::numeric_limits<unsigned char>::round_error() << std::endl;
  64. std::cout << "std::numeric_limits<wchar_t>::round_error(): "
  65. << std::numeric_limits<wchar_t>::round_error() << std::endl;
  66. std::cout << "std::numeric_limits<char16_t>::round_error(): "
  67. << std::numeric_limits<char16_t>::round_error() << std::endl;
  68. std::cout << "std::numeric_limits<char32_t>::round_error(): "
  69. << std::numeric_limits<char32_t>::round_error() << std::endl;
  70. std::cout << "std::numeric_limits<short>::round_error(): "
  71. << std::numeric_limits<short>::round_error() << std::endl;
  72. std::cout << "std::numeric_limits<unsigned short>::round_error(): "
  73. << std::numeric_limits<unsigned short>::round_error() << std::endl;
  74. std::cout << "std::numeric_limits<int>::round_error(): "
  75. << std::numeric_limits<int>::round_error() << std::endl;
  76. std::cout << "std::numeric_limits<unsigned int>::round_error(): "
  77. << std::numeric_limits<unsigned int>::round_error() << std::endl;
  78. std::cout << "std::numeric_limits<long>::round_error(): "
  79. << std::numeric_limits<long>::round_error() << std::endl;
  80. std::cout << "std::numeric_limits<unsigned long>::round_error(): "
  81. << std::numeric_limits<unsigned long>::round_error() << std::endl;
  82. std::cout << "std::numeric_limits<long long>::round_error(): "
  83. << std::numeric_limits<long long>::round_error() << std::endl;
  84. std::cout << "std::numeric_limits<unsigned long long>::round_error(): "
  85. << std::numeric_limits<unsigned long long>::round_error() << std::endl;
  86. std::cout << "std::numeric_limits<float>::round_error(): "
  87. << std::numeric_limits<float>::round_error() << std::endl;
  88. std::cout << "std::numeric_limits<double>::round_error(): "
  89. << std::numeric_limits<double>::round_error() << std::endl;
  90. std::cout << "std::numeric_limits<long double>::round_error(): "
  91. << std::numeric_limits<long double>::round_error() << std::endl;
  92. std::cout << "std::numeric_limits<std::string>::round_error(): "
  93. << std::numeric_limits<std::string>::round_error() << std::endl;
  94. //必须偏特化
  95. // std::cout << "std::numeric_limits<SName>::round_error(): "
  96. // << std::numeric_limits<SName>::round_error() << std::endl;
  97. std::cout << "std::numeric_limits<SPartSpec>::round_error(): "
  98. << std::numeric_limits<SPartSpec>::round_error() << std::endl;
  99. return 0;
  100. }

输出

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

std::numeric_limits<T>::infinity

static T infinity() throw();

(C++11 前)

static constexpr T infinity() noexcept;

(C++11 起)

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

返回值

Tstd::numeric_limits<T>::infinity()
/* non-specialized */T()
boolfalse
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​
floatHUGE_VALF
doubleHUGE_VAL
long doubleHUGE_VALL

调用示例

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

输出

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/302778
推荐阅读
相关标签
  

闽ICP备14008679号