C++和C语言中float最大值,float最小值等特殊数的表示

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/fengbingchun/article/details/77922558

在C/C++11中,std::numeric_limits为模板类,在库编译平台提供基础算术类型的极值等属性信息,取代传统C语言,所采用的预处理常数。比较常用的使用是对于给定的基础类型用来判断在当前系统上的最大值、最小值。若使用此类,需包含<limits>头文件。它支持的基础算术类型包括如下:


min、max与C库宏常量的关系如下:


测试代码如下:


   
   
  1. #include "numeric_limits.hpp"
  2. #include <limits>
  3. #include <iostream>
  4. //
  5. /* reference:
  6. http://www.cplusplus.com/reference/limits/numeric_limits/
  7. https://msdn.microsoft.com/en-us/library/c707ct0t.aspx
  8. */
  9. int test_numeric_limits_1()
  10. {
  11. std:: cout << std::boolalpha;
  12. std:: cout << "Minimum value for int: " << std::numeric_limits< int>::min() << std:: endl;
  13. std:: cout << "Maximum value for int: " << std::numeric_limits< int>::max() << std:: endl;
  14. std:: cout << "int is signed: " << std::numeric_limits< int>::is_signed << std:: endl;
  15. std:: cout << "Non-sign bits in int: " << std::numeric_limits< int>::digits << std:: endl;
  16. std:: cout << "int has infinity: " << std::numeric_limits< int>::has_infinity << std:: endl;
  17. 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
  18. std:: cout << "Lowest value for float: " << std::numeric_limits< float>::lowest() << std:: endl; // the lowest value
  19. std:: cout << "Maximum value for float: " << std::numeric_limits< float>::max() << std:: endl;
  20. std:: cout << "float is signed: " << std::numeric_limits< float>::is_signed << std:: endl;
  21. std:: cout << "Non-sign bits in float: " << std::numeric_limits< float>::digits << std:: endl;
  22. std:: cout << "float has infinity: " << std::numeric_limits< float>::has_infinity << std:: endl;
  23. std:: cout << "Minimum value for unsigned short: " << std::numeric_limits< unsigned short>::min() << std:: endl;
  24. std:: cout << "Maximum value for unsigned short: " << std::numeric_limits< unsigned short>::max() << std:: endl;
  25. std:: cout << "is_specialized(float): " << std::numeric_limits< float>::is_specialized << std:: endl;
  26. std:: cout << "is_integer(float): " << std::numeric_limits< float>::is_integer << std:: endl;
  27. std:: cout << "is_exact(float): " << std::numeric_limits< float>::is_exact << std:: endl;
  28. std:: cout << "is_bounded(float): " << std::numeric_limits< float>::is_bounded << std:: endl;
  29. std:: cout << "is_modulo(float): " << std::numeric_limits< float>::is_modulo << std:: endl;
  30. std:: cout << "is_iec559(float): " << std::numeric_limits< float>::is_iec559 << std:: endl;
  31. std:: cout << "digits10(float): " << std::numeric_limits< float>::digits10 << std:: endl;
  32. std:: cout << "radix(float): " << std::numeric_limits< float>::radix << std:: endl;
  33. std:: cout << "min_exponent(float): " << std::numeric_limits< float>::min_exponent << std:: endl;
  34. std:: cout << "max_exponent(float): " << std::numeric_limits< float>::max_exponent << std:: endl;
  35. std:: cout << "min_exponent10(float): " << std::numeric_limits< float>::min_exponent10 << std:: endl;
  36. std:: cout << "max_exponent10(float): " << std::numeric_limits< float>::max_exponent10 << std:: endl;
  37. std:: cout << "epsilon(float): " << std::numeric_limits< float>::epsilon() << std:: endl;
  38. std:: cout << "round_style(float): " << std::numeric_limits< float>::round_style << std:: endl;
  39. std:: cout << "The smallest nonzero denormalized value for float: "
  40. << std::numeric_limits< float>::denorm_min()<< std:: endl;
  41. std:: cout << "The difference between 1 and the smallest value greater than 1 for float: "
  42. << std::numeric_limits< float>::epsilon()<< std:: endl;
  43. std:: cout << "Whether float objects allow denormalized values: "
  44. << std::numeric_limits< float>::has_denorm << std:: endl;
  45. std:: cout << "Whether float objects can detect denormalized loss: "
  46. << std::numeric_limits< float>::has_denorm_loss << std:: endl;
  47. std:: cout << "Whether float objects have quiet_NaN: "
  48. << std::numeric_limits< float>::has_quiet_NaN << std:: endl;
  49. std:: cout << "Whether float objects have a signaling_NaN: "
  50. << std::numeric_limits< float>::has_signaling_NaN << std:: endl;
  51. std:: cout << "The base for type float is: "
  52. << std::numeric_limits< float>::radix << std:: endl;
  53. std:: cout << "The maximum rounding error for type float is: "
  54. << std::numeric_limits< float>::round_error() << std:: endl;
  55. std:: cout << "The rounding style for a double type is: "
  56. << std::numeric_limits< double>::round_style << std:: endl;
  57. std:: cout << "The signaling NaN for type float is: "
  58. << std::numeric_limits< float>::signaling_NaN() << std:: endl;
  59. std:: cout << "Whether float types can detect tinyness before rounding: "
  60. << std::numeric_limits< float>::tinyness_before << std:: endl;
  61. std:: cout << "Whether float types have implemented trapping: "
  62. << std::numeric_limits< float>::traps << std:: endl;
  63. return 0;
  64. }
在windows10 64位上执行结果如下:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值