number_types.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2014-2015 Oracle and/or its affiliates.
  4. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Licensed under the Boost Software License version 1.0.
  7. // http://www.boost.org/users/license.html
  8. #ifndef BOOST_GEOMETRY_TEST_UTIL_NUMBER_TYPES_HPP
  9. #define BOOST_GEOMETRY_TEST_UTIL_NUMBER_TYPES_HPP
  10. #include <cmath>
  11. // define a custom number type and its sqrt in own namespace
  12. namespace number_types
  13. {
  14. template <typename T>
  15. struct custom
  16. {
  17. typedef custom<T> self;
  18. T m_value;
  19. custom() : m_value(0) {}
  20. explicit custom(T const& value) : m_value(value) {}
  21. bool operator<(self const& other) const
  22. {
  23. return m_value < other.m_value;
  24. }
  25. self operator-() const
  26. {
  27. return self(-m_value);
  28. }
  29. self operator-(self const& other) const
  30. {
  31. return self(m_value - other.m_value);
  32. }
  33. };
  34. template <typename T>
  35. inline custom<T> sqrt(custom<T> const& c)
  36. {
  37. return custom<T>(std::sqrt(c.m_value));
  38. }
  39. template <typename T>
  40. inline custom<T> fabs(custom<T> const& c)
  41. {
  42. return custom<T>(c.m_value < T(0) ? c.m_value : -c.m_value);
  43. }
  44. } // namespace number_types
  45. // define a custom number type with sqrt in global namespace
  46. namespace number_types
  47. {
  48. template <typename T>
  49. struct custom_with_global_sqrt
  50. {
  51. typedef custom_with_global_sqrt<T> self;
  52. T m_value;
  53. custom_with_global_sqrt() : m_value(0) {}
  54. explicit custom_with_global_sqrt(T const& value) : m_value(value) {}
  55. bool operator<(self const& other) const
  56. {
  57. return m_value < other.m_value;
  58. }
  59. self operator-() const
  60. {
  61. return self(-m_value);
  62. }
  63. self operator-(self const& other) const
  64. {
  65. return self(m_value - other.m_value);
  66. }
  67. };
  68. } // namespace number_types
  69. template <typename T>
  70. inline number_types::custom_with_global_sqrt<T>
  71. sqrt(number_types::custom_with_global_sqrt<T> const& c)
  72. {
  73. return number_types::custom_with_global_sqrt<T>(std::sqrt(c.m_value));
  74. }
  75. template <typename T>
  76. inline number_types::custom_with_global_sqrt<T>
  77. fabs(number_types::custom_with_global_sqrt<T> const& c)
  78. {
  79. return number_types::custom_with_global_sqrt<T>
  80. (c.m_value < T(0) ? c.m_value : -c.m_value);
  81. }
  82. // define a custom number type and its sqrt in global namespace
  83. template <typename T>
  84. struct custom_global
  85. {
  86. typedef custom_global<T> self;
  87. T m_value;
  88. custom_global() : m_value(0) {}
  89. explicit custom_global(T const& value) : m_value(value) {}
  90. bool operator<(self const& other) const
  91. {
  92. return m_value < other.m_value;
  93. }
  94. self operator-() const
  95. {
  96. return self(-m_value);
  97. }
  98. self operator-(self const& other) const
  99. {
  100. return self(m_value - other.m_value);
  101. }
  102. };
  103. template <typename T>
  104. inline custom_global<T> sqrt(custom_global<T> const& c)
  105. {
  106. return custom_global<T>(std::sqrt(c.m_value));
  107. }
  108. template <typename T>
  109. inline custom_global<T> fabs(custom_global<T> const& c)
  110. {
  111. return custom_global<T>(c.m_value < T(0) ? c.m_value : -c.m_value);
  112. }
  113. // custom number type without functions definition
  114. template <typename T>
  115. struct custom_raw
  116. {
  117. typedef custom_raw<T> self;
  118. T m_value;
  119. custom_raw() : m_value(0) {}
  120. explicit custom_raw(T const& value) : m_value(value) {}
  121. bool operator<(self const& other) const
  122. {
  123. return m_value < other.m_value;
  124. }
  125. self operator-() const
  126. {
  127. return self(-m_value);
  128. }
  129. self operator-(self const& other) const
  130. {
  131. return self(m_value - other.m_value);
  132. }
  133. };
  134. #endif // BOOST_GEOMETRY_TEST_UTIL_NUMBER_TYPES_HPP