numeric_values.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // (C) Copyright 2007-2009 Andrew Sutton
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GRAPH_NUMERIC_VALUES_HPP
  7. #define BOOST_GRAPH_NUMERIC_VALUES_HPP
  8. #include <limits>
  9. namespace boost
  10. {
  11. #define BOOST_GRAPH_SPECIALIZE_NUMERIC_FLOAT(type) \
  12. template <> struct numeric_values<type> { \
  13. typedef type value_type; \
  14. static type zero() { return 0.0; } \
  15. static type infinity() { return std::numeric_limits<type>::infinity(); } \
  16. };
  17. /**
  18. * This generic type reports various numeric values for some type. In the
  19. * general case, numeric values simply treat their maximum value as infinity
  20. * and the default-constructed value as 0.
  21. *
  22. * Specializations of this template can redefine the notions of zero and
  23. * infinity for various types. For example, the class is specialized for
  24. * floating point types to use the built in notion of infinity.
  25. */
  26. template <typename T>
  27. struct numeric_values
  28. {
  29. typedef T value_type;
  30. static T zero()
  31. { return T(); }
  32. static T infinity()
  33. { return (std::numeric_limits<T>::max)(); }
  34. };
  35. // Specializations for floating point types refer to 0.0 and their infinity
  36. // value defined by numeric_limits.
  37. BOOST_GRAPH_SPECIALIZE_NUMERIC_FLOAT(float)
  38. BOOST_GRAPH_SPECIALIZE_NUMERIC_FLOAT(double)
  39. BOOST_GRAPH_SPECIALIZE_NUMERIC_FLOAT(long double)
  40. #undef BOOST_GRAPH_SPECIALIZE_NUMERIC_VALUE
  41. }
  42. #endif