|
NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
|
基本数学运算函数 更多...
函数 | |
| constexpr uint64_t | neforce::safe_decimal_to_uint64 (const decimal_t x) noexcept |
| 将非负 decimal_t 安全转换为 uint64_t(避免 >= 2^63 时的 UB) | |
| constexpr uint64_t | neforce::fibonacci (const uint32_t n) noexcept |
| 计算斐波那契数 | |
| constexpr uint64_t | neforce::leonardo (const uint32_t n) noexcept |
| 计算莱昂纳多数 | |
| template<typename T> | |
| constexpr T | neforce::angular2radian (const T angular) noexcept |
| 角度转弧度 | |
| template<typename T> | |
| constexpr T | neforce::radian2angular (const T radian) noexcept |
| 弧度转角度 | |
| template<typename T> | |
| constexpr enable_if_t< is_signed_v< T >, T > | neforce::absolute (const T x) noexcept |
| 取绝对值(有符号数版本) | |
| template<typename T> | |
| constexpr const T & | neforce::sum (const T &x) noexcept |
| 单参数求和 | |
| template<typename First, typename... Rests, enable_if_t<(sizeof...(Rests) > 0), int > = 0> | |
| constexpr decltype(auto) | neforce::sum (First first, Rests... args) |
| 多参数求和 | |
| template<typename... Args, enable_if_t<(sizeof...(Args) > 0), int > = 0> | |
| constexpr decltype(auto) | neforce::average (Args... args) |
| 计算平均值 | |
| template<typename T> | |
| constexpr int | neforce::sign (const T &value) noexcept |
| 获取数值的符号 | |
| template<typename T> | |
| constexpr T | neforce::gcd (const T &m, const T &n) noexcept |
| 计算最大公约数 | |
| template<typename T> | |
| constexpr T | neforce::lcm (const T &m, const T &n) noexcept |
| 计算最小公倍数 | |
| template<typename T> | |
| constexpr enable_if_t< is_floating_point_v< T >, T > | neforce::mod (const T x, const T y) |
| 浮点数取模运算 | |
| template<typename T> | |
| constexpr T | neforce::power (const T &x, uint32_t n) noexcept |
| 幂运算 | |
| constexpr decimal_t | neforce::exponential (const uint32_t n) noexcept |
| 计算e的n次幂 | |
| constexpr decimal_t | neforce::logarithm_e (const decimal_t x) noexcept |
| 计算自然对数 | |
| constexpr decimal_t | neforce::logarithm (const decimal_t x, const uint32_t base) |
| 计算任意底数的对数 | |
| constexpr decimal_t | neforce::logarithm_2 (const decimal_t x) |
| 计算以2为底的对数 | |
| constexpr decimal_t | neforce::logarithm_10 (const decimal_t x) |
| 计算以10为底的对数 | |
| constexpr decimal_t | neforce::square_root (const decimal_t x, const decimal_t precise=constants::DEFAULT_TOLERANCE) noexcept |
| 计算平方根 | |
| constexpr decimal_t | neforce::cube_root (const decimal_t x, const decimal_t precise=constants::DEFAULT_TOLERANCE) noexcept |
| 计算立方根 | |
| constexpr uint64_t | neforce::factorial (const uint32_t n) noexcept |
| 计算阶乘 | |
| constexpr decimal_t | neforce::floor (const decimal_t x) noexcept |
| 向下取整 | |
| constexpr decimal_t | neforce::floor (const decimal_t x, const uint32_t bit) noexcept |
| 向下取整到指定位数 | |
| constexpr decimal_t | neforce::ceil (const decimal_t x) noexcept |
| 向上取整 | |
| constexpr decimal_t | neforce::ceil (const decimal_t x, const uint32_t bit) noexcept |
| 向上取整到指定位数 | |
| constexpr decimal_t | neforce::round (const decimal_t x) noexcept |
| 四舍五入 | |
| constexpr decimal_t | neforce::floor_bit (const decimal_t x, const uint32_t bit) noexcept |
| 向下舍入到指定位数 | |
| constexpr decimal_t | neforce::ceil_bit (const decimal_t x, const uint32_t bit) noexcept |
| 向上舍入到指定位数 | |
| constexpr decimal_t | neforce::round_bit (const decimal_t x, const uint32_t bit) noexcept |
| 四舍五入到指定位数 | |
| constexpr decimal_t | neforce::truncate_bit (const decimal_t x, const uint32_t bit) noexcept |
| 截断到指定位数 | |
| constexpr decimal_t | neforce::round (const decimal_t x, const uint32_t bit) noexcept |
| 四舍五入到指定位数 | |
| constexpr decimal_t | neforce::truncate (const decimal_t x, const int bit) noexcept |
| 截断 | |
| constexpr decimal_t | neforce::truncate (const decimal_t x) noexcept |
| 截断到整数位 | |
| constexpr bool | neforce::around_multiple (const decimal_t x, const decimal_t axis, const decimal_t toler=constants::DEFAULT_TOLERANCE) |
| 判断是否接近某个倍数值 | |
| constexpr bool | neforce::around_pi (const decimal_t x, const decimal_t toler=constants::LOOSE_TOLERANCE) |
| 判断是否接近π的整数倍 | |
| constexpr bool | neforce::around_zero (const decimal_t x, const decimal_t toler=constants::LOOSE_TOLERANCE) noexcept |
| 判断是否接近零 | |
| constexpr decimal_t | neforce::remainder (const decimal_t x, const decimal_t y) noexcept |
| 计算余数 | |
| constexpr decimal_t | neforce::float_part (const decimal_t x) noexcept |
| 获取小数部分 | |
| constexpr decimal_t | neforce::float_apart (decimal_t x, int64_t *int_ptr) noexcept |
| 分离整数和小数部分 | |
| constexpr decimal_t | neforce::sine (decimal_t x) noexcept |
| 计算正弦值 | |
| constexpr decimal_t | neforce::cosine (decimal_t x) noexcept |
| 计算余弦值 | |
| constexpr decimal_t | neforce::tangent (decimal_t x) noexcept |
| 计算正切值 | |
| constexpr decimal_t | neforce::cotangent (const decimal_t x) |
| 计算余切值 | |
| constexpr decimal_t | neforce::arctangent (decimal_t x) noexcept |
| 计算反正切值 | |
| constexpr decimal_t | neforce::arcsine (const decimal_t x) noexcept |
| 计算反正弦值 | |
| constexpr decimal_t | neforce::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 | 原数值 |
引用了 absolute().
被这些函数引用 absolute(), arccosine(), arcsine(), arctangent(), around_multiple(), around_zero(), ceil(), cube_root(), floor(), gcd(), logarithm_e(), round(), square_root() , 以及 truncate().
|
constexprnoexcept |
角度转弧度
| T | 运算类型 |
| angular | 角度值 |
引用了 angular2radian(), is_arithmetic_v, neforce::constants::PI , 以及 neforce::constants::SEMI_CIRCLE.
被这些函数引用 angular2radian().
计算反余弦值
| x | 参数(|x| ≤ 1) |
引用了 absolute(), arccosine(), arcsine(), is_nan(), neforce::constants::PI , 以及 neforce::numeric_traits< T, Dummy >::quiet_nan().
被这些函数引用 arccosine().
计算反正弦值
| x | 参数(|x| ≤ 1) |
引用了 absolute(), arcsine(), arctangent(), is_nan(), neforce::constants::PI, neforce::numeric_traits< T, Dummy >::quiet_nan() , 以及 square_root().
被这些函数引用 arccosine() , 以及 arcsine().
计算反正切值
| x | 参数 |
当|x|>1时,利用恒等式 arctan(x) = π/2 - arctan(1/x)。
引用了 absolute(), arctangent(), is_infinity(), is_nan(), neforce::constants::MACHINE_EPSILON, neforce::constants::PI , 以及 square_root().
被这些函数引用 arcsine() , 以及 arctangent().
|
constexpr |
判断是否接近某个倍数值
| x | 待判断值 |
| axis | 基准值 |
| toler | 容差 |
| math_exception | 当axis为0时抛出 |
引用了 absolute(), around_multiple(), neforce::constants::DEFAULT_TOLERANCE, neforce::constants::MACHINE_EPSILON , 以及 round().
被这些函数引用 around_multiple() , 以及 around_pi().
|
constexpr |
判断是否接近π的整数倍
| x | 待判断值 |
| toler | 容差 |
引用了 around_multiple(), around_pi(), neforce::constants::LOOSE_TOLERANCE , 以及 neforce::constants::PI.
被这些函数引用 around_pi().
|
constexprnoexcept |
判断是否接近零
| x | 待判断值 |
| toler | 容差 |
引用了 absolute(), around_zero() , 以及 neforce::constants::LOOSE_TOLERANCE.
被这些函数引用 around_zero() , 以及 tangent().
|
constexpr |
向上取整
| x | 原数值 |
引用了 absolute(), ceil(), is_finite(), neforce::constants::MACHINE_EPSILON, neforce::numeric_traits< T, Dummy >::max() , 以及 safe_decimal_to_uint64().
被这些函数引用 ceil(), ceil(), ceil(), ceil_bit(), neforce::atomic_futex< WaiterBit >::load_when_equal_for(), neforce::atomic_futex< WaiterBit >::load_when_equal_until(), neforce::hashtable< pair< const Key, T >, Key, HashFcn, select1st< pair< const Key, T > >, EqualKey, Alloc >::rehash(), neforce::hashtable< pair< const Key, T >, Key, HashFcn, select1st< pair< const Key, T > >, EqualKey, Alloc >::reserve(), truncate(), neforce::condition_variable::wait_for(), neforce::condition_variable::wait_for() , 以及 neforce::condition_variable::wait_until().
向上舍入到指定位数
| x | 原数值 |
| bit | 位数 |
引用了 ceil(), ceil_bit() , 以及 power().
被这些函数引用 ceil_bit(), round_bit() , 以及 truncate_bit().
计算余弦值
| x | 弧度值 |
利用恒等式 cos(x) = sin(π/2 - x) 计算。
引用了 cosine(), is_infinity(), is_nan() , 以及 neforce::numeric_traits< T, Dummy >::quiet_nan().
被这些函数引用 cosine().
计算余切值
| x | 弧度值 |
| math_exception | 当x接近π的整数倍时抛出 |
引用了 cotangent() , 以及 tangent().
被这些函数引用 cotangent().
|
constexprnoexcept |
计算立方根
| x | 被开方数 |
| precise | 精度要求 |
使用牛顿迭代法计算。
引用了 absolute(), cube_root(), neforce::constants::DEFAULT_TOLERANCE, is_infinity(), is_nan() , 以及 prev().
被这些函数引用 cube_root().
计算e的n次幂
| n | 指数 |
引用了 neforce::constants::EULER, exponential() , 以及 power().
被这些函数引用 exponential().
计算斐波那契数
| n | 索引位置 |
如果n小于预计算的数量,直接返回预计算结果; 否则递归计算。
引用了 fibonacci(), neforce::constants::FIBONACCI_COUNT , 以及 neforce::constants::FIBONACCI_LIST.
被这些函数引用 fibonacci() , 以及 leonardo().
分离整数和小数部分
| x | 原数值 |
| int_ptr | 指向整数部分的指针 |
将x的整数部分存入int_ptr,返回小数部分。
引用了 float_apart().
被这些函数引用 float_apart().
向下取整
| x | 原数值 |
引用了 absolute(), floor(), is_finite(), neforce::constants::MACHINE_EPSILON, neforce::numeric_traits< T, Dummy >::max() , 以及 safe_decimal_to_uint64().
被这些函数引用 bit_ceil(), floor(), floor(), floor(), floor_bit(), round() , 以及 truncate().
向下舍入到指定位数
| x | 原数值 |
| bit | 位数,>0表示小数位,0表示整数位 |
引用了 floor(), floor_bit() , 以及 power().
被这些函数引用 floor_bit(), round_bit() , 以及 truncate_bit().
|
constexprnoexcept |
|
constexprnoexcept |
计算莱昂纳多数
| n | 索引位置 |
莱昂纳多数:L(n) = 2 * F(n+1) - 1
引用了 fibonacci() , 以及 leonardo().
被这些函数引用 adjust_leonardo_heap(), leonardo(), pop_leonardo_heap() , 以及 sort_leonardo_heap().
计算任意底数的对数
| x | 真数 |
| base | 底数 |
引用了 logarithm() , 以及 logarithm_e().
被这些函数引用 logarithm(), logarithm_10() , 以及 logarithm_2().
计算自然对数
| x | 真数 |
使用反正切泰勒展开计算。
引用了 absolute(), neforce::numeric_traits< T, Dummy >::infinity(), is_infinity(), is_nan(), logarithm_e(), neforce::constants::MACHINE_EPSILON , 以及 neforce::numeric_traits< T, Dummy >::quiet_nan().
被这些函数引用 neforce::bloom_filter< T, Hash >::approximate_count(), neforce::bloom_filter< T, Hash >::capacity(), logarithm() , 以及 logarithm_e().
|
constexpr |
浮点数取模运算
| T | 浮点类型 |
| x | 被除数 |
| y | 除数 |
| math_exception | 除数为0时 |
引用了 is_infinity(), is_nan(), mod() , 以及 neforce::numeric_traits< T, Dummy >::quiet_nan().
被这些函数引用 mod(), neforce::ipackage< integer8, int8_t >::operator%=(), operator&() , 以及 operator|().
|
constexprnoexcept |
幂运算
| T | 底数类型 |
| x | 底数 |
| n | 指数 |
使用快速幂算法实现。
引用了 is_arithmetic_v , 以及 power().
被这些函数引用 ceil(), ceil_bit(), exponential(), neforce::bloom_filter< T, Hash >::false_positive_rate(), floor(), floor_bit(), power(), round(), round_bit() , 以及 truncate().
|
constexprnoexcept |
弧度转角度
| T | 运算类型 |
| radian | 弧度值 |
引用了 is_arithmetic_v, neforce::constants::PI, radian2angular() , 以及 neforce::constants::SEMI_CIRCLE.
被这些函数引用 radian2angular().
计算余数
| x | 被除数 |
| y | 除数 |
使用对称舍入规则
引用了 is_infinity(), is_nan(), neforce::numeric_traits< T, Dummy >::quiet_nan(), remainder() , 以及 round().
被这些函数引用 _udiv128(), neforce::uint128_t::div64(), neforce::uint128_t::divmod128() , 以及 remainder().
四舍五入
| x | 原数值 |
引用了 absolute(), floor(), is_finite() , 以及 round().
被这些函数引用 around_multiple(), neforce::color::blend(), neforce::color::from_premultiplied(), neforce::color::operator*(), neforce::color::operator*=(), remainder(), round(), round(), neforce::color::set_opacity() , 以及 neforce::color::to_premultiplied().
四舍五入到指定位数
| x | 原数值 |
| bit | 位数 |
引用了 ceil_bit(), floor_bit(), power() , 以及 round_bit().
被这些函数引用 round_bit().
将非负 decimal_t 安全转换为 uint64_t(避免 >= 2^63 时的 UB)
被这些函数引用 ceil(), floor() , 以及 safe_decimal_to_uint64().
|
constexprnoexcept |
获取数值的符号
| T | 数值类型 |
| value | 原数值 |
引用了 is_arithmetic_v , 以及 sign().
被这些函数引用 neforce::simd::max_i8(), neforce::simd::max_u32(), neforce::simd::min_i8(), neforce::simd::min_u32(), neforce::datetime::parse_RFC3339(), neforce::simd::saturated_add_u16(), neforce::simd::saturated_sub_u16(), sign() , 以及 neforce::datetime::to_offset_string().
计算正弦值
| x | 弧度值 |
使用泰勒展开计算,先进行周期性处理。
引用了 is_infinity(), is_nan(), neforce::numeric_traits< T, Dummy >::quiet_nan() , 以及 sine().
被这些函数引用 sine().
|
constexprnoexcept |
计算平方根
| x | 被开方数 |
| precise | 精度要求 |
使用牛顿迭代法计算。
引用了 absolute(), neforce::constants::DEFAULT_TOLERANCE, is_infinity(), is_nan(), prev(), neforce::numeric_traits< T, Dummy >::quiet_nan() , 以及 square_root().
被这些函数引用 arcsine(), arctangent() , 以及 square_root().
|
constexprnoexcept |
|
constexpr |
计算正切值
| x | 弧度值 |
| math_exception | 当x接近π/2的奇数倍时抛出 |
引用了 around_zero(), neforce::numeric_traits< T, Dummy >::infinity(), is_infinity(), is_nan(), neforce::constants::LOOSE_TOLERANCE, neforce::numeric_traits< T, Dummy >::quiet_nan() , 以及 tangent().
被这些函数引用 cotangent() , 以及 tangent().
截断
| x | 原数值 |
| bit | 位数 |
引用了 absolute(), ceil(), floor(), power() , 以及 truncate().
被这些函数引用 truncate() , 以及 truncate().
截断到指定位数
| x | 原数值 |
| bit | 位数 |
引用了 ceil_bit(), floor_bit() , 以及 truncate_bit().
被这些函数引用 truncate_bit().