MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
Mathematics Math utilities, often with constexpr support

Classes

struct  mtcore::math::GCD< A, B >
 Template to get GCD of numbers. More...
 
struct  mtcore::math::LCM< A, B >
 Template to get LCM of numbers. More...
 
struct  mtcore::math::MultRatios< T1, T2 >
 Template to multiply two std::ratio numbers. More...
 
struct  mtcore::math::AddRatios< T1, T2 >
 Template to add two std::ratio numbers. More...
 
struct  mtcore::math::RatioParts< T >
 Template to get the Numerator and Denominator of a std::ratio. More...
 
struct  mtcore::math::InvertRatio< T >
 Template to invert a ratio number. More...
 
struct  mtcore::math::NegateRatio< T >
 Template to negate the power of a ratio. More...
 
struct  mtcore::math::IntPowRatioImpl< T, Pow, Odd, Neg, Z >
 Template to get the integer power (positive or negative) of a ratio. More...
 

Functions

template<typename T, typename R = double>
constexpr R mtcore::math::floor_constexpr (T num) noexcept
 Constexpr floor function.
 
template<typename T, typename R = double>
constexpr R mtcore::math::ceil_constexpr (T num) noexcept
 Constexpr ceil function.
 
template<typename T, typename R = T>
constexpr R mtcore::math::floor (T num) noexcept
 Floors a number with support for constexpr and fast runtime compilation.
 
template<typename T, typename R = T>
constexpr R mtcore::math::ceil (T v) noexcept
 Ceils a number with support for constexpr and fast runtime compilation.
 
template<typename L, typename R, typename Res = L>
constexpr Res mtcore::math::mod (L left, R right) noexcept
 Calculates the mathematical mod of two numbers.
 
template<typename T>
constexpr auto mtcore::math::mod_range (T x, i64 a, i64 b) noexcept -> T
 x mod [a..b)
 
template<typename L, typename R, typename Res = L>
constexpr auto mtcore::math::amod (L x, R y) noexcept -> Res
 x mod [1..y]
 
template<typename T, typename R = T>
constexpr auto mtcore::math::approx_eq (T x, R y, T epsilon=T{0.000001}) noexcept -> bool
 Checks if two numbers are approximately equal (within epsilon distance)
 
template<typename T>
constexpr int mtcore::math::sign (const T &v)
 Gets the sign (1 for positive, -1 for negative, 0 for zero)
 
template<typename L>
constexpr L mtcore::math::abs (L l)
 Gets absolute value of a number.
 
template<typename T>
constexpr T mtcore::math::int_pow (T val, int exp)
 Raises to an integer power (positive or negative)
 
template<std::integral T>
constexpr T mtcore::math::gcd (T a, T b)
 Calculates the GCD (Greatest Common Divisor) of two integer numbers.
 
template<std::integral T>
constexpr T mtcore::math::lcm (T a, T b)
 Calculates the LCM (Least Common Multiple) of two integer numbers.
 

Detailed Description

Function Documentation

◆ abs()

template<typename L>
L mtcore::math::abs ( L l)
constexpr

Gets absolute value of a number.

Template Parameters
LType to get absolute value of
Parameters
lValue to get absolute of

Definition at line 188 of file core/mtcore/math/core.hpp.

188 {
189 return l * static_cast<L>(sign(l));
190 }
constexpr int sign(const T &v)
Gets the sign (1 for positive, -1 for negative, 0 for zero)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ amod()

template<typename L, typename R, typename Res = L>
auto mtcore::math::amod ( L x,
R y ) -> Res
constexprnoexcept

x mod [1..y]

Definition at line 151 of file core/mtcore/math/core.hpp.

151 {
152 return static_cast<Res>(y + mod(x, -y));
153 }
constexpr Res mod(L left, R right) noexcept
Calculates the mathematical mod of two numbers.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ approx_eq()

template<typename T, typename R = T>
auto mtcore::math::approx_eq ( T x,
R y,
T epsilon = T{0.000001} ) -> bool
constexprnoexcept

Checks if two numbers are approximately equal (within epsilon distance)

Definition at line 161 of file core/mtcore/math/core.hpp.

161 {0.000001}) noexcept -> bool {
162 return (x <= y + epsilon) & (x >= y - epsilon);
163 }

◆ ceil()

template<typename T, typename R = T>
R mtcore::math::ceil ( T v)
constexprnoexcept

Ceils a number with support for constexpr and fast runtime compilation.

Also accepts ints, though ints are a noop.

Template Parameters
TType to floor
RType to cast result to (default is no cast)
Parameters
vNumber to floor

Definition at line 103 of file core/mtcore/math/core.hpp.

103 {
104 if constexpr (std::is_integral_v<T>) {
105 return static_cast<R>(v);
106 }
107 else if (std::is_constant_evaluated()) {
108 return ceil_constexpr<T, R>(v);
109 }
110 else {
111 return static_cast<R>(std::ceil(v));
112 }
113 }
constexpr R ceil_constexpr(T num) noexcept
Constexpr ceil function.
Here is the call graph for this function:

◆ ceil_constexpr()

template<typename T, typename R = double>
R mtcore::math::ceil_constexpr ( T num)
constexprnoexcept

Constexpr ceil function.

Prefer math::ceil for better runtime support.

See also
mtcore::math::ceil
Template Parameters
T
R
Parameters
num

Definition at line 62 of file core/mtcore/math/core.hpp.

62 {
63 // Check to see if we're at our maximum supported radix range
64 // If we are, then the number we received is an integer since all the significant digits
65 // have to be positive at this point
66 const T max = static_cast<T>(static_cast<uint64_t>(1) << static_cast<uint64_t>(std::numeric_limits<T>::digits - 1));
67 if (num >= max || num <= -max) {
68 return num;
69 }
70 const auto a = static_cast<T>(static_cast<int64_t>(num));
71 return (num > 0 && num != a) ? static_cast<R>(a + 1) : static_cast<R>(a);
72 }
Here is the caller graph for this function:

◆ floor()

template<typename T, typename R = T>
R mtcore::math::floor ( T num)
constexprnoexcept

Floors a number with support for constexpr and fast runtime compilation.

Also accepts ints, though ints are a noop.

Template Parameters
TType to floor
RType to cast result to (default is no cast)
Parameters
numNumber to floor

Definition at line 82 of file core/mtcore/math/core.hpp.

82 {
83 if constexpr (std::is_integral<T>::value) {
84 return static_cast<R>(num);
85 }
86 else if (std::is_constant_evaluated()) {
87 return floor_constexpr<T, R>(num);
88 }
89 else {
90 return static_cast<R>(std::floor(num));
91 }
92 }
constexpr R floor_constexpr(T num) noexcept
Constexpr floor function.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ floor_constexpr()

template<typename T, typename R = double>
R mtcore::math::floor_constexpr ( T num)
constexprnoexcept

Constexpr floor function.

Prefer math::floor for better runtime support.

See also
mtcore::math::floor
Template Parameters
T
R
Parameters
num

Definition at line 41 of file core/mtcore/math/core.hpp.

41 {
42 // Check to see if we're at our maximum supported radix range
43 // If we are, then the number we received is an integer since all the significant digits
44 // have to be positive at this point
45 const T max = static_cast<T>(static_cast<uint64_t>(1) << static_cast<uint64_t>(std::numeric_limits<T>::digits - 1));
46 if (num >= max || num <= -max) {
47 return static_cast<R>(num);
48 }
49 const auto a = static_cast<T>(static_cast<int64_t>(num));
50 return (num < 0 && num != a) ? static_cast<R>(a - 1) : static_cast<R>(a);
51 }
Here is the caller graph for this function:

◆ gcd()

template<std::integral T>
T mtcore::math::gcd ( T a,
T b )
constexpr

Calculates the GCD (Greatest Common Divisor) of two integer numbers.

Template Parameters
TType of values to operate on
Parameters
avalue 1
bvalue 2

Definition at line 223 of file core/mtcore/math/core.hpp.

223 {
224 if (b == 0) {
225 return a;
226 }
227 return gcd(b, mod(a, b));
228 }
constexpr T gcd(T a, T b)
Calculates the GCD (Greatest Common Divisor) of two integer numbers.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ int_pow()

template<typename T>
T mtcore::math::int_pow ( T val,
int exp )
constexpr

Raises to an integer power (positive or negative)

Template Parameters
TType of value to exponentiate
Parameters
valValue to exponentiate
expExponent to raise to

Definition at line 200 of file core/mtcore/math/core.hpp.

200 {
201 if (exp < 0) {
202 return int_pow(1 / val, -exp);
203 }
204 else if (exp == 0) {
205 return 1;
206 }
207 else if ((exp & 1) == 0) {
208 return int_pow(val * val, exp / 2);
209 }
210 else {
211 return val * int_pow(val * val, (exp - 1) / 2);
212 }
213 }
ValIter< T > val(const T &r)
Generic value iterator that uses the operator[] and incrementing indexes to iterate over a collection...
Definition iter.hpp:114
constexpr T int_pow(T val, int exp)
Raises to an integer power (positive or negative)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lcm()

template<std::integral T>
T mtcore::math::lcm ( T a,
T b )
constexpr

Calculates the LCM (Least Common Multiple) of two integer numbers.

Template Parameters
TType of values to operate on
Parameters
avalue 1
bvalue 2

Definition at line 238 of file core/mtcore/math/core.hpp.

238 {
239 return abs(a) * (abs(b) / gcd(a, b));
240 }
constexpr L abs(L l)
Gets absolute value of a number.
Here is the call graph for this function:

◆ mod()

template<typename L, typename R, typename Res = L>
Res mtcore::math::mod ( L left,
R right )
constexprnoexcept

Calculates the mathematical mod of two numbers.

Has different negative number handling than % which allows it to be better for indexes. Also, it's more consistent across platforms and handles floating point numbers as well.

Template Parameters
LType of left operand
RType of right operand
ResType of result
Parameters
leftLeft operand
rightRight operand
Returns
Mod

Definition at line 128 of file core/mtcore/math/core.hpp.

128 {
129 if (std::is_integral_v<L> || std::is_integral_v<R>) {
130 return static_cast<Res>(mod(static_cast<long double>(left), static_cast<long double>(right)));
131 }
132 else {
133 return static_cast<Res>(left - right * math::floor(left / right));
134 }
135 }
constexpr R floor(T num) noexcept
Floors a number with support for constexpr and fast runtime compilation.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mod_range()

template<typename T>
auto mtcore::math::mod_range ( T x,
i64 a,
i64 b ) -> T
constexprnoexcept

x mod [a..b)

Definition at line 142 of file core/mtcore/math/core.hpp.

142 {
143 return a == b ? x : a + mod(x - a, b - a);
144 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sign()

template<typename T>
int mtcore::math::sign ( const T & v)
constexpr

Gets the sign (1 for positive, -1 for negative, 0 for zero)

Definition at line 171 of file core/mtcore/math/core.hpp.

171 {
172 if (v < 0) {
173 return -1;
174 }
175 if (v > 0) {
176 return 1;
177 }
178 return 0;
179 }
Here is the caller graph for this function: