blob: e1e44e05695cdf8bfaee394129f33a6380da8b5f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#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;
}
// Returns the sign of a number.
// -1 if x < 0, 1 if x > 0, 0 if x == 0.
template <typename T, typename R = T>
R sign(T x) {
return (x > 0) - (x < 0);
}
// III. Utility functions.
// Compares two floating point numbers for equality, with a given epsilon.
template <typename T>
Bool floats_equal(T a, T b, Real epsilon = 0.0001f) {
return std::abs(a - b) < epsilon;
}
// Compares two floating point numbers for inequality, with a given epsilon.
template <typename T>
Bool floats_less(T a, T b, Real epsilon = 0.0001f) {
return a < b - epsilon;
}
}
|