1#ifndef MSTL_CORE_NUMERIC_MATH_HPP__
2#define MSTL_CORE_NUMERIC_MATH_HPP__
44 55, 89, 144, 233, 377,
45 610, 987, 1597, 2584, 4181,
46 6765, 10946, 17711, 28657, 46368,
47 75025, 121393, 196418, 317811, 514229,
48 832040, 1346269, 2178309, 3524578, 5702887,
49 9227465, 14930352, 24157817, 39088169, 63245986,
50 102334155, 165580141, 267914296, 433494437, 701408733,
51 1134903170, 1836311903, 2971215073, 4807526976, 7778742049
102 static_assert(is_arithmetic_v<T>,
"arithmetic required");
114 static_assert(is_arithmetic_v<T>,
"arithmetic required");
127 return x > T(0) ? x : -x;
152MSTL_CONST_FUNCTION
constexpr const T&
sum(
const T& x)
noexcept {
164template <
typename First,
typename... Rests,
enable_if_t<(
sizeof...(Rests) > 0),
int> = 0>
165MSTL_CONST_FUNCTION
constexpr decltype(
auto)
sum(First first, Rests... args) {
175template <
typename... Args,
enable_if_t<(
sizeof...(Args) > 0),
int> = 0>
176MSTL_CONST_FUNCTION
constexpr decltype(
auto)
average(Args... args) {
177 return _MSTL sum(args...) /
sizeof...(Args);
187MSTL_CONSTEXPR14
int sign(
const T& value)
noexcept {
188 static_assert(is_arithmetic_v<T>,
"arithmetic required");
189 constexpr T zero = T(0);
190 if (value > zero)
return 1;
191 if (value < zero)
return -1;
203MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 T
gcd(
const T& m,
const T& n)
noexcept {
205 constexpr T zero = T(0);
222MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 T
lcm(
const T& m,
const T& n)
noexcept {
236MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 T
float_mod(
const T x,
const T y) {
237 static_assert(is_arithmetic_v<T>,
"arithmetic required");
238 if (y == 0) throw_exception(
math_exception(
"zero can not be dividend."));
239 const T result = x -
static_cast<make_integer_t<sizeof(T)
>>(x / y) * y;
253MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 T
power(
const T& x,
uint32_t n)
noexcept {
254 static_assert(is_arithmetic_v<T>,
"arithmetic required");
255 if (n == 0)
return 1;
286MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 T
logarithm_e(
const T x)
noexcept {
287 static_assert(is_floating_point_v<T>,
"floating point required");
289 const T a = (x - 1) / (x + 1);
290 const T a_sqrt = a * a;
295 y = 1.0 / nk + a_sqrt * y;
346MSTL_PURE_FUNCTION MSTL_CONSTEXPR14
decimal_t
350 while (
absolute(result - t) > precise) {
352 result = 0.5 * (t + x / t);
365MSTL_PURE_FUNCTION MSTL_CONSTEXPR14
decimal_t
369 while (
absolute(result - t) > precise) {
371 result = (2 * t + x / (t * t)) / 3;
398 const auto int_part = x * times;
399 if (x < 0 && x * times * 10 / 10.0 != int_part)
400 return (int_part - 1) / times;
401 return int_part / times;
412 const auto int_part = x * times;
413 if (x > 0 && x * times * 10 / 10.0 != int_part)
414 return (int_part + 1) / times;
415 return int_part / times;
447 return (x >= 0) ?
static_cast<int64_t>(x) :
static_cast<int64_t>(x - 1);
458 return floor(x * factor) / factor;
467 return (x >= 0) ?
static_cast<int64_t>(x + 1) :
static_cast<int64_t>(x);
478 return ceil(x * factor) / factor;
487 return (x >= 0) ?
floor(x + 0.5) :
ceil(x - 0.5);
498 return round(x * factor) / factor;
508 return x < 0 ?
ceil(x, bit) :
floor(x, bit);
579 return x -
static_cast<int64_t>(x);
591 *int_ptr =
static_cast<int64_t>(x);
611 x = x - twoPi *
floor(x / twoPi);
628 fac = fac * (i + 1) * (i + 2);
631 sum = idx / fac * neg;
635 const auto fin =
sign * taylor;
685MSTL_PURE_FUNCTION MSTL_CONSTEXPR14
decimal_t __arctangent_taylor(
const decimal_t x)
noexcept {
713 return _INNER __arctangent_taylor(x);
unsigned int uint32_t
32位无符号整数类型
long double decimal_t
扩展精度浮点数类型
long long int64_t
64位有符号整数类型
double float64_t
64位双精度浮点数类型
unsigned long long uint64_t
64位无符号整数类型
MSTL_INLINE17 constexpr decimal_t SEMI_CIRCLE
半圆角度 180°(角度制)
MSTL_INLINE17 constexpr decimal_t PI
圆周率 π(弧度制)
MSTL_INLINE17 constexpr decimal_t PHI
黄金分割比 φ
MSTL_INLINE17 constexpr uint32_t FIBONACCI_COUNT
斐波那契数列预计算数量
MSTL_INLINE17 constexpr decimal_t EULER
自然常数 e
MSTL_INLINE17 constexpr uint32_t TAYLOR_CONVERGENCE
泰勒展开收敛项数
MSTL_INLINE17 constexpr decimal_t PRECISE_TOLERANCE
精确容差
MSTL_INLINE17 constexpr decimal_t CIRCLE
全圆角度 360°(角度制)
MSTL_INLINE17 constexpr uint64_t FIBONACCI_LIST[]
预计算的斐波那契数列
MSTL_INLINE17 constexpr decimal_t EPSILON
浮点数精度容差
MSTL_INLINE17 constexpr decimal_t LOW_PRECISE_TOLERANCE
低精度容差
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool around_multiple(const decimal_t x, const decimal_t axis, const decimal_t toler=_CONSTANTS PRECISE_TOLERANCE)
判断是否接近某个倍数值
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 decimal_t cotangent(const decimal_t x)
计算余切值
MSTL_PURE_FUNCTION constexpr T angular2radian(const T angular) noexcept
角度转弧度
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool around_zero(const decimal_t x, const decimal_t toler=_CONSTANTS PRECISE_TOLERANCE)
判断是否接近零
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 decimal_t arcsine(const decimal_t x)
计算反正弦值
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 decimal_t round_bit(const decimal_t x, const uint32_t bit) noexcept
四舍五入到指定位数
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 decimal_t square_root(const decimal_t x, const decimal_t precise=_CONSTANTS PRECISE_TOLERANCE) noexcept
计算平方根
MSTL_CONST_FUNCTION constexpr const T & sum(const T &x) noexcept
单参数求和
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 uint64_t leonardo(const uint32_t n)
计算莱昂纳多数
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 decimal_t truncate_bit(const decimal_t x, const uint32_t bit) noexcept
截断到指定位数
MSTL_CONST_FUNCTION constexpr decimal_t float_part(const decimal_t x) noexcept
获取小数部分
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 uint64_t factorial(const uint32_t n) noexcept
计算阶乘
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 decimal_t float_apart(decimal_t x, int64_t *int_ptr) noexcept
分离整数和小数部分
MSTL_CONSTEXPR14 int sign(const T &value) noexcept
获取数值的符号
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 decimal_t exponential(const uint32_t n) noexcept
计算e的n次幂
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 T float_mod(const T x, const T y)
浮点数取模运算
MSTL_PURE_FUNCTION constexpr T radian2angular(const T radian) noexcept
弧度转角度
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 T logarithm_e(const T x) noexcept
计算自然对数
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 decimal_t tangent(const decimal_t x)
计算正切值
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 decimal_t arctangent(const decimal_t x) noexcept
计算反正切值
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 uint64_t fibonacci(const uint32_t n)
计算斐波那契数
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 decimal_t ceil_bit(const decimal_t x, const uint32_t bit) noexcept
向上舍入到指定位数
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 decimal_t floor(const decimal_t x) noexcept
向下取整
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 decimal_t truncate(const decimal_t x, const int bit) noexcept
截断
MSTL_CONST_FUNCTION constexpr decltype(auto) average(Args... args)
计算平均值
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 T logarithm(const T x, const uint32_t base)
计算任意底数的对数
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 decimal_t cube_root(const decimal_t x, const decimal_t precise=_CONSTANTS PRECISE_TOLERANCE) noexcept
计算立方根
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 decimal_t sine(decimal_t x) noexcept
计算正弦值
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool around_pi(const decimal_t x, const decimal_t toler=_CONSTANTS LOW_PRECISE_TOLERANCE)
判断是否接近π的整数倍
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 T lcm(const T &m, const T &n) noexcept
计算最小公倍数
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 decimal_t cosine(const decimal_t x) noexcept
计算余弦值
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 decimal_t remainder(const decimal_t x, const decimal_t y) noexcept
计算余数
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 decimal_t round(const decimal_t x) noexcept
四舍五入
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 decimal_t ceil(const decimal_t x) noexcept
向上取整
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 T gcd(const T &m, const T &n) noexcept
计算最大公约数
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 decimal_t floor_bit(const decimal_t x, const uint32_t bit) noexcept
向下舍入到指定位数
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 T power(const T &x, uint32_t n) noexcept
幂运算
MSTL_CONST_FUNCTION constexpr enable_if_t< is_signed_v< T >, T > absolute(const T x) noexcept
取绝对值(有符号数版本)
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 decimal_t arccosine(const decimal_t x)
计算反余弦值
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 T logarithm_2(const T x)
计算以2为底的对数
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 T logarithm_10(const T x)
计算以10为底的对数
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_CONSTANTS__
结束constants命名空间
#define MSTL_END_INNER__
结束inner命名空间
#define _CONSTANTS
constants命名空间前缀
#define _INNER
inner命名空间前缀
#define MSTL_BEGIN_CONSTANTS__
开始constants命名空间
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
typename make_integer< Size, IsSigned >::type make_integer_t
make_integer的便捷别名
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名