math.hpp
Useful mathematical functions (that are constexpr when possible.)
Note
Status: BETA (though new functions are added frequently)
Functions
-
inline constexpr int Mod(int in_val, int mod_val)
% is actually remainder; Mod is a proper modulus command that handles negative #’s correctly
-
inline double Mod(double in_val, double mod_val)
Regular Mod doesn’t work on doubles. Build one that does!
-
inline int FloorDivide(int dividend, int divisor)
Divide one integer by another, rounding towards minus infinity.
-
inline int RoundedDivide(int dividend, int divisor)
Default integer division is truncated, not rounded. Round the division result instead of truncating it. Rounding ties (i.e., result % divisor == 0.5) are rounded up.
-
inline size_t RoundedDivide(size_t dividend, size_t divisor)
Default integer division is truncated, not rounded. Round the division result instead of truncating it. Rounding ties (i.e., result % divisor == 0.5) will be rounded up.
-
inline int UnbiasedDivide(int dividend, int divisor, Random &r)
Regular integer division is truncated, not rounded. Round the division result instead of truncating it. Rounding ties (i.e., result % divisor == 0.5) are broken by coin toss.
-
inline size_t UnbiasedDivide(size_t dividend, size_t divisor, Random &r)
Regular integer division is truncated, not rounded. Round the division result instead of truncating it. Rounding ties (i.e., result % divisor == 0.5) are broken by coin toss.
-
template<typename TYPE>
constexpr TYPE ToRange(const TYPE &value, const TYPE &in_min, const TYPE &in_max) Run both min and max on a value to put it into a desired range.
-
template<typename T, typename ...Ts>
constexpr T Min(T in1, T in2, Ts... extras) Min of multiple elements is solved recursively.
-
template<typename T, typename ...Ts>
constexpr T Max(T in1, T in2, Ts... extras) Max of multiple elements is solved recursively.
-
template<typename T>
constexpr const T &MinRef(const T &in1) MinRef works like Min, but never copies any inputs; always treats as references. MinRef of only one element returns reference to that element itself!
-
template<typename T, typename ...Ts>
constexpr const T &MinRef(const T &in1, const T &in2, const Ts&... extras) MinRef of multiple elements returns reference to minimum value.
-
template<typename T>
constexpr const T &MaxRef(const T &in1) MaxRef works like Max, but never copies any inputs; always treats as references. MaxRef of only one element returns reference to that element itself!
-
template<typename T, typename ...Ts>
constexpr const T &MaxRef(const T &in1, const T &in2, const Ts&... extras) MaxRef of multiple elements returns reference to maximum value.
-
static constexpr double Log2(double x)
Compile-time log base 2 calculator.
-
static constexpr double Log(double x, double base = 10.0)
Compile-time log calculator.
-
static constexpr double Ln(double x)
Compile-time natural log calculator.
-
static constexpr double Log10(double x)
Compile-time log base 10 calculator.
-
static constexpr double Pow2(double exp)
A fast 2^x command.
-
template<typename TYPE>
static constexpr TYPE IntPow(TYPE base, TYPE exp) A fast method for calculating exponents for int types.
-
template<typename T>
static constexpr T Pow(T base, typename internal::identity<T>::type exp) A fast method for calculating exponents on doubles or integral types. Uses if constexpr to work around compiler bug in Emscripten (issue #296).
-
static constexpr double Exp(double exp)
A fast method of calculating e^x.
-
template<typename TYPE>
static constexpr int IntLog2(TYPE x) A compile-time int-log calculator (aka, significant bits)
-
template<typename T>
constexpr const T &Min(const T &in1, const T &in2, const T &in3) Return the minimum of three values.
-
template<typename T>
const T &Min(std::initializer_list<const T&> lst) A version of Min that allows a variable number of inputs to be compared.
-
template<typename T>
const T &Max(std::initializer_list<const T&> lst) A version of Max that allows a variable number of inputs to be compared.
-
inline uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A. Returns zero on overflow.
-
inline constexpr bool IsPowerOf2(const size_t x)
Tests if a number is a power of two.
-
inline constexpr int Factorial(int i)
-
inline bool Toggle(bool &in_bool)
Toggle an input bool.
-
inline constexpr bool AllTrue()
Combine bools to AND them all together.
-
inline constexpr bool AnyTrue()
Combine bools to OR them all together.