From c1a0768e55604687e82243bf64acd88d97a37ba0 Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 7 Dec 2023 01:16:11 +0100 Subject: Consolidate mathematical functions into single file and add more --- src/Math/Functions.hpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/Math/Functions.hpp (limited to 'src/Math/Functions.hpp') 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 +#include "Constants.hpp" + +namespace Math { + +// I. Trigonometric functions. + +template +T radians(T degrees) { + return degrees * PI / 180.0f; +} + +template +T degrees(T radians) { + return radians * 180.0f / PI; +} + +// II. Mathematical functions. + +template +T sigmoid(T x) { + return 1 / (1 + std::pow(E, -x)); +} + +// Returns the least nonnegative remainder of a % b. +// Euclidian definition of modulo. +template +auto mod(A a, B b) -> decltype(a % b) { + return (a % b + b) % b; +} + +template +R sign(T x) { + return x < 0 ? -1 : 1; +} + +// III. Utility functions. + +template +Bool floats_equal(T a, T b, Real epsilon = 0.0001f) { + return std::abs(a - b) < epsilon; +} + +} \ No newline at end of file -- cgit 1.4.1