14 constexpr T
abs(T value)
16 return value < 0 ? -value : value;
19 template<std::
integral T>
22 return (val & 1) == 1;
25 template<std::
integral T>
28 return (val & 1) == 0;
31 constexpr
int gcd(
int nFirst,
int nSecond)
33 if (nFirst == 0 || nSecond == 0)
38 const int nRemainder = nFirst % nSecond;
46 constexpr
int lcm(
int nFirst,
int nSecond)
48 if (nFirst == 0 || nSecond == 0)
52 nSecond =
abs(nSecond);
54 return nFirst /
gcd(nFirst, nSecond) * nSecond;
58 constexpr
double pow(T number,
int nPower)
60 static_assert(std::is_integral_v<T> || std::is_floating_point_v<T>,
"Integral or floating point required");
62 if (!std::is_constant_evaluated())
64 const bool bNegativePower = nPower < 0;
65 const size_t nPositivePower =
static_cast<size_t>(std::abs(nPower));
68 switch (nPositivePower)
74 fResult =
static_cast<double>(number);
78 fResult =
static_cast<double>(number * number);
82 const std::bitset<std::numeric_limits<int>::digits> powerBitSet(nPositivePower);
84 std::array<double, std::numeric_limits<int>::digits> powers;
86 powers[0] =
static_cast<double>(number);
91 while (nCurPower < nPositivePower)
93 powers[nCurIndex] = powers[nCurIndex - 1] * powers[nCurIndex - 1];
98 for (
size_t i = 0; i < nCurIndex; ++i)
99 if (powerBitSet.test(i))
100 fResult *= powers[i];
105 return bNegativePower ? 1.0 / fResult : fResult;
115 return 1.0 /
pow(number, -nPower);
119 double fResult = 1.0;
120 for (
int i = 0; i < nPower; ++i)
121 fResult *=
static_cast<double>(number);
128 template<std::
integral I>
134 std::bitset<std::numeric_limits<I>::digits> powers(
static_cast<size_t>(
abs(nValue)));
136 I nPow =
static_cast<I
>(std::numeric_limits<I>::digits - 1);
137 while (!powers.test(
static_cast<size_t>(nPow)))
constexpr int lcm(int nFirst, int nSecond)
Least common multiple.
I maxpot(I nValue)
Max power of two in integer.
constexpr bool is_odd(T val)
Check if value is odd.
constexpr T abs(T value)
Constexpr absolute value.
constexpr bool is_even(T val)
Check if value is even.
constexpr double pow(T number, int nPower)
Power function for integer power.
constexpr int gcd(int nFirst, int nSecond)
Greatest common divisor.