math.h 855 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef MATH_H
  2. #define MATH_H
  3. #include <cstdint>
  4. #include <limits>
  5. #include <cmath>
  6. uint32_t getDelta(uint32_t prev, uint32_t now);
  7. uint32_t getDelta(uint32_t prev, uint32_t now, uint32_t max);
  8. template<typename T>
  9. T sign(T value) {
  10. if (value > 0) {
  11. return 1;
  12. }
  13. if (value < 0) {
  14. return -1;
  15. }
  16. return 0;
  17. }
  18. template<typename T, typename U>
  19. T clamp(U value) {
  20. if (value >= std::numeric_limits<T>().max()) {
  21. return std::numeric_limits<T>().max();
  22. }
  23. if (value <= std::numeric_limits<T>().min()) {
  24. return std::numeric_limits<T>().min();
  25. }
  26. return value;
  27. }
  28. template<typename T>
  29. T min(T x, T y) {
  30. if (x < y) {
  31. return x;
  32. }
  33. return y;
  34. }
  35. template<typename T>
  36. T max(T x, T y) {
  37. if (x > y) {
  38. return x;
  39. }
  40. return y;
  41. }
  42. template<typename T>
  43. T hypot(T x, T y) {
  44. return std::sqrt(x * x + y * y);
  45. }
  46. #endif