std::erf, std::erff, std::erfl
来自 cppreference.cn
定义于头文件 <cmath> |
||
(1) | ||
float erf ( float num ); double erf ( double num ); |
(直到 C++23) | |
/*floating-point-type*/ erf ( /*floating-point-type*/ num ); |
(自 C++23 起) (constexpr 自 C++26 起) |
|
float erff( float num ); |
(2) | (自 C++11 起) (constexpr 自 C++26 起) |
long double erfl( long double num ); |
(3) | (自 C++11 起) (constexpr 自 C++26 起) |
SIMD 重载 (自 C++26 起) |
||
定义于头文件 <simd> |
||
template< /*math-floating-point*/ V > constexpr /*deduced-simd-t*/<V> |
(S) | (自 C++26 起) |
附加重载 (自 C++11 起) |
||
定义于头文件 <cmath> |
||
template< class Integer > double erf ( Integer num ); |
(A) | (constexpr 自 C++26 起) |
S) SIMD 重载对 v_num 执行元素级
std::erf 。
|
(自 C++26 起) |
A) 为所有整数类型提供了附加重载,它们被视为 double。
|
(自 C++11 起) |
目录 |
[编辑] 参数
num | - | 浮点值或整数值 |
[编辑] 返回值
如果没有错误发生,则返回 num 的误差函数值,即2 |
√π |
0e-t2
dt。
如果由于下溢而发生范围错误,则返回正确的结果(舍入后),即
2*num |
√π |
[编辑] 错误处理
错误报告按照 math_errhandling 中的规定。
如果实现支持 IEEE 浮点算术 (IEC 60559),
- 如果参数为 ±0,则返回 ±0。
- 如果参数为 ±∞,则返回 ±1。
- 如果参数为 NaN,则返回 NaN。
[编辑] 注解
如果 |num| < DBL_MIN * (std::sqrt(π) / 2),则保证发生下溢。
erf(x |
σ√2 |
附加重载不一定需要完全按照 (A) 的形式提供。它们只需要足以确保对于整数类型的参数 num,std::erf(num) 与 std::erf(static_cast<double>(num)) 具有相同的效果。
[编辑] 示例
以下示例计算正态变量在区间 (x1, x2) 上的概率
运行此代码
#include <cmath> #include <iomanip> #include <iostream> double phi(double x1, double x2) { return (std::erf(x2 / std::sqrt(2)) - std::erf(x1 / std::sqrt(2))) / 2; } int main() { std::cout << "Normal variate probabilities:\n" << std::fixed << std::setprecision(2); for (int n = -4; n < 4; ++n) std::cout << '[' << std::setw(2) << n << ':' << std::setw(2) << n + 1 << "]: " << std::setw(5) << 100 * phi(n, n + 1) << "%\n"; std::cout << "Special values:\n" << "erf(-0) = " << std::erf(-0.0) << '\n' << "erf(Inf) = " << std::erf(INFINITY) << '\n'; }
输出
Normal variate probabilities: [-4:-3]: 0.13% [-3:-2]: 2.14% [-2:-1]: 13.59% [-1: 0]: 34.13% [ 0: 1]: 34.13% [ 1: 2]: 13.59% [ 2: 3]: 2.14% [ 3: 4]: 0.13% Special values: erf(-0) = -0.00 erf(Inf) = 1.00
[编辑] 参见
(C++11)(C++11)(C++11) |
互补误差函数 (函数) |
C 文档 关于 erf
|
[编辑] 外部链接
Weisstein, Eric W. "Erf." 来自 MathWorld — Wolfram Web Resource。 |