std::numeric_limits的使用

举报
liuzhen007 发表于 2021/05/27 19:05:59 2021/05/27
【摘要】 std::numeric_limits是C/C++11中的一个模板类,在库编译平台提供基础算术类型的极值等属性信息,取代传统C语言,所采用的预处理常数。比较常用的使用是对于给定的基础类型用来判断在当前系统上的最大值、最小值。下面通过一段程序看看std::numeric_limits是怎么使用的。 #include <iostream>#include <c...

std::numeric_limits是C/C++11中的一个模板类,在库编译平台提供基础算术类型的极值等属性信息,取代传统C语言,所采用的预处理常数。比较常用的使用是对于给定的基础类型用来判断在当前系统上的最大值、最小值。下面通过一段程序看看std::numeric_limits是怎么使用的。


  
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <limits>
  4. int main()
  5. {
  6. std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << std::endl;
  7. std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << std::endl;
  8. std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << std::endl;
  9. std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << std::endl;
  10. std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << std::endl;
  11. std::cout << "Minimum value for float: " << std::numeric_limits<float>::min() << std::endl; // min returns the smallest positive value the type can encode, not the lowest
  12. std::cout << "Lowest value for float: " << std::numeric_limits<float>::lowest() << std::endl; // the lowest value
  13. std::cout << "Maximum value for float: " << std::numeric_limits<float>::max() << std::endl;
  14. std::cout << "float is signed: " << std::numeric_limits<float>::is_signed << std::endl;
  15. std::cout << "Non-sign bits in float: " << std::numeric_limits<float>::digits << std::endl;
  16. std::cout << "float has infinity: " << std::numeric_limits<float>::has_infinity << std::endl;
  17. std::cout << "Minimum value for unsigned short: " << std::numeric_limits<unsigned short>::min() << std::endl;
  18. std::cout << "Maximum value for unsigned short: " << std::numeric_limits<unsigned short>::max() << std::endl;
  19. std::cout << "is_specialized(float): " << std::numeric_limits<float>::is_specialized << std::endl;
  20. std::cout << "is_integer(float): " << std::numeric_limits<float>::is_integer << std::endl;
  21. std::cout << "is_exact(float): " << std::numeric_limits<float>::is_exact << std::endl;
  22. std::cout << "is_bounded(float): " << std::numeric_limits<float>::is_bounded << std::endl;
  23. std::cout << "is_modulo(float): " << std::numeric_limits<float>::is_modulo << std::endl;
  24. std::cout << "is_iec559(float): " << std::numeric_limits<float>::is_iec559 << std::endl;
  25. std::cout << "digits10(float): " << std::numeric_limits<float>::digits10 << std::endl;
  26. std::cout << "radix(float): " << std::numeric_limits<float>::radix << std::endl;
  27. std::cout << "min_exponent(float): " << std::numeric_limits<float>::min_exponent << std::endl;
  28. std::cout << "max_exponent(float): " << std::numeric_limits<float>::max_exponent << std::endl;
  29. std::cout << "min_exponent10(float): " << std::numeric_limits<float>::min_exponent10 << std::endl;
  30. std::cout << "max_exponent10(float): " << std::numeric_limits<float>::max_exponent10 << std::endl;
  31. std::cout << "epsilon(float): " << std::numeric_limits<float>::epsilon() << std::endl;
  32. std::cout << "round_style(float): " << std::numeric_limits<float>::round_style << std::endl;
  33. std::cout << "The smallest nonzero denormalized value for float: "
  34. << std::numeric_limits<float>::denorm_min()<< std::endl;
  35. std::cout << "The difference between 1 and the smallest value greater than 1 for float: "
  36. << std::numeric_limits<float>::epsilon()<< std::endl;
  37. std::cout << "Whether float objects allow denormalized values: "
  38. << std::numeric_limits<float>::has_denorm << std::endl;
  39. std::cout << "Whether float objects can detect denormalized loss: "
  40. << std::numeric_limits<float>::has_denorm_loss << std::endl;
  41. std::cout << "Whether float objects have quiet_NaN: "
  42. << std::numeric_limits<float>::has_quiet_NaN << std::endl;
  43. std::cout << "Whether float objects have a signaling_NaN: "
  44. << std::numeric_limits<float>::has_signaling_NaN << std::endl;
  45. std::cout << "The base for type float is: "
  46. << std::numeric_limits<float>::radix << std::endl;
  47. std::cout << "The maximum rounding error for type float is: "
  48. << std::numeric_limits<float>::round_error() << std::endl;
  49. std::cout << "The rounding style for a double type is: "
  50. << std::numeric_limits<double>::round_style << std::endl;
  51. std::cout << "The signaling NaN for type float is: "
  52. << std::numeric_limits<float>::signaling_NaN() << std::endl;
  53. std::cout << "Whether float types can detect tinyness before rounding: "
  54. << std::numeric_limits<float>::tinyness_before << std::endl;
  55. std::cout << "Whether float types have implemented trapping: "
  56. << std::numeric_limits<float>::traps << std::endl;
  57. return 0;
  58. }

利用 g++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.71.0/gcc-head/include -std=c++11 的执行结果如下: 


  
  1. Minimum value for int: -2147483648
  2. Maximum value for int: 2147483647
  3. int is signed: true
  4. Non-sign bits in int: 31
  5. int has infinity: false
  6. Minimum value for float: 1.17549e-38
  7. Lowest value for float: -3.40282e+38
  8. Maximum value for float: 3.40282e+38
  9. float is signed: true
  10. Non-sign bits in float: 24
  11. float has infinity: true
  12. Minimum value for unsigned short: 0
  13. Maximum value for unsigned short: 65535
  14. is_specialized(float): true
  15. is_integer(float): false
  16. is_exact(float): false
  17. is_bounded(float): true
  18. is_modulo(float): false
  19. is_iec559(float): true
  20. digits10(float): 6
  21. radix(float): 2
  22. min_exponent(float): -125
  23. max_exponent(float): 128
  24. min_exponent10(float): -37
  25. max_exponent10(float): 38
  26. epsilon(float): 1.19209e-07
  27. round_style(float): 1
  28. The smallest nonzero denormalized value for float: 1.4013e-45
  29. The difference between 1 and the smallest value greater than 1 for float: 1.19209e-07
  30. Whether float objects allow denormalized values: 1
  31. Whether float objects can detect denormalized loss: false
  32. Whether float objects have quiet_NaN: true
  33. Whether float objects have a signaling_NaN: true
  34. The base for type float is: 2
  35. The maximum rounding error for type float is: 0.5
  36. The rounding style for a double type is: 1
  37. The signaling NaN for type float is: nan
  38. Whether float types can detect tinyness before rounding: false
  39. Whether float types have implemented trapping: false

 

 

文章来源: liuzhen.blog.csdn.net,作者:Data-Mining,版权归原作者所有,如需转载,请联系作者。

原文链接:liuzhen.blog.csdn.net/article/details/103072772

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。