GPR5100 - Rollback
Loading...
Searching...
No Matches
basic.h
Go to the documentation of this file.
1
4#pragma once
5
6#include <random>
7
8namespace core
9{
15constexpr float Abs(float v)
16{
17 return v < 0.0f ? -v : v;
18}
19
20constexpr bool Equal(float a, float b, float epsilon = 0.0000001f)
21{
22 return Abs(a - b) < epsilon;
23}
24
35constexpr float Lerp(float start, float end, float t)
36{
37 return start + (end - start) * t;
38}
39
48template<typename T>
49constexpr float Clamp(T value, T lower, T upper)
50{
51 return value < lower ? lower : (value > upper ? upper : value);
52}
53
54template<typename T>
55typename std::enable_if<std::is_integral<T>::value, T>::type RandomRange(T start, T end)
56{
57 static std::random_device rd; //Will be used to obtain a seed for the random number engine
58 static std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
59 std::uniform_int_distribution<T> dis(start, end);
60 return dis(gen);
61}
62
63template<typename T>
64typename std::enable_if<std::is_floating_point<T>::value, T>::type RandomRange(T start, T end)
65{
66 static std::random_device rd; //Will be used to obtain a seed for the random number engine
67 static std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
68 std::uniform_real_distribution<T> dis(start, end);
69 return dis(gen);
70}
71template<typename T>
72T constexpr SqrtNewtonRaphson(T x, T curr, T prev)
73{
74 return curr == prev
75 ? curr
76 : SqrtNewtonRaphson<T>(x, (curr + x / curr) * 0.5, curr);
77}
78
79
88template<typename T>
89T constexpr Sqrt(T x)
90{
91 return x >= 0 && x < std::numeric_limits<T>::infinity()
92 ? SqrtNewtonRaphson<T>(x, x, 0)
93 : std::numeric_limits<T>::quiet_NaN();
94}
95}
constexpr float Clamp(T value, T lower, T upper)
Clamp is a math function that clamps the input value between an upper and lower value.
Definition: basic.h:49
T constexpr Sqrt(T x)
Sqrt is a constexpr version of the square root.
Definition: basic.h:89
constexpr float Abs(float v)
Abs is a constexpr function that returns the absolute value.
Definition: basic.h:15
constexpr float Lerp(float start, float end, float t)
Lerp is a function calculating the linear interpolation between two points.
Definition: basic.h:35