Namespaces
Variants

std:: erf, std:: erff, std:: erfl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float erf ( float num ) ;

double erf ( double num ) ;

long double erf ( long 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 >

erf ( const V & v_num ) ;
(S) (C++26 起)
附加重载 (C++11 起)
定义于头文件 <cmath>
template < class Integer >
double erf ( Integer num ) ;
(A) (C++26 起为 constexpr)
1-3) 计算 num 误差函数 标准库为所有 cv 未限定浮点类型提供了 std::erf 的重载版本作为参数类型。 (C++23 起)
S) SIMD 重载对 v_num 执行逐元素的 std::erf 运算。
(定义参见 math-floating-point deduced-simd-t 。)
(since C++26)
A) 为所有整数类型提供了额外的重载,这些类型被视为 double
(since C++11)

目录

参数

num - 浮点数或整数值

返回值

If no errors occur, value of the error function of num , that is
2
π
num
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
π
is returned.

错误处理

错误报告方式遵循 math_errhandling 中的规范。

如果实现支持 IEEE 浮点算术 (IEC 60559),

  • 若参数为 ±0,则返回 ±0。
  • 若参数为 ±∞,则返回 ±1。
  • 若参数为 NaN,则返回 NaN。

注释

| num | < DBL_MIN * ( std:: sqrt ( π ) / 2 ) 时,保证会发生下溢。

erf(
x
σ 2
) is the probability that a measurement whose errors are subject to a normal distribution with standard deviation σ is less than x away from the mean value.

额外的重载不需要完全按照 (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 网络资源。