sandbox
Loading...
Searching...
No Matches
concepts.hpp
1#ifndef LIBSBX_MATH_CONCEPTS_HPP_
2#define LIBSBX_MATH_CONCEPTS_HPP_
3
4#include <type_traits>
5#include <numbers>
6#include <limits>
7#include <cmath>
8
9namespace sbx::math {
10
11template<typename Type>
12concept numeric = (std::is_integral_v<Type> && !std::is_same_v<Type, bool>) || std::is_floating_point_v<Type>;
13
14
15
16template<typename Type>
17struct is_floating_point : std::bool_constant<std::is_floating_point_v<Type>> { };
18
19template<typename Type>
20inline constexpr bool is_floating_point_v = is_floating_point<Type>::value;
21
22template<typename Type>
23concept floating_point = is_floating_point_v<Type>;
24
25
26
27template<typename Type>
28struct is_integral : std::bool_constant<std::is_integral_v<Type> && !std::is_same_v<Type, bool>> { };
29
30template<typename Type>
31inline constexpr bool is_integral_v = is_integral<Type>::value;
32
33template<typename Type>
34concept integral = is_integral_v<Type>;
35
36template<typename Type>
37struct is_unsigned_integral : std::bool_constant<is_integral_v<Type> && std::is_unsigned_v<Type>> { };
38
39template<typename Type>
40inline constexpr bool is_unsigned_integral_v = is_unsigned_integral<Type>::value;
41
42template<typename Type>
43concept unsigned_integral = is_unsigned_integral_v<Type>;
44
45
46
47template<typename Type>
48struct is_scalar : std::bool_constant<is_floating_point_v<Type> || is_integral_v<Type>> { };
49
50template<typename Type>
51inline constexpr bool is_scalar_v = is_scalar<Type>::value;
52
53template<typename Type>
54concept scalar = is_scalar_v<Type>;
55
56
57
58template<typename>
60
61template<integral Type>
62struct comparision_traits<Type> {
63
64 template<scalar Other>
65 inline static constexpr auto equal(Type lhs, Other rhs) noexcept -> bool {
66 return lhs == static_cast<Type>(rhs);
67 }
68
69}; // template<integral Type>
70
71template<floating_point Type>
72struct comparision_traits<Type> {
73
74 inline static constexpr auto epsilon = std::numeric_limits<Type>::epsilon();
75
76 template<scalar Other>
77 inline static constexpr auto equal(Type lhs, Other rhs) noexcept -> bool {
78 return std::abs(lhs - static_cast<Type>(rhs)) <= epsilon;
79 }
80
81}; // template<floating_point Type>
82
83} // namespace sbx::math
84
85#endif // LIBSBX_MATH_CONCEPTS_HPP_
Definition: concepts.hpp:59
Definition: concepts.hpp:17
Definition: concepts.hpp:28
Definition: concepts.hpp:48
Definition: concepts.hpp:37