summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Ferf.html
blob: a4223e042b097c814fed1c673af09a35eb91262c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    <h1 id="firstHeading" class="firstHeading">erf, erff, erfl</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;math.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">float       erff( float arg );</pre>
</td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">double      erf( double arg );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long double erfl( long double arg );</pre>
</td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;tgmath.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define erf( arg )</pre>
</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  </table> <div class="t-li1">
<span class="t-li">1-3)</span> Computes the <a href="https://en.wikipedia.org/wiki/Error_function" class="extiw" title="enwiki:Error function">error function</a> of <code>arg</code>.</div> <div class="t-li1">
<span class="t-li">4)</span> Type-generic macro: If <code>arg</code> has type <code>long double</code>, <code>erfl</code> is called. Otherwise, if <code>arg</code> has integer type or the type <code>double</code>, <code>erf</code> is called. Otherwise, <code>erff</code> is called.</div>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> arg </td> <td> - </td> <td> floating point value </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> If no errors occur, value of the error function of <code>arg</code>, that is \(\frac{2}{\sqrt{\pi} }\int_{0}^{arg}{e^{-{t^2} }\mathsf{d}t}\)<span><span>2</span><span>/</span><span>√π</span></span>∫<sub class="t-su t-su-b">arg0</sub><i>e</i><sup>-t<sup class="t-su">2</sup></sup>d<i>t</i>, is returned. If a range error occurs due to underflow, the correct result (after rounding), that is \(\frac{2\cdot arg}{\sqrt{\pi} }\)<span><span>2*arg</span><span>/</span><span>√π</span></span>, is returned. <h3 id="Error_handling"> Error handling</h3> <p>Errors are reported as specified in <a href="math_errhandling" title="c/numeric/math/math errhandling"><code>math_errhandling</code></a>.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> If the argument is ±0, ±0 is returned </li>
<li> If the argument is ±∞, ±1 is returned </li>
<li> If the argument is NaN, NaN is returned </li>
</ul> <h3 id="Notes"> Notes</h3> <p>Underflow is guaranteed if <code><span class="sy3">|</span>arg<span class="sy3">|</span> <span class="sy1">&lt;</span> <a href="http://en.cppreference.com/w/c/types/limits"><span class="kw375">DBL_MIN</span></a><span class="sy2">*</span><span class="br0">(</span><a href="http://en.cppreference.com/w/c/numeric/math/sqrt"><span class="kw665">sqrt</span></a><span class="br0">(</span>π<span class="br0">)</span><span class="sy2">/</span><span class="nu0">2</span><span class="br0">)</span></code>.  \(\operatorname{erf}(\frac{x}{\sigma \sqrt{2} })\)erf(</p>
<span><span>x</span><span>/</span><span>σ√2</span></span>) is the probability that a measurement whose errors are subject to a normal distribution with standard deviation \(\sigma\)σ is less than \(x\)x away from the mean value. <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;math.h&gt;
#include &lt;stdio.h&gt;
 
double phi(double x1, double x2)
{
    return (erf(x2 / sqrt(2)) - erf(x1 / sqrt(2))) / 2;
}
 
int main(void)
{
    puts("normal variate probabilities:");
    for (int n = -4; n &lt; 4; ++n)
        printf("[%2d:%2d]: %5.2f%%\n", n, n + 1, 100 * phi(n, n + 1));
 
    puts("special values:");
    printf("erf(-0) = %f\n", erf(-0.0));
    printf("erf(Inf) = %f\n", erf(INFINITY));
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">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.000000
erf(Inf) = 1.000000</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.8.1 The erf functions (p: 249) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.5.1 The erf functions (p: 525) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.8.1 The erf functions (p: 230) </li>
<li> 7.22 Type-generic math &lt;tgmath.h&gt; (p: 335-337) </li>
<li> F.9.5.1 The erf functions (p: 462) </li>
</ul>
</ul>           <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="erfc" title="c/numeric/math/erfc"> <span class="t-lines"><span>erfc</span><span>erfcf</span><span>erfcl</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes complementary error function <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/math/erf" title="cpp/numeric/math/erf">C++ documentation</a></span> for <code>erf</code> </td>
</tr> </table> <h3 id="External_links"> External links</h3> <table> <tr> <td>
<a rel="nofollow" class="external text" href="https://mathworld.wolfram.com/Erf.html">Weisstein, Eric W. "Erf."</a> From MathWorld — A Wolfram Web Resource. </td>
</tr>
</table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/numeric/math/erf" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/erf</a>
  </p>
</div>