Erf
定义于头文件 <cmath>
中。
描述
计算 num
的误差函数。
库为所有 cv-unqualified 浮点类型提供了 std::erf 的重载,作为参数 num
的类型 (从 C++23 开始)。
还为所有整数类型提供了额外的重载,这些整数类型被视为 double。
声明
- C++23
- C++11
// 1)
/* floating-point-type */ erf( /* floating-point-type */ num );
// 2)
float erff( float num );
// 3)
long double erfl( long double num );
// 4)
template< class Integer >
double erf ( Integer num );
// 1)
float erf ( float num );
// 2)
double erf ( double num );
//3)
long double erf ( long double num );
// 4)
float erff( float num );
// 5)
long double erfl( long double num );
// 6)
template< class Integer >
double erf ( Integer num );
参数
num
- 浮点或整数值
返回值
如果没有发生错误,则返回 num
的误差函数的值,即 math here
。如果由于下溢发生范围错误,则返回正确的结果(舍入后),即 math here
。
错误处理
错误按 math_errhandling 中指定的方式报告。
如果实现支持 IEEE 浮点运算(IEC 60559)
如果参数是 ±0
,返回 ±0
。如果参数是 ±∞
,返回 ±1
。如果参数是 NaN,返回 NaN
备注
如果 |num| < DBL_MIN * (std::sqrt(π)/2)
,则保证下溢
math here
是测量误差服从标准差 σ 的正态分布时,测量值与平均值相差小于 x 的概率。不要求额外重载完全按照附加重载提供。它们只需要足以确保对于其整数类型的参数 num
,std::erf(num)
与 std::erf(static_cast<double>(num))
具有相同的效果。
示例
#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