compare.hpp 944 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #pragma once
  2. #include <algorithm>
  3. #include <cmath>
  4. namespace math {
  5. template <typename T>
  6. bool between(T val, T min, T max) {
  7. return val >= min && val < max;
  8. }
  9. template <typename T>
  10. bool approx_equal(T lhs, T rhs, T eps) {
  11. T const a = std::abs(lhs);
  12. T const b = std::abs(rhs);
  13. return std::abs(lhs - rhs) <= (std::max(a, b) * eps);
  14. }
  15. template <typename T>
  16. bool essentially_equal(T lhs, T rhs, T eps) {
  17. T const a = std::abs(lhs);
  18. T const b = std::abs(rhs);
  19. return std::abs(lhs - rhs) <= (std::min(a, b) * eps);
  20. }
  21. template <typename T>
  22. bool definitely_greater(T lhs, T rhs, T eps) {
  23. T const a = std::abs(lhs);
  24. T const b = std::abs(rhs);
  25. return (lhs - rhs) > (std::max(a, b) * eps);
  26. }
  27. template <typename T>
  28. bool definitely_less(T lhs, T rhs, T eps) {
  29. T const a = std::abs(lhs);
  30. T const b = std::abs(rhs);
  31. return (rhs - lhs) > (std::max(a, b) * eps);
  32. }
  33. }