Maipa's Standard Library Extension 1.5.6
mstd
Loading...
Searching...
No Matches
math_functions.hpp
1/*
2 * mstd - Maipa's Standard Library
3 *
4 * Licensed under the BSD 3-Clause License with Attribution Requirement.
5 * See the LICENSE file for details: https://github.com/MAIPA01/mstd/blob/main/LICENSE
6 *
7 * Copyright (c) 2025, Patryk Antosik (MAIPA01)
8 */
9
10#pragma once
11#ifndef _MSTD_MATH_FUNCTIONS_HPP_
12 #define _MSTD_MATH_FUNCTIONS_HPP_
13
14 #include <mstd/config.hpp>
15
17_MSTD_WARNING("this is only available for c++17 and greater!");
18 #else
19
20 #include <mstd/arithmetic_types.hpp>
21
22namespace mstd {
24 template<arithmetic T>
25 #else
26 template<class T, std::enable_if_t<std::is_arithmetic_v<T>, bool> = true>
27 #endif
28 _MSTD_INLINE17 _MSTD_CONSTEXPR20 T signum(const T& x) noexcept {
30 return static_cast<T>((static_cast<T>(0) < x) - (x < static_cast<T>(0)));
31 }
32 else { return static_cast<T>(0) != x; }
33 }
34
36 template<arithmetic T>
37 #else
38 template<class T, std::enable_if_t<std::is_arithmetic_v<T>, bool> = true>
39 #endif
40 _MSTD_INLINE17 _MSTD_CONSTEXPR20 T step(const T& edge, const T& x) noexcept {
41 return x < edge ? T(0) : T(1);
42 }
43
45 template<arithmetic T>
46 #else
47 template<class T, std::enable_if_t<std::is_arithmetic_v<T>, bool> = true>
48 #endif
49 _MSTD_INLINE17 _MSTD_CONSTEXPR20 T remap(const T& input, const T& currStart, const T& currEnd, const T& expectedStart,
50 const T& expectedEnd) noexcept {
52 }
53
55 template<arithmetic T>
56 #else
57 template<class T, std::enable_if_t<std::is_arithmetic_v<T>, bool> = true>
58 #endif
59 _MSTD_INLINE17 _MSTD_CONSTEXPR20 T deg_to_rad(const T& angle) noexcept {
60 _MSTD_CONSTEXPR17 const T half_circle = static_cast<T>(180);
61 return angle * (static_cast<T>(M_PI) / half_circle);
62 }
63
65 template<arithmetic T>
66 #else
67 template<class T, std::enable_if_t<std::is_arithmetic_v<T>, bool> = true>
68 #endif
69 _MSTD_INLINE17 _MSTD_CONSTEXPR20 T rad_to_deg(const T& rad) noexcept {
70 _MSTD_CONSTEXPR17 const T half_circle = static_cast<T>(180);
71 return rad * (half_circle / static_cast<T>(M_PI));
72 }
73
74 static _MSTD_CONSTEXPR17 const float default_epsilon = 1e-4f;
75
78 #else
79 template<class AT, class BT, class EpsT, std::enable_if_t<mstd::are_floating_points_v<AT, BT, EpsT>, bool> = true>
80 #endif
81 _MSTD_INLINE17 _MSTD_CONSTEXPR20 bool epsilon_equal(const AT& a, const BT& b,
82 const EpsT& epsilon = default_epsilon) noexcept {
83 return std::abs(a - b) < epsilon;
84 }
85
87 template<arithmetic AT, arithmetic BT, floating_point EpsT = double>
88 #else
89 template<class AT, class BT, class EpsT = double,
90 std::enable_if_t<mstd::are_arithmetic_v<AT, BT> && std::is_floating_point_v<EpsT>, bool> = true>
91 #endif
92 _MSTD_INLINE17 _MSTD_CONSTEXPR20 bool is_equal(const AT& a, const BT& b, const EpsT& eps = default_epsilon) {
94 return epsilon_equal(a, b, eps);
95 }
96 else { return a == b; }
97 }
98
100 template<arithmetic AT, arithmetic BT, floating_point EpsT = double>
101 #else
102 template<class AT, class BT, class EpsT = double,
103 std::enable_if_t<mstd::are_arithmetic_v<AT, BT> && std::is_floating_point_v<EpsT>, bool> = true>
104 #endif
105 _MSTD_INLINE17 _MSTD_CONSTEXPR20 bool is_not_equal(const AT& a, const BT& b, const EpsT& eps = default_epsilon) {
106 return !is_equal(a, b, eps);
107 }
108
110 template<arithmetic T>
111 #else
112 template<class T, std::enable_if_t<std::is_arithmetic_v<T>, bool> = true>
113 #endif
115 return std::clamp(a, T(0), T(1));
116 }
117
119 template<arithmetic T>
120 #else
121 template<class T, std::enable_if_t<std::is_arithmetic_v<T>, bool> = true>
122 #endif
124 if _MSTD_CONSTEXPR17 (std::is_floating_point_v<T>) { return x - std::floor(x); }
125 else { return 0; }
126 }
127
128 _MSTD_INLINE17 _MSTD_CONSTEXPR20 float q_rsqrt(float number) noexcept {
129 _MSTD_CONSTEXPR17 const uint32_t magi_number = 0x5F37'59DF;
130 _MSTD_CONSTEXPR17 const float one_half = 1.5f;
131 _MSTD_CONSTEXPR17 const float half = 0.5f;
133 auto y = std::bit_cast<float>(magi_number - (std::bit_cast<uint32_t>(number) >> 1));
134 #else
135 auto yi = magi_number - ((*reinterpret_cast<uint32_t*>(&number)) >> 1);
136 auto y = *reinterpret_cast<float*>(&yi);
137 #endif
138 return y * (one_half - (number * half * y * y));
139 }
140} // namespace mstd
141 #endif
142#endif
#define _MSTD_HAS_CXX17
Definition config.hpp:45
#define _MSTD_CONSTEXPR17
Definition config.hpp:76
#define _MSTD_HAS_CXX20
Definition config.hpp:52
#define _MSTD_INLINE17
Definition config.hpp:83
#define _MSTD_CONSTEXPR20
Definition config.hpp:84
Definition arithmetic_types.hpp:23
_MSTD_INLINE17 _MSTD_CONSTEXPR20 T deg_to_rad(const T &angle) noexcept
Definition math_functions.hpp:59
static _MSTD_CONSTEXPR17 const float default_epsilon
Definition math_functions.hpp:74
_MSTD_INLINE17 _MSTD_CONSTEXPR20 bool is_not_equal(const AT &a, const BT &b, const EpsT &eps=default_epsilon)
Definition math_functions.hpp:105
_MSTD_INLINE17 _MSTD_CONSTEXPR20 bool is_equal(const AT &a, const BT &b, const EpsT &eps=default_epsilon)
Definition math_functions.hpp:92
_MSTD_INLINE17 _MSTD_CONSTEXPR20 T fract(const T &x)
Definition math_functions.hpp:123
_MSTD_INLINE17 _MSTD_CONSTEXPR20 T signum(const T &x) noexcept
Definition math_functions.hpp:28
_MSTD_INLINE17 _MSTD_CONSTEXPR20 T rad_to_deg(const T &rad) noexcept
Definition math_functions.hpp:69
_MSTD_INLINE17 _MSTD_CONSTEXPR20 T saturate(const T &a) noexcept
Definition math_functions.hpp:114
_MSTD_INLINE17 _MSTD_CONSTEXPR20 T remap(const T &input, const T &currStart, const T &currEnd, const T &expectedStart, const T &expectedEnd) noexcept
Definition math_functions.hpp:49
_MSTD_INLINE17 _MSTD_CONSTEXPR20 bool epsilon_equal(const AT &a, const BT &b, const EpsT &epsilon=default_epsilon) noexcept
Definition math_functions.hpp:81
_MSTD_INLINE17 _MSTD_CONSTEXPR20 float q_rsqrt(float number) noexcept
Definition math_functions.hpp:128
_MSTD_INLINE17 _MSTD_CONSTEXPR20 T step(const T &edge, const T &x) noexcept
Definition math_functions.hpp:40