NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
数学函数

基本数学运算函数 更多...

函数

constexpr uint64_t safe_decimal_to_uint64 (const decimal_t x) noexcept
 将非负 decimal_t 安全转换为 uint64_t(避免 >= 2^63 时的 UB)
constexpr uint64_t fibonacci (const uint32_t n) noexcept
 计算斐波那契数
constexpr uint64_t leonardo (const uint32_t n) noexcept
 计算莱昂纳多数
template<typename T>
constexpr T angular2radian (const T angular) noexcept
 角度转弧度
template<typename T>
constexpr T radian2angular (const T radian) noexcept
 弧度转角度
template<typename T>
constexpr enable_if_t< is_signed_v< T >, T > absolute (const T x) noexcept
 取绝对值(有符号数版本)
template<typename T>
constexpr const T & sum (const T &x) noexcept
 单参数求和
template<typename First, typename... Rests, enable_if_t<(sizeof...(Rests) > 0), int > = 0>
constexpr decltype(auto) sum (First first, Rests... args)
 多参数求和
template<typename... Args, enable_if_t<(sizeof...(Args) > 0), int > = 0>
constexpr decltype(auto) average (Args... args)
 计算平均值
template<typename T>
constexpr int sign (const T &value) noexcept
 获取数值的符号
template<typename T>
constexpr T gcd (const T &m, const T &n) noexcept
 计算最大公约数
template<typename T>
constexpr T lcm (const T &m, const T &n) noexcept
 计算最小公倍数
template<typename T>
constexpr enable_if_t< is_floating_point_v< T >, T > mod (const T x, const T y)
 浮点数取模运算
template<typename T>
constexpr T power (const T &x, uint32_t n) noexcept
 幂运算
constexpr decimal_t exponential (const uint32_t n) noexcept
 计算e的n次幂
constexpr decimal_t logarithm_e (const decimal_t x) noexcept
 计算自然对数
constexpr decimal_t logarithm (const decimal_t x, const uint32_t base)
 计算任意底数的对数
constexpr decimal_t logarithm_2 (const decimal_t x)
 计算以2为底的对数
constexpr decimal_t logarithm_10 (const decimal_t x)
 计算以10为底的对数
constexpr decimal_t square_root (const decimal_t x, const decimal_t precise=constants::DEFAULT_TOLERANCE) noexcept
 计算平方根
constexpr decimal_t cube_root (const decimal_t x, const decimal_t precise=constants::DEFAULT_TOLERANCE) noexcept
 计算立方根
constexpr uint64_t factorial (const uint32_t n) noexcept
 计算阶乘
constexpr decimal_t floor (const decimal_t x) noexcept
 向下取整
constexpr decimal_t floor (const decimal_t x, const uint32_t bit) noexcept
 向下取整到指定位数
constexpr decimal_t ceil (const decimal_t x) noexcept
 向上取整
constexpr decimal_t ceil (const decimal_t x, const uint32_t bit) noexcept
 向上取整到指定位数
constexpr decimal_t round (const decimal_t x) noexcept
 四舍五入
constexpr decimal_t floor_bit (const decimal_t x, const uint32_t bit) noexcept
 向下舍入到指定位数
constexpr decimal_t ceil_bit (const decimal_t x, const uint32_t bit) noexcept
 向上舍入到指定位数
constexpr decimal_t round_bit (const decimal_t x, const uint32_t bit) noexcept
 四舍五入到指定位数
constexpr decimal_t truncate_bit (const decimal_t x, const uint32_t bit) noexcept
 截断到指定位数
constexpr decimal_t round (const decimal_t x, const uint32_t bit) noexcept
 四舍五入到指定位数
constexpr decimal_t truncate (const decimal_t x, const int bit) noexcept
 截断
constexpr decimal_t truncate (const decimal_t x) noexcept
 截断到整数位
constexpr bool around_multiple (const decimal_t x, const decimal_t axis, const decimal_t toler=constants::DEFAULT_TOLERANCE)
 判断是否接近某个倍数值
constexpr bool around_pi (const decimal_t x, const decimal_t toler=constants::LOOSE_TOLERANCE)
 判断是否接近π的整数倍
constexpr bool around_zero (const decimal_t x, const decimal_t toler=constants::LOOSE_TOLERANCE) noexcept
 判断是否接近零
constexpr decimal_t remainder (const decimal_t x, const decimal_t y) noexcept
 计算余数
constexpr decimal_t float_part (const decimal_t x) noexcept
 获取小数部分
constexpr decimal_t float_apart (decimal_t x, int64_t *int_ptr) noexcept
 分离整数和小数部分
constexpr decimal_t sine (decimal_t x) noexcept
 计算正弦值
constexpr decimal_t cosine (decimal_t x) noexcept
 计算余弦值
constexpr decimal_t tangent (decimal_t x) noexcept
 计算正切值
constexpr decimal_t cotangent (const decimal_t x)
 计算余切值
constexpr decimal_t arctangent (decimal_t x) noexcept
 计算反正切值
constexpr decimal_t arcsine (const decimal_t x) noexcept
 计算反正弦值
constexpr decimal_t arccosine (const decimal_t x) noexcept
 计算反余弦值

详细描述

基本数学运算函数

遵循的国际标准

本实现严格遵循以下数学计算相关标准规范:

浮点数算术标准:

数学函数规范:

三角函数实现

本实现采用泰勒级数展开与参数归约相结合的方法:

**参数归约**(根据 Cody & Waite 方法):

  1. 将输入 x 映射到 [-π/2, π/2] 区间
  2. 使用高精度 π/2 值(高位 + 低位)减少舍入误差
  3. 记录象限信息以确定最终符号

**泰勒级数展开**:

  • sin(x) = x - x³/3! + x⁵/5! - x⁷/7! + ...
  • cos(x) = 1 - x²/2! + x⁴/4! - x⁶/6! + ...
  • arctan(x) = x - x³/3 + x⁵/5 - x⁷/7 + ... (|x| ≤ 1)

迭代终止条件:当前项小于 MACHINE_EPSILON × 当前和

对数函数实现

**自然对数 ln(x)**:

  • 使用恒等式:ln(x) = 2 × arctanh((x-1)/(x+1))
  • arctanh(y) = y + y³/3 + y⁵/5 + ... (|y| < 1)

**任意底对数**:

  • log_base(x) = ln(x) / ln(base)

方根函数实现

**平方根 √x**(牛顿迭代法):

  • 初始猜测:guess = x × 0.5
  • 迭代公式:guess = 0.5 × (guess + x / guess)
  • 终止条件:|guess - prev| ≤ tolerance × |guess|

**立方根 ³√x**(牛顿迭代法):

  • 迭代公式:guess = (2×prev + x / prev²) / 3

特殊值处理

根据 IEEE 754-2019 §6,特殊值处理规则:

输入情况 返回值
输入为 NaN 返回 NaN
输入为 ±∞ 根据函数语义返回 ±∞ 或 NaN
负数的平方根 返回 NaN
除数为零 返回 NaN 或抛出异常

| |x| > 1 的反正弦/反余弦| 返回 NaN |

性能与精度说明

  • 三角函数使用参数归约减少大输入值的误差
  • 泰勒级数迭代次数由机器精度自适应确定
  • 预计算的斐波那契数列加速小索引值的查询
参见
https://standards.ieee.org/ieee/754/6210/
https://dlmf.nist.gov/
https://en.cppreference.com/w/cpp/numeric/math

函数说明

◆ absolute()

template<typename T>
enable_if_t< is_signed_v< T >, T > absolute ( const T x)
constexprnoexcept

取绝对值(有符号数版本)

模板参数
T数值类型
参数
x原数值
返回
绝对值

在文件 math.hpp246 行定义.

被这些函数引用 arccosine(), arcsine(), arctangent(), around_multiple(), around_zero(), ceil(), cube_root(), floor(), gcd(), logarithm_e(), round(), square_root() , 以及 truncate().

◆ angular2radian()

template<typename T>
T angular2radian ( const T angular)
constexprnoexcept

角度转弧度

模板参数
T运算类型
参数
angular角度值
返回
对应的弧度值

在文件 math.hpp222 行定义.

引用了 is_arithmetic_v.

◆ arccosine()

decimal_t arccosine ( const decimal_t x)
constexprnoexcept

计算反余弦值

参数
x参数(|x| ≤ 1)
返回
arccos(x)(弧度值,范围[0, π])

在文件 math.hpp1095 行定义.

引用了 absolute(), arcsine(), is_nan() , 以及 numeric_traits< T, Dummy >::quiet_nan().

◆ arcsine()

decimal_t arcsine ( const decimal_t x)
constexprnoexcept

计算反正弦值

参数
x参数(|x| ≤ 1)
返回
arcsin(x)(弧度值,范围[-π/2, π/2])

在文件 math.hpp1074 行定义.

引用了 absolute(), arctangent(), is_nan(), numeric_traits< T, Dummy >::quiet_nan() , 以及 square_root().

被这些函数引用 arccosine().

◆ arctangent()

decimal_t arctangent ( decimal_t x)
constexprnoexcept

计算反正切值

参数
x参数
返回
arctan(x)(弧度值,范围(-π/2, π/2))

当|x|>1时,利用恒等式 arctan(x) = π/2 - arctan(1/x)。

在文件 math.hpp1029 行定义.

引用了 absolute(), is_infinity(), is_nan() , 以及 square_root().

被这些函数引用 arcsine().

◆ around_multiple()

bool around_multiple ( const decimal_t x,
const decimal_t axis,
const decimal_t toler = constants::DEFAULT_TOLERANCE )
constexpr

判断是否接近某个倍数值

参数
x待判断值
axis基准值
toler容差
返回
如果x接近axis的整数倍则返回true
异常
math_exception当axis为0时抛出

在文件 math.hpp783 行定义.

引用了 absolute() , 以及 round().

被这些函数引用 around_pi().

◆ around_pi()

bool around_pi ( const decimal_t x,
const decimal_t toler = constants::LOOSE_TOLERANCE )
constexpr

判断是否接近π的整数倍

参数
x待判断值
toler容差
返回
如果x接近π的整数倍则返回true

在文件 math.hpp798 行定义.

引用了 around_multiple().

◆ around_zero()

bool around_zero ( const decimal_t x,
const decimal_t toler = constants::LOOSE_TOLERANCE )
constexprnoexcept

判断是否接近零

参数
x待判断值
toler容差
返回
如果|x| < toler则返回true

在文件 math.hpp808 行定义.

引用了 absolute().

被这些函数引用 tangent().

◆ average()

template<typename... Args, enable_if_t<(sizeof...(Args) > 0), int > = 0>
decltype(auto) average ( Args... args)
constexpr

计算平均值

模板参数
Args参数类型
参数
args要求平均值的参数
返回
平均值

在文件 math.hpp295 行定义.

引用了 sum().

◆ ceil() [1/2]

◆ ceil() [2/2]

decimal_t ceil ( const decimal_t x,
const uint32_t bit )
constexprnoexcept

向上取整到指定位数

参数
x原数值
bit位数
返回
向上取整后的值

在文件 math.hpp664 行定义.

引用了 ceil() , 以及 power().

◆ ceil_bit()

decimal_t ceil_bit ( const decimal_t x,
const uint32_t bit )
constexprnoexcept

向上舍入到指定位数

参数
x原数值
bit位数
返回
向上舍入后的值

在文件 math.hpp715 行定义.

引用了 ceil() , 以及 power().

被这些函数引用 round_bit() , 以及 truncate_bit().

◆ cosine()

decimal_t cosine ( decimal_t x)
constexprnoexcept

计算余弦值

参数
x弧度值
返回
cos(x)

利用恒等式 cos(x) = sin(π/2 - x) 计算。

在文件 math.hpp949 行定义.

引用了 is_infinity(), is_nan() , 以及 numeric_traits< T, Dummy >::quiet_nan().

◆ cotangent()

decimal_t cotangent ( const decimal_t x)
constexpr

计算余切值

参数
x弧度值
返回
cot(x)
异常
math_exception当x接近π的整数倍时抛出

在文件 math.hpp1020 行定义.

引用了 tangent().

◆ cube_root()

decimal_t cube_root ( const decimal_t x,
const decimal_t precise = constants::DEFAULT_TOLERANCE )
constexprnoexcept

计算立方根

参数
x被开方数
precise精度要求
返回
³√x

使用牛顿迭代法计算。

在文件 math.hpp544 行定义.

引用了 absolute(), is_infinity(), is_nan() , 以及 prev().

◆ exponential()

decimal_t exponential ( const uint32_t n)
constexprnoexcept

计算e的n次幂

参数
n指数
返回
e^n

在文件 math.hpp419 行定义.

引用了 power().

◆ factorial()

uint64_t factorial ( const uint32_t n)
constexprnoexcept

计算阶乘

参数
n非负整数
返回
n!

在文件 math.hpp573 行定义.

◆ fibonacci()

uint64_t fibonacci ( const uint32_t n)
constexprnoexcept

计算斐波那契数

参数
n索引位置
返回
第n个斐波那契数

如果n小于预计算的数量,直接返回预计算结果; 否则递归计算。

在文件 math.hpp190 行定义.

被这些函数引用 leonardo().

◆ float_apart()

decimal_t float_apart ( decimal_t x,
int64_t * int_ptr )
constexprnoexcept

分离整数和小数部分

参数
x原数值
int_ptr指向整数部分的指针
返回
小数部分

将x的整数部分存入int_ptr,返回小数部分。

在文件 math.hpp845 行定义.

◆ float_part()

decimal_t float_part ( const decimal_t x)
constexprnoexcept

获取小数部分

参数
x原数值
返回
x的小数部分

在文件 math.hpp833 行定义.

◆ floor() [1/2]

decimal_t floor ( const decimal_t x)
constexprnoexcept

向下取整

参数
x原数值
返回
向下取整后的值

在文件 math.hpp586 行定义.

引用了 absolute(), is_finite(), numeric_traits< T, Dummy >::max() , 以及 safe_decimal_to_uint64().

被这些函数引用 bit_ceil(), floor(), floor_bit(), round() , 以及 truncate().

◆ floor() [2/2]

decimal_t floor ( const decimal_t x,
const uint32_t bit )
constexprnoexcept

向下取整到指定位数

参数
x原数值
bit位数
返回
向下取整后的值

在文件 math.hpp617 行定义.

引用了 floor() , 以及 power().

◆ floor_bit()

decimal_t floor_bit ( const decimal_t x,
const uint32_t bit )
constexprnoexcept

向下舍入到指定位数

参数
x原数值
bit位数,>0表示小数位,0表示整数位
返回
向下舍入后的值

在文件 math.hpp704 行定义.

引用了 floor() , 以及 power().

被这些函数引用 round_bit() , 以及 truncate_bit().

◆ gcd()

template<typename T>
T gcd ( const T & m,
const T & n )
constexprnoexcept

计算最大公约数

模板参数
T数值类型
参数
m第一个数
n第二个数
返回
最大公约数

在文件 math.hpp326 行定义.

引用了 absolute().

被这些函数引用 lcm().

◆ lcm()

template<typename T>
T lcm ( const T & m,
const T & n )
constexprnoexcept

计算最小公倍数

模板参数
T数值类型
参数
m第一个数
n第二个数
返回
最小公倍数

在文件 math.hpp345 行定义.

引用了 gcd().

◆ leonardo()

uint64_t leonardo ( const uint32_t n)
constexprnoexcept

计算莱昂纳多数

参数
n索引位置
返回
第n个莱昂纳多数

莱昂纳多数:L(n) = 2 * F(n+1) - 1

在文件 math.hpp211 行定义.

引用了 fibonacci().

被这些函数引用 adjust_leonardo_heap(), pop_leonardo_heap() , 以及 sort_leonardo_heap().

◆ logarithm()

decimal_t logarithm ( const decimal_t x,
const uint32_t base )
constexpr

计算任意底数的对数

参数
x真数
base底数
返回
以base为底x的对数

在文件 math.hpp481 行定义.

引用了 logarithm_e().

被这些函数引用 logarithm_10() , 以及 logarithm_2().

◆ logarithm_10()

decimal_t logarithm_10 ( const decimal_t x)
constexpr

计算以10为底的对数

参数
x真数
返回
log₁₀(x)

在文件 math.hpp501 行定义.

引用了 logarithm().

◆ logarithm_2()

decimal_t logarithm_2 ( const decimal_t x)
constexpr

计算以2为底的对数

参数
x真数
返回
log₂(x)

在文件 math.hpp494 行定义.

引用了 logarithm().

◆ logarithm_e()

decimal_t logarithm_e ( const decimal_t x)
constexprnoexcept

计算自然对数

参数
x真数
返回
ln(x)

使用反正切泰勒展开计算。

在文件 math.hpp430 行定义.

引用了 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().

◆ mod()

template<typename T>
enable_if_t< is_floating_point_v< T >, T > mod ( const T x,
const T y )
constexpr

浮点数取模运算

模板参数
T浮点类型
参数
x被除数
y除数
返回
x除以y的余数
异常
math_exception除数为0时

在文件 math.hpp358 行定义.

引用了 is_infinity(), is_nan() , 以及 numeric_traits< T, Dummy >::quiet_nan().

被这些函数引用 ipackage< integer8, int8_t >::operator%=(), operator&() , 以及 operator|().

◆ power()

template<typename T>
T power ( const T & x,
uint32_t n )
constexprnoexcept

幂运算

模板参数
T底数类型
参数
x底数
n指数
返回
x的n次幂

使用快速幂算法实现。

在文件 math.hpp397 行定义.

引用了 is_arithmetic_v.

被这些函数引用 ceil(), ceil_bit(), exponential(), bloom_filter< T, Hash >::false_positive_rate(), floor(), floor_bit(), round(), round_bit() , 以及 truncate().

◆ radian2angular()

template<typename T>
T radian2angular ( const T radian)
constexprnoexcept

弧度转角度

模板参数
T运算类型
参数
radian弧度值
返回
对应的角度值

在文件 math.hpp234 行定义.

引用了 is_arithmetic_v.

◆ remainder()

decimal_t remainder ( const decimal_t x,
const decimal_t y )
constexprnoexcept

计算余数

参数
x被除数
y除数
返回
x除以y的余数

使用对称舍入规则

在文件 math.hpp821 行定义.

引用了 is_infinity(), is_nan(), numeric_traits< T, Dummy >::quiet_nan() , 以及 round().

被这些函数引用 _udiv128() , 以及 uint128_t::div64().

◆ round() [1/2]

decimal_t round ( const decimal_t x)
constexprnoexcept

四舍五入

参数
x原数值
返回
四舍五入后的值

在文件 math.hpp674 行定义.

引用了 absolute(), floor() , 以及 is_finite().

被这些函数引用 around_multiple(), color::blend(), color::from_premultiplied(), color::operator*(), color::operator*=(), remainder(), round(), color::set_opacity() , 以及 color::to_premultiplied().

◆ round() [2/2]

decimal_t round ( const decimal_t x,
const uint32_t bit )
constexprnoexcept

四舍五入到指定位数

参数
x原数值
bit位数
返回
四舍五入后的值

在文件 math.hpp747 行定义.

引用了 power() , 以及 round().

◆ round_bit()

decimal_t round_bit ( const decimal_t x,
const uint32_t bit )
constexprnoexcept

四舍五入到指定位数

参数
x原数值
bit位数
返回
四舍五入后的值

在文件 math.hpp726 行定义.

引用了 ceil_bit(), floor_bit() , 以及 power().

◆ safe_decimal_to_uint64()

uint64_t safe_decimal_to_uint64 ( const decimal_t x)
constexprnoexcept

将非负 decimal_t 安全转换为 uint64_t(避免 >= 2^63 时的 UB)

返回
对应的 uint64_t

在文件 math.hpp173 行定义.

被这些函数引用 ceil() , 以及 floor().

◆ sign()

template<typename T>
int sign ( const T & value)
constexprnoexcept

获取数值的符号

模板参数
T数值类型
参数
value原数值
返回
符号值:正数返回1,负数返回-1,零返回0

在文件 math.hpp306 行定义.

引用了 is_arithmetic_v.

被这些函数引用 datetime::parse_RFC3339() , 以及 datetime::to_offset_string().

◆ sine()

decimal_t sine ( decimal_t x)
constexprnoexcept

计算正弦值

参数
x弧度值
返回
sin(x)

使用泰勒展开计算,先进行周期性处理。

在文件 math.hpp919 行定义.

引用了 is_infinity(), is_nan() , 以及 numeric_traits< T, Dummy >::quiet_nan().

◆ square_root()

decimal_t square_root ( const decimal_t x,
const decimal_t precise = constants::DEFAULT_TOLERANCE )
constexprnoexcept

计算平方根

参数
x被开方数
precise精度要求
返回
√x

使用牛顿迭代法计算。

在文件 math.hpp512 行定义.

引用了 absolute(), is_infinity(), is_nan(), prev() , 以及 numeric_traits< T, Dummy >::quiet_nan().

被这些函数引用 arcsine() , 以及 arctangent().

◆ sum() [1/2]

template<typename T>
const T & sum ( const T & x)
constexprnoexcept

单参数求和

模板参数
T数值类型
参数
x唯一参数
返回
参数本身

递归sum的终止位置

在文件 math.hpp271 行定义.

被这些函数引用 average() , 以及 sum().

◆ sum() [2/2]

template<typename First, typename... Rests, enable_if_t<(sizeof...(Rests) > 0), int > = 0>
decltype(auto) sum ( First first,
Rests... args )
constexpr

多参数求和

模板参数
First第一个参数类型
Rests剩余参数类型
参数
first第一个参数
args剩余参数
返回
所有参数的和

在文件 math.hpp284 行定义.

引用了 sum().

◆ tangent()

decimal_t tangent ( decimal_t x)
constexprnoexcept

计算正切值

参数
x弧度值
返回
tan(x)
异常
math_exception当x接近π/2的奇数倍时抛出

在文件 math.hpp977 行定义.

引用了 around_zero(), numeric_traits< T, Dummy >::infinity(), is_infinity(), is_nan() , 以及 numeric_traits< T, Dummy >::quiet_nan().

被这些函数引用 cotangent().

◆ truncate() [1/2]

decimal_t truncate ( const decimal_t x)
constexprnoexcept

截断到整数位

参数
x原数值
返回
截断后的整数值

在文件 math.hpp768 行定义.

引用了 is_finite().

◆ truncate() [2/2]

decimal_t truncate ( const decimal_t x,
const int bit )
constexprnoexcept

截断

参数
x原数值
bit位数
返回
截断后的值

在文件 math.hpp758 行定义.

引用了 absolute(), ceil(), floor() , 以及 power().

◆ truncate_bit()

decimal_t truncate_bit ( const decimal_t x,
const uint32_t bit )
constexprnoexcept

截断到指定位数

参数
x原数值
bit位数
返回
截断后的值

在文件 math.hpp736 行定义.

引用了 ceil_bit() , 以及 floor_bit().