C++ 参考手册

位置:首页 > C++ 参考手册 >数值库 >常用数学函数 > std::erfc, std::erfcf, std::erfcl

定义于头文件 <cmath>
float       erfc ( float arg );
float       erfcf( float arg );
(1) (C++11 起)
double      erfc ( double arg );
(2) (C++11 起)
long double erfc ( long double arg );
long double erfcl( long double arg );
(3) (C++11 起)
double      erfc ( IntegralType arg );
(4) (C++11 起)
1-3) 计算 arg补误差函数,即 1.0-erf(arg) ,但对于大的 arg 无精度损失。
4) 接受任何整数类型参数的重载集或函数模板。等价于 (2) (参数被转型到 double )。

参数

arg - 浮点或整数类型

返回值

若不出现错误,则返回 arg 的补误差函数的值,即
2
π

arg
e-t2
dt
1-erf(arg)

若出现源于下溢的值域错误,则返回(舍入后的)正确结果。

错误处理

报告 math_errhandling 中指定的错误。

若实现支持 IEEE 浮点算术( IEC 60559 ),则

  • 若参数为 +∞ ,则返回 +0
  • 若参数为 -∞ ,则返回 2
  • 若参数为 NaN ,则返回 NaN

注解

对于 IEEE 兼容的 double 类型,若 arg > 26.55 则保证下溢。

示例

#include <iostream>
#include <cmath>
#include <iomanip>
double normalCDF(double x) // Phi(-∞, x) 又称为 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

参阅

(C++11)(C++11)(C++11)
误差函数
(函数)

外部链接

Weisstein, Eric W. "Erfc." 来自 MathWorld--A Wolfram Web Resource 。