Erfc
定义于头文件 <cmath>
中。
描述
计算 `num` 的互补误差函数,即 `1.0 - std::erf(num)`,但对于大的 `num` 不会损失精度。
库为所有 cv 非限定浮点类型提供了 `std::erfc` 的重载,作为参数 `num` 的类型 (自 C++23 起)。
额外的重载为所有整数类型提供,它们被视为double。
声明
- C++23
- C++11
// 1)
/* floating-point-type */ erfc( /* floating-point-type */ num );
// 2)
float erfcf( float num );
// 3)
long double erfcl( long double num );
// 4)
template< class Integer >
double erfc ( Integer num );
// 1)
float erfc ( float num );
// 2)
double erfc ( double num );
//3)
long double erfc ( long double num );
// 4)
float erfcf( float num );
// 5)
long double erfcl( long double num );
// 6)
template< class Integer >
double erfc ( Integer num );
参数
num
- 浮点或整数值
返回值
如果未发生错误,则返回 num 的互补误差函数的值,即 `math here`。如果由于下溢发生范围错误,则返回正确的结果(四舍五入后)。
错误处理
错误按 math_errhandling 中指定的方式报告。
如果实现支持 IEEE 浮点运算(IEC 60559)
如果参数是 `+∞`,则返回 `+0`。如果参数是 `-∞`,则返回 `2`。如果参数是 NaN,则返回 NaN。
备注
对于 IEEE 兼容的 double 类型,如果 `num > 26.55`,则保证下溢。
不要求完全按照额外重载提供额外重载。它们只需足以确保对于其整数类型的参数 `num`,`std::erfc(num)` 与 `std::erfc(static_cast<double>(num))` 具有相同的效果。
示例
#include <cmath>
#include <iomanip>
#include <iostream>
double normalCDF(double x) // Phi(-∞, x) aka N(x)
{
return std::erfc(-x / std::sqrt(2)) / 2;
}
int main()
{
std::cout
<< "normal cumulative distribution function:\n"
<< std::fixed
<< std::setprecision(2);
for (double n = 0; n < 1; n += 0.1)
std::cout
<< "normalCDF(" << n << ") = "
<< 100 * normalCDF(n) << "%\n";
std::cout
<< "special values:\n"
<< "erfc(-Inf) = "
<< std::erfc(-INFINITY) << '\n'
<< "erfc(Inf) = "
<< std::erfc(INFINITY) << '\n';
}
normal cumulative distribution function:
normalCDF(0.00) = 50.00%
normalCDF(0.10) = 53.98%
normalCDF(0.20) = 57.93%
normalCDF(0.30) = 61.79%
normalCDF(0.40) = 65.54%
normalCDF(0.50) = 69.15%
normalCDF(0.60) = 72.57%
normalCDF(0.70) = 75.80%
normalCDF(0.80) = 78.81%
normalCDF(0.90) = 81.59%
normalCDF(1.00) = 84.13%
special values:
erfc(-Inf) = 2.00
erfc(Inf) = 0.00