|
NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
|
基本数学运算函数 更多...
函数 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 uint64_t | fibonacci (const uint32_t n) noexcept |
| 计算斐波那契数 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 uint64_t | leonardo (const uint32_t n) noexcept |
| 计算莱昂纳多数 | |
| template<typename T> | |
| NEFORCE_PURE_FUNCTION constexpr T | angular2radian (const T angular) noexcept |
| 角度转弧度 | |
| template<typename T> | |
| NEFORCE_PURE_FUNCTION constexpr T | radian2angular (const T radian) noexcept |
| 弧度转角度 | |
| template<typename T> | |
| NEFORCE_CONST_FUNCTION constexpr enable_if_t< is_signed_v< T >, T > | absolute (const T x) noexcept |
| 取绝对值(有符号数版本) | |
| template<typename T> | |
| NEFORCE_CONST_FUNCTION constexpr const T & | sum (const T &x) noexcept |
| 单参数求和 | |
| template<typename First, typename... Rests, enable_if_t<(sizeof...(Rests) > 0), int > = 0> | |
| NEFORCE_CONST_FUNCTION constexpr decltype(auto) | sum (First first, Rests... args) |
| 多参数求和 | |
| template<typename... Args, enable_if_t<(sizeof...(Args) > 0), int > = 0> | |
| NEFORCE_CONST_FUNCTION constexpr decltype(auto) | average (Args... args) |
| 计算平均值 | |
| template<typename T> | |
| NEFORCE_CONSTEXPR14 int | sign (const T &value) noexcept |
| 获取数值的符号 | |
| template<typename T> | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T | gcd (const T &m, const T &n) noexcept |
| 计算最大公约数 | |
| template<typename T> | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T | lcm (const T &m, const T &n) noexcept |
| 计算最小公倍数 | |
| template<typename T> | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 enable_if_t< is_floating_point_v< T >, T > | mod (const T x, const T y) |
| 浮点数取模运算 | |
| template<typename T> | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T | power (const T &x, uint32_t n) noexcept |
| 幂运算 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | exponential (const uint32_t n) noexcept |
| 计算e的n次幂 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | logarithm_e (const decimal_t x) noexcept |
| 计算自然对数 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | logarithm (const decimal_t x, const uint32_t base) |
| 计算任意底数的对数 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | logarithm_2 (const decimal_t x) |
| 计算以2为底的对数 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | logarithm_10 (const decimal_t x) |
| 计算以10为底的对数 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | square_root (const decimal_t x, const decimal_t precise=constants::DEFAULT_TOLERANCE) noexcept |
| 计算平方根 | |
| 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 uint64_t | factorial (const uint32_t n) noexcept |
| 计算阶乘 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | floor (const decimal_t x) noexcept |
| 向下取整 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | floor (const decimal_t x, const uint32_t bit) noexcept |
| 向下取整到指定位数 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | ceil (const decimal_t x) noexcept |
| 向上取整 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | ceil (const decimal_t x, const uint32_t bit) noexcept |
| 向上取整到指定位数 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | round (const decimal_t x) noexcept |
| 四舍五入 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | floor_bit (const decimal_t x, const uint32_t bit) noexcept |
| 向下舍入到指定位数 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | ceil_bit (const decimal_t x, const uint32_t bit) noexcept |
| 向上舍入到指定位数 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | round_bit (const decimal_t x, const uint32_t bit) noexcept |
| 四舍五入到指定位数 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | truncate_bit (const decimal_t x, const uint32_t bit) noexcept |
| 截断到指定位数 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | round (const decimal_t x, const uint32_t bit) noexcept |
| 四舍五入到指定位数 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | truncate (const decimal_t x, const int bit) noexcept |
| 截断 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | truncate (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 bool | around_pi (const decimal_t x, const decimal_t toler=constants::LOOSE_TOLERANCE) |
| 判断是否接近π的整数倍 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool | around_zero (const decimal_t x, const decimal_t toler=constants::LOOSE_TOLERANCE) noexcept |
| 判断是否接近零 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | remainder (const decimal_t x, const decimal_t y) noexcept |
| 计算余数 | |
| NEFORCE_CONST_FUNCTION constexpr decimal_t | float_part (const decimal_t x) noexcept |
| 获取小数部分 | |
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | float_apart (decimal_t x, int64_t *int_ptr) noexcept |
| 分离整数和小数部分 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | sine (decimal_t x) noexcept |
| 计算正弦值 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | cosine (decimal_t x) noexcept |
| 计算余弦值 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | tangent (decimal_t x) noexcept |
| 计算正切值 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | cotangent (const decimal_t x) |
| 计算余切值 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | arctangent (decimal_t x) noexcept |
| 计算反正切值 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | arcsine (const decimal_t x) noexcept |
| 计算反正弦值 | |
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t | arccosine (const decimal_t x) noexcept |
| 计算反余弦值 | |
基本数学运算函数
本实现严格遵循以下数学计算相关标准规范:
浮点数算术标准:
数学函数规范:
本实现采用泰勒级数展开与参数归约相结合的方法:
**参数归约**(根据 Cody & Waite 方法):
**泰勒级数展开**:
迭代终止条件:当前项小于 MACHINE_EPSILON × 当前和。
**自然对数 ln(x)**:
**任意底对数**:
**平方根 √x**(牛顿迭代法):
**立方根 ³√x**(牛顿迭代法):
根据 IEEE 754-2019 §6,特殊值处理规则:
| 输入情况 | 返回值 |
|---|---|
| 输入为 NaN | 返回 NaN |
| 输入为 ±∞ | 根据函数语义返回 ±∞ 或 NaN |
| 负数的平方根 | 返回 NaN |
| 除数为零 | 返回 NaN 或抛出异常 |
| |x| > 1 的反正弦/反余弦| 返回 NaN |
|
constexprnoexcept |
取绝对值(有符号数版本)
| T | 数值类型 |
| x | 原数值 |
被这些函数引用 arccosine(), arcsine(), arctangent(), around_multiple(), around_zero(), cube_root(), gcd(), logarithm_e(), round(), square_root() , 以及 truncate().
|
constexprnoexcept |
计算反余弦值
| x | 参数(|x| ≤ 1) |
引用了 absolute(), arcsine(), is_nan() , 以及 numeric_traits< T, Dummy >::quiet_nan().
计算反正弦值
| x | 参数(|x| ≤ 1) |
引用了 absolute(), arctangent(), is_nan(), numeric_traits< T, Dummy >::quiet_nan() , 以及 square_root().
被这些函数引用 arccosine().
计算反正切值
| x | 参数 |
当|x|>1时,利用恒等式 arctan(x) = π/2 - arctan(1/x)。
引用了 absolute(), is_infinity(), is_nan() , 以及 square_root().
被这些函数引用 arcsine().
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool around_multiple | ( | const decimal_t | x, |
| const decimal_t | axis, | ||
| const decimal_t | toler = constants::DEFAULT_TOLERANCE ) |
判断是否接近某个倍数值
| x | 待判断值 |
| axis | 基准值 |
| toler | 容差 |
| math_exception | 当axis为0时抛出 |
引用了 absolute() , 以及 round().
被这些函数引用 around_pi().
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool around_pi | ( | const decimal_t | x, |
| const decimal_t | toler = constants::LOOSE_TOLERANCE ) |
|
noexcept |
|
constexpr |
向上取整
| x | 原数值 |
引用了 is_finite().
被这些函数引用 ceil(), ceil_bit(), atomic_futex< WaiterBit >::load_when_equal_for(), atomic_futex< WaiterBit >::load_when_equal_until(), hashtable< pair< const Key, T >, Key, HashFcn, select1st< pair< const Key, T > >, EqualKey, Alloc >::rehash(), hashtable< pair< const Key, T >, Key, HashFcn, select1st< pair< const Key, T > >, EqualKey, Alloc >::reserve(), truncate(), condition_variable::wait_for(), condition_variable::wait_for() , 以及 condition_variable::wait_until().
|
noexcept |
计算余弦值
| x | 弧度值 |
利用恒等式 cos(x) = sin(π/2 - x) 计算。
引用了 is_infinity(), is_nan() , 以及 numeric_traits< T, Dummy >::quiet_nan().
|
noexcept |
向下取整
| x | 原数值 |
引用了 is_finite().
被这些函数引用 bit_ceil(), floor(), floor_bit(), round() , 以及 truncate().
|
noexcept |
|
noexcept |
|
noexcept |
计算莱昂纳多数
| n | 索引位置 |
莱昂纳多数:L(n) = 2 * F(n+1) - 1
引用了 fibonacci().
被这些函数引用 adjust_leonardo_heap(), pop_leonardo_heap() , 以及 sort_leonardo_heap().
| NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm | ( | const decimal_t | x, |
| const uint32_t | base ) |
计算自然对数
| x | 真数 |
使用反正切泰勒展开计算。
引用了 absolute(), numeric_traits< T, Dummy >::infinity(), is_infinity(), is_nan() , 以及 numeric_traits< T, Dummy >::quiet_nan().
被这些函数引用 bloom_filter< T, Hash >::approximate_count(), bloom_filter< T, Hash >::capacity() , 以及 logarithm().
| NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 enable_if_t< is_floating_point_v< T >, T > mod | ( | const T | x, |
| const T | y ) |
浮点数取模运算
| T | 浮点类型 |
| x | 被除数 |
| y | 除数 |
| math_exception | 除数为0时 |
引用了 is_infinity(), is_nan() , 以及 numeric_traits< T, Dummy >::quiet_nan().
被这些函数引用 ipackage< integer8, int8_t >::operator%=(), operator&() , 以及 operator|().
|
noexcept |
幂运算
| T | 底数类型 |
| x | 底数 |
| n | 指数 |
使用快速幂算法实现。
引用了 is_arithmetic_v.
被这些函数引用 ceil(), ceil_bit(), exponential(), bloom_filter< T, Hash >::false_positive_rate(), floor(), floor_bit(), round(), round_bit() , 以及 truncate().
|
constexprnoexcept |
|
noexcept |
计算余数
| x | 被除数 |
| y | 除数 |
使用对称舍入规则
引用了 is_infinity(), is_nan(), numeric_traits< T, Dummy >::quiet_nan() , 以及 round().
四舍五入
| x | 原数值 |
引用了 absolute(), floor() , 以及 is_finite().
被这些函数引用 around_multiple(), color::blend(), color::from_premultiplied(), color::operator*(), color::operator*=(), remainder(), round(), color::set_opacity() , 以及 color::to_premultiplied().
|
noexcept |
获取数值的符号
| T | 数值类型 |
| value | 原数值 |
引用了 is_arithmetic_v.
被这些函数引用 datetime::parse_ISO_UTC() , 以及 datetime::to_offset_string().
计算正弦值
| x | 弧度值 |
使用泰勒展开计算,先进行周期性处理。
引用了 is_infinity(), is_nan() , 以及 numeric_traits< T, Dummy >::quiet_nan().
|
noexcept |
计算平方根
| x | 被开方数 |
| precise | 精度要求 |
使用牛顿迭代法计算。
引用了 absolute(), is_infinity(), is_nan(), prev() , 以及 numeric_traits< T, Dummy >::quiet_nan().
被这些函数引用 arcsine() , 以及 arctangent().
|
constexprnoexcept |
|
constexpr |
计算正切值
| x | 弧度值 |
| math_exception | 当x接近π/2的奇数倍时抛出 |
引用了 around_zero(), numeric_traits< T, Dummy >::infinity(), is_infinity(), is_nan() , 以及 numeric_traits< T, Dummy >::quiet_nan().
被这些函数引用 cotangent().