std:: erf, std:: erff, std:: erfl
|
定义于头文件
<cmath>
|
||
| (1) | ||
|
float
erf
(
float
num
)
;
double
erf
(
double
num
)
;
|
(C++23 前) | |
|
/*floating-point-type*/
erf ( /*floating-point-type*/ num ) ; |
(C++23 起)
(C++26 起为 constexpr) |
|
|
float
erff
(
float
num
)
;
|
(2) |
(C++11 起)
(C++26 起为 constexpr) |
|
long
double
erfl
(
long
double
num
)
;
|
(3) |
(C++11 起)
(C++26 起为 constexpr) |
|
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) | (C++26 起为 constexpr) |
|
S)
SIMD 重载对
v_num
执行逐元素的
std::erf
运算。
|
(since C++26) |
|
A)
为所有整数类型提供了额外的重载,这些类型被视为
double
。
|
(since C++11) |
目录 |
参数
| num | - | 浮点数或整数值 |
返回值
If no errors occur, value of the error function of num , that is| 2 |
| √ π |
0 e -t 2
d t , is returned.
If a range error occurs due to underflow, the correct result (after rounding), that is
| 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 文档
for
erf
|
|
外部链接
| 魏斯坦, 埃里克·W. "误差函数." 摘自 MathWorld —— 一个 Wolfram 网络资源。 |