bounds.qbk 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. [/
  2. Boost.Optional
  3. Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. ]
  8. [section bounds<> traits class]
  9. [section Introduction]
  10. To determine the ranges of numeric types with `std::numeric_limits` \[18.2.1\],
  11. different syntax have to be used depending on numeric type. Specifically,
  12. `numeric_limits<T>::min()` for integral types returns the minimum finite value,
  13. whereas for floating point types it returns the minimum positive normalized
  14. value. The difference in semantics makes client code unnecessarily complex
  15. and error prone.
  16. `boost::numeric::bounds<>` provides a consistent interface for retrieving the
  17. maximum finite value, the minimum finite value and the minimum positive normalized
  18. value (0 for integral types) for numeric types. The selection of implementation
  19. is performed at compile time, so there is no runtime overhead.
  20. [endsect]
  21. [section traits class bounds<N>]
  22. template<class N>
  23. struct bounds
  24. {
  25. static N lowest () { return implementation_defined; }
  26. static N highest () { return implementation_defined; }
  27. static N smallest() { return implementation_defined; }
  28. };
  29. [heading Members]
  30. [: `lowest()` ]
  31. Returns the minimum finite value, equivalent to `numeric_limits<T>::min()` when `T`
  32. is an integral type, and to `-numeric_limits<T>::max()` when `T` is a floating point type.
  33. [: `highest()` ]
  34. Returns the maximum finite value, equivalent to `numeric_limits<T>::max()`.
  35. [: `smallest()` ]
  36. Returns the smallest positive normalized value for floating point types with
  37. denormalization, or returns 0 for integral types.
  38. [endsect]
  39. [section Examples]
  40. The following example demonstrates the use of `numeric::bounds<>` and the
  41. equivalent code using `numeric_limits`:
  42. #include <iostream>
  43. #include <boost/numeric/conversion/bounds.hpp>
  44. #include <boost/limits.hpp>
  45. int main() {
  46. std::cout << "numeric::bounds versus numeric_limits example.\n";
  47. std::cout << "The maximum value for float:\n";
  48. std::cout << boost::numeric::bounds<float>::highest() << "\n";
  49. std::cout << std::numeric_limits<float>::max() << "\n";
  50. std::cout << "The minimum value for float:\n";
  51. std::cout << boost::numeric::bounds<float>::lowest() << "\n";
  52. std::cout << -std::numeric_limits<float>::max() << "\n";
  53. std::cout << "The smallest positive value for float:\n";
  54. std::cout << boost::numeric::bounds<float>::smallest() << "\n";
  55. std::cout << std::numeric_limits<float>::min() << "\n";
  56. return 0;
  57. }
  58. [endsect]
  59. [endsect]