1#ifndef NEFORCE_CORE_NUMERIC_MATH_HPP__
2#define NEFORCE_CORE_NUMERIC_MATH_HPP__
14NEFORCE_BEGIN_NAMESPACE__
16NEFORCE_BEGIN_CONSTANTS__
39NEFORCE_INLINE17
constexpr decimal_t PI = 3.14159265358979323846L;
62 0, 1, 1, 2, 3, 5, 8, 13, 21,
63 34, 55, 89, 144, 233, 377, 610, 987, 1597,
64 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393,
65 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465,
66 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733,
67 1134903170, 1836311903, 2971215073, 4807526976, 7778742049};
73NEFORCE_END_CONSTANTS__
148NEFORCE_CONST_FUNCTION
constexpr int64_t safe_trunc(
const decimal_t x)
noexcept {
154 return static_cast<int64_t>(x);
166 if (n < constants::FIBONACCI_COUNT) {
167 return constants::FIBONACCI_LIST[n];
169 uint64_t a = constants::FIBONACCI_LIST[constants::FIBONACCI_COUNT - 2];
170 uint64_t b = constants::FIBONACCI_LIST[constants::FIBONACCI_COUNT - 1];
171 for (
uint32_t i = constants::FIBONACCI_COUNT; i <= n; ++i) {
199 return angular *
static_cast<T
>(constants::PI) /
static_cast<T
>(constants::SEMI_CIRCLE);
211 return radian * (
static_cast<T
>(constants::SEMI_CIRCLE) /
static_cast<T
>(constants::PI));
222 return x > T(0) ? x : -x;
246NEFORCE_CONST_FUNCTION
constexpr const T&
sum(
const T& x)
noexcept {
258template <
typename First,
typename... Rests,
enable_if_t<(
sizeof...(Rests) > 0),
int> = 0>
259NEFORCE_CONST_FUNCTION
constexpr decltype(
auto)
sum(First first, Rests... args) {
260 return first + _NEFORCE
sum(args...);
269template <
typename... Args,
enable_if_t<(
sizeof...(Args) > 0),
int> = 0>
270NEFORCE_CONST_FUNCTION
constexpr decltype(
auto)
average(Args... args) {
271 return _NEFORCE
sum(args...) /
sizeof...(Args);
281NEFORCE_CONSTEXPR14
int sign(
const T& value)
noexcept {
283 constexpr T zero = T(0);
301NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T
gcd(
const T& m,
const T& n)
noexcept {
303 constexpr T zero = T(0);
320NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T
lcm(
const T& m,
const T& n)
noexcept {
321 return (m / _NEFORCE
gcd(m, n)) * n;
338 NEFORCE_THROW_EXCEPTION(
math_exception(
"zero can not be dividend."));
357 NEFORCE_THROW_EXCEPTION(
math_exception(
"zero can not be dividend."));
372NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T
power(
const T& x,
uint32_t n)
noexcept {
395 return power(constants::EULER, n);
427 }
else if (m < 1.0L) {
434 const decimal_t a = (m - 1.0L) / (m + 1.0L);
446 constexpr decimal_t LN2 = 0.69314718055994530941723212145817656807L;
447 return 2.0L * s +
static_cast<decimal_t>(k) * LN2;
459 NEFORCE_THROW_EXCEPTION(
math_exception(
"zero can not be dividend."));
486NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14
decimal_t
518NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14
decimal_t
530 const bool negative = x < 0.0L;
533 decimal_t guess = v > 1.0L ? v / 3.0L : v;
540 return negative ? -guess : guess;
565 const int64_t i = safe_trunc(x);
567 return (x < 0.0L && x != di) ? di - 1.0L : di;
578 return floor(x * factor) / factor;
590 const int64_t i = safe_trunc(x);
592 return (x > 0.0L && x != di) ? di + 1.0L : di;
603 return ceil(x * factor) / factor;
619 return x < 0 ? -int_part : int_part;
622 return x < 0 ? -(int_part + 1) : (int_part + 1);
625 if (
static_cast<int64_t>(int_part) % 2 == 0) {
626 return x < 0 ? -int_part : int_part;
628 return x < 0 ? -(int_part + 1) : (int_part + 1);
641 return floor(x * factor) / factor;
652 return ceil(x * factor) / factor;
684 return round(x * factor) / factor;
695 return (x < 0 ?
ceil(x * factor, 0) :
floor(x * factor, 0)) / factor;
707 return static_cast<decimal_t>(safe_trunc(x));
719 const decimal_t toler = constants::DEFAULT_TOLERANCE) {
720 if (
absolute(axis) < constants::MACHINE_EPSILON) {
734 const decimal_t toler = constants::LOOSE_TOLERANCE) {
744NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14
bool
762 return x - y *
round(x / y);
771 return x -
static_cast<decimal_t>(safe_trunc(x));
783 *int_ptr = safe_trunc(x);
784 return x -
static_cast<decimal_t>(*int_ptr);
790NEFORCE_CONSTEXPR14
void reduce_arg_sincos(
decimal_t& x,
int& quadrant)
noexcept {
798 constexpr decimal_t HALF_PI_HI = 1.57079632679489661923L;
799 constexpr decimal_t HALF_PI_LO = 6.12323399573676603587e-17L;
800 constexpr decimal_t INV_HALF_PI = 0.63661977236758134308L;
804 quadrant = (quadrant + (k & 3)) & 3;
822 bool sin_done =
false, cos_done =
false;
824 while (!sin_done || !cos_done) {
826 term_sin = -term_sin * x2 / ((i + 1.0L) * (i + 2.0L));
833 term_cos = -term_cos * x2 / (i * (i + 1.0L));
862 inner::reduce_arg_sincos(x, quadrant);
865 inner::sincos_taylor(x, sin_x, cos_x);
892 inner::reduce_arg_sincos(x, quadrant);
894 inner::sincos_taylor(x, sin_x, cos_x);
920 inner::reduce_arg_sincos(x, quadrant);
922 inner::sincos_taylor(x, sin_x, cos_x);
946 return (s > 0) ? inf : -inf;
971 return (x > 0) ? constants::PI / 2 : -constants::PI / 2;
974 bool negative = x < 0.0L;
998 result = constants::PI / 2 - result;
1019 return constants::PI / 2;
1022 return -constants::PI / 2;
1043 return constants::PI;
1045 return constants::PI / 2 -
arcsine(x);
1050NEFORCE_END_NAMESPACE__
static NEFORCE_NODISCARD constexpr T quiet_nan() noexcept
获取安静nan表示
static NEFORCE_NODISCARD constexpr T epsilon() noexcept
获取机器精度
static NEFORCE_NODISCARD constexpr T min() noexcept
获取类型的最小值
static NEFORCE_NODISCARD constexpr T max() noexcept
获取类型的最大值
static NEFORCE_NODISCARD constexpr T infinity() noexcept
获取正无穷大表示
NEFORCE_INLINE17 constexpr size_t extent_v
extent的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_arithmetic_v
is_arithmetic的便捷变量模板
unsigned int uint32_t
32位无符号整数类型
long double decimal_t
扩展精度浮点数类型
long long int64_t
64位有符号整数类型
unsigned long long uint64_t
64位无符号整数类型
constexpr Iterator prev(Iterator iter, iter_difference_t< Iterator > n=-1)
获取迭代器的前一个位置
NEFORCE_INLINE17 constexpr decimal_t SEMI_CIRCLE
半圆角度 180° (角度制)
NEFORCE_INLINE17 constexpr decimal_t TWO_PI_HI
高精度 2π 高位
NEFORCE_INLINE17 constexpr decimal_t LOOSE_TOLERANCE
宽松容差
NEFORCE_INLINE17 constexpr decimal_t PI
圆周率 π (弧度制)
NEFORCE_INLINE17 constexpr decimal_t DEFAULT_TOLERANCE
默认容差
NEFORCE_INLINE17 constexpr decimal_t EULER
自然常数 e
NEFORCE_INLINE17 constexpr decimal_t CIRCLE
全圆角度 360° (角度制)
NEFORCE_INLINE17 constexpr uint64_t FIBONACCI_LIST[]
预计算的斐波那契数列
NEFORCE_INLINE17 constexpr uint32_t FIBONACCI_COUNT
斐波那契数列预计算数量
NEFORCE_INLINE17 constexpr decimal_t PHI
黄金分割比 φ
NEFORCE_INLINE17 constexpr decimal_t TWO_PI_LO
高精度 2π 低位
NEFORCE_INLINE17 constexpr decimal_t MACHINE_EPSILON
机器精度
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t sine(decimal_t x) noexcept
计算正弦值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t truncate(const decimal_t x, const int bit) noexcept
截断
NEFORCE_CONST_FUNCTION constexpr decimal_t float_part(const decimal_t x) noexcept
获取小数部分
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t floor(const decimal_t x) noexcept
向下取整
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t floor_bit(const decimal_t x, const uint32_t bit) noexcept
向下舍入到指定位数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 uint64_t leonardo(const uint32_t n) noexcept
计算莱昂纳多数
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t ceil_bit(const decimal_t x, const uint32_t bit) noexcept
向上舍入到指定位数
NEFORCE_CONST_FUNCTION constexpr enable_if_t< is_signed_v< T >, T > absolute(const T x) noexcept
取绝对值(有符号数版本)
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm_2(const decimal_t x)
计算以2为底的对数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t arctangent(decimal_t x) noexcept
计算反正切值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t square_root(const decimal_t x, const decimal_t precise=constants::DEFAULT_TOLERANCE) noexcept
计算平方根
NEFORCE_PURE_FUNCTION constexpr T radian2angular(const T radian) noexcept
弧度转角度
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t arcsine(const decimal_t x) noexcept
计算反正弦值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool around_zero(const decimal_t x, const decimal_t toler=constants::LOOSE_TOLERANCE) noexcept
判断是否接近零
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm_10(const decimal_t x)
计算以10为底的对数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 uint64_t fibonacci(const uint32_t n) noexcept
计算斐波那契数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t cotangent(const decimal_t x)
计算余切值
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm(const decimal_t x, const uint32_t base)
计算任意底数的对数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t cosine(decimal_t x) noexcept
计算余弦值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm_e(const decimal_t x) noexcept
计算自然对数
NEFORCE_CONST_FUNCTION constexpr const T & sum(const T &x) noexcept
单参数求和
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool around_pi(const decimal_t x, const decimal_t toler=constants::LOOSE_TOLERANCE)
判断是否接近π的整数倍
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t float_apart(decimal_t x, int64_t *int_ptr) noexcept
分离整数和小数部分
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T lcm(const T &m, const T &n) noexcept
计算最小公倍数
NEFORCE_CONST_FUNCTION constexpr decltype(auto) average(Args... args)
计算平均值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 uint64_t factorial(const uint32_t n) noexcept
计算阶乘
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T power(const T &x, uint32_t n) noexcept
幂运算
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t ceil(const decimal_t x) noexcept
向上取整
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool around_multiple(const decimal_t x, const decimal_t axis, const decimal_t toler=constants::DEFAULT_TOLERANCE)
判断是否接近某个倍数值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t round(const decimal_t x) noexcept
四舍五入
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T gcd(const T &m, const T &n) noexcept
计算最大公约数
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t remainder(const decimal_t x, const decimal_t y) noexcept
计算余数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t arccosine(const decimal_t x) noexcept
计算反余弦值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 enable_if_t< is_floating_point_v< T >, T > mod(const T x, const T y)
浮点数取模运算
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t tangent(decimal_t x) noexcept
计算正切值
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t exponential(const uint32_t n) noexcept
计算e的n次幂
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t cube_root(const decimal_t x, const decimal_t precise=constants::DEFAULT_TOLERANCE) noexcept
计算立方根
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t round_bit(const decimal_t x, const uint32_t bit) noexcept
四舍五入到指定位数
NEFORCE_PURE_FUNCTION constexpr T angular2radian(const T angular) noexcept
角度转弧度
NEFORCE_CONSTEXPR14 int sign(const T &value) noexcept
获取数值的符号
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t truncate_bit(const decimal_t x, const uint32_t bit) noexcept
截断到指定位数
NEFORCE_CONST_FUNCTION constexpr bool is_nan(const T x) noexcept
检查浮点数是否为NaN
NEFORCE_CONST_FUNCTION constexpr bool is_infinity(const T x) noexcept
检查浮点数是否为无穷大
NEFORCE_CONST_FUNCTION constexpr bool is_finite(const T x) noexcept
检查浮点数是否为有限值
typename make_integer< Size, IsSigned >::type make_integer_t
make_integer的便捷别名
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名