-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtility.mpp
More file actions
62 lines (55 loc) · 2.01 KB
/
Utility.mpp
File metadata and controls
62 lines (55 loc) · 2.01 KB
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
56
57
58
59
60
61
62
export module CppUtils.Math.Utility;
import std;
import CppUtils.Type.Concept;
export namespace CppUtils::Math
{
// Plus utile en C++23 (std::abs constexpr)
[[nodiscard]] inline constexpr auto absolute(auto number) -> decltype(auto)
{
if constexpr (std::unsigned_integral<decltype(number)>)
return number;
else if constexpr (std::floating_point<decltype(number)>)
return number < 0 ? -number : number;
else
return number < 0 ?
static_cast<std::make_unsigned_t<decltype(number)>>(-number) :
static_cast<std::make_unsigned_t<decltype(number)>>(number);
}
static_assert(absolute(0) == 0u);
static_assert(absolute(1) == 1u);
static_assert(absolute(-1) == 1u);
static_assert(absolute(0.0) == 0.0);
static_assert(absolute(1.5) == 1.5);
static_assert(absolute(-1.5) == 1.5);
static_assert(absolute(0u) == 0u);
static_assert(absolute(1u) == 1u);
template<std::floating_point Float>
[[nodiscard]] inline constexpr auto isEqual(Float lhs, Float rhs, Float epsilon = std::numeric_limits<Float>::epsilon()) noexcept -> bool
{
auto difference = absolute(lhs - rhs);
auto maximum = std::max(absolute(lhs), absolute(rhs));
if (maximum >= Float{1})
return difference <= epsilon * maximum;
else if (maximum > Float{0})
return difference / maximum <= epsilon;
return true;
}
static_assert(isEqual(0.0, 0.0));
static_assert(isEqual(0.1 + 0.2, 0.3));
static_assert(not isEqual(0.1 + 0.2, 0.29));
static_assert(not isEqual(0.1 + 0.2, 0.31));
template<Type::Numeric T>
[[nodiscard]] inline constexpr auto isBetween(T value, T low, T high) noexcept -> bool
{
return low <= value and value <= high;
}
static_assert(isBetween(5, 0, 10));
static_assert(not isBetween(-5, 0, 10));
static_assert(not isBetween(15, 0, 10));
static_assert(isBetween(5.0, 0.0, 10.0));
static_assert(not isBetween(-5.0, 0.0, 10.0));
static_assert(not isBetween(15.0, 0.0, 10.0));
static_assert(isBetween(5.0f, 0.0f, 10.0f));
static_assert(not isBetween(-5.0f, 0.0f, 10.0f));
static_assert(not isBetween(15.0f, 0.0f, 10.0f));
}