diff options
| author | Mel <einebeere@gmail.com> | 2023-12-07 01:16:11 +0100 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2023-12-07 01:16:11 +0100 |
| commit | c1a0768e55604687e82243bf64acd88d97a37ba0 (patch) | |
| tree | 9ab0e1a5a1a9e3e8089c5827fa0c275f88acf1d4 /src/Math/Functions.hpp | |
| parent | ca8b16620ec207f2b32edd1f5d46f7b0bfb0a14c (diff) | |
| download | meowcraft-c1a0768e55604687e82243bf64acd88d97a37ba0.tar.zst meowcraft-c1a0768e55604687e82243bf64acd88d97a37ba0.zip | |
Consolidate mathematical functions into single file and add more
Diffstat (limited to 'src/Math/Functions.hpp')
| -rw-r--r-- | src/Math/Functions.hpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/Math/Functions.hpp b/src/Math/Functions.hpp new file mode 100644 index 0000000..fd99c6d --- /dev/null +++ b/src/Math/Functions.hpp @@ -0,0 +1,46 @@ +#pragma once + +#include <cmath> +#include "Constants.hpp" + +namespace Math { + +// I. Trigonometric functions. + +template<typename T> +T radians(T degrees) { + return degrees * PI / 180.0f; +} + +template<typename T> +T degrees(T radians) { + return radians * 180.0f / PI; +} + +// II. Mathematical functions. + +template <typename T> +T sigmoid(T x) { + return 1 / (1 + std::pow(E, -x)); +} + +// Returns the least nonnegative remainder of a % b. +// Euclidian definition of modulo. +template <typename A, typename B> +auto mod(A a, B b) -> decltype(a % b) { + return (a % b + b) % b; +} + +template <typename T, typename R = T> +R sign(T x) { + return x < 0 ? -1 : 1; +} + +// III. Utility functions. + +template <typename T> +Bool floats_equal(T a, T b, Real epsilon = 0.0001f) { + return std::abs(a - b) < epsilon; +} + +} \ No newline at end of file |
