std::erf, std::erff, std::erfl
來自 cppreference.com
定義於標頭檔案 <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< /*數學浮點型別*/ V > constexpr /*推導 SIMD 型別*/<V> |
(S) | (C++26 起) |
額外過載 (自 C++11 起) |
||
定義於標頭檔案 <cmath> |
||
template< class Integer > double erf ( Integer num ); |
(A) | (C++26 起為 constexpr) |
S) SIMD 過載在 v_num 上執行元素級別的
std::erf 。
|
(C++26 起) |
A) 為所有整數型別提供了額外的過載,它們被視為 double。
|
(C++11 起) |
目錄 |
[編輯] 引數
num | - | 浮點值或整數值 |
[編輯] 返回值
如果沒有錯誤發生,返回 num 的誤差函式值,即2 |
√π |
0e-t2
dt。
如果由於下溢發生範圍錯誤,返回正確的結果(舍入後),即
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 文件 用於 erf
|
[編輯] 外部連結
Weisstein, Eric W. "Erf." 來自 MathWorld — Wolfram Web 資源。 |