numeric_values.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. { \
  14. typedef type value_type; \
  15. static type zero() { return 0.0; } \
  16. static type infinity() \
  17. { \
  18. return std::numeric_limits< type >::infinity(); \
  19. } \
  20. };
  21. /**
  22. * This generic type reports various numeric values for some type. In the
  23. * general case, numeric values simply treat their maximum value as infinity
  24. * and the default-constructed value as 0.
  25. *
  26. * Specializations of this template can redefine the notions of zero and
  27. * infinity for various types. For example, the class is specialized for
  28. * floating point types to use the built in notion of infinity.
  29. */
  30. template < typename T > struct numeric_values
  31. {
  32. typedef T value_type;
  33. static T zero() { return T(); }
  34. static T infinity() { return (std::numeric_limits< T >::max)(); }
  35. };
  36. // Specializations for floating point types refer to 0.0 and their infinity
  37. // value defined by numeric_limits.
  38. BOOST_GRAPH_SPECIALIZE_NUMERIC_FLOAT(float)
  39. BOOST_GRAPH_SPECIALIZE_NUMERIC_FLOAT(double)
  40. BOOST_GRAPH_SPECIALIZE_NUMERIC_FLOAT(long double)
  41. #undef BOOST_GRAPH_SPECIALIZE_NUMERIC_VALUE
  42. }
  43. #endif