summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Ffenv%2Ffe_exceptions.html
blob: aa31da7a76fb9ab48295307f272e2ab2611066f8 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    <h1 id="firstHeading" class="firstHeading"><small>FE_DIVBYZERO, FE_INEXACT, FE_INVALID, FE_OVERFLOW, FE_UNDERFLOW, FE_ALL_EXCEPT</small></h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;fenv.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define FE_DIVBYZERO    /*implementation defined power of 2*/</pre>
</td> <td class="t-dcl-nopad"> </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">#define FE_INEXACT      /*implementation defined power of 2*/</pre>
</td> <td class="t-dcl-nopad"> </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">#define FE_INVALID      /*implementation defined power of 2*/</pre>
</td> <td class="t-dcl-nopad"> </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">#define FE_OVERFLOW     /*implementation defined power of 2*/</pre>
</td> <td class="t-dcl-nopad"> </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">#define FE_UNDERFLOW    /*implementation defined power of 2*/</pre>
</td> <td class="t-dcl-nopad"> </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">#define FE_ALL_EXCEPT  FE_DIVBYZERO | FE_INEXACT | \
                       FE_INVALID | FE_OVERFLOW |  \
                       FE_UNDERFLOW</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  </table> <p>All these macro constants (except <code>FE_ALL_EXCEPT</code>) expand to integer constant expressions that are distinct powers of 2, which uniquely identify all supported floating-point exceptions. Each macro is only defined if it is supported.</p>
<p>The macro constant <code>FE_ALL_EXCEPT</code>, which expands to the bitwise OR of all other <code>FE_*</code>, is always defined and is zero if floating-point exceptions are not supported by the implementation.</p>
<table class="t-dsc-begin"> <tr class="t-dsc-hitem"> <th> Constant </th> <th> Explanation </th>
</tr> <tr class="t-dsc"> <td> <code>FE_DIVBYZERO</code> </td> <td> pole error occurred in an earlier floating-point operation </td>
</tr> <tr class="t-dsc"> <td> <code>FE_INEXACT</code> </td> <td> inexact result: rounding was necessary to store the result of an earlier floating-point operation </td>
</tr> <tr class="t-dsc"> <td> <code>FE_INVALID</code> </td> <td> domain error occurred in an earlier floating-point operation </td>
</tr> <tr class="t-dsc"> <td> <code>FE_OVERFLOW</code> </td> <td> the result of an earlier floating-point operation was too large to be representable </td>
</tr> <tr class="t-dsc"> <td> <code>FE_UNDERFLOW</code> </td> <td> the result of an earlier floating-point operation was subnormal with a loss of precision </td>
</tr> <tr class="t-dsc"> <td> <code>FE_ALL_EXCEPT</code> </td> <td> bitwise OR of all supported floating-point exceptions </td>
</tr> </table> <p>The implementation may define additional macro constants in <code>&lt;fenv.h&gt;</code> to identify additional floating-point exceptions. All such constants begin with <code>FE_</code> followed by at least one uppercase letter.</p>
<p>See <a href="../math/math_errhandling" title="c/numeric/math/math errhandling">math_errhandling</a> for further details.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;float.h&gt;
#include &lt;fenv.h&gt;
 
#pragma STDC FENV_ACCESS ON
void show_fe_exceptions(void)
{
    printf("exceptions raised:");
    if(fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))   printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))   printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))  printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW)) printf(" FE_UNDERFLOW");
    feclearexcept(FE_ALL_EXCEPT);
    printf("\n");
}
 
int main(void)
{
    printf("MATH_ERREXCEPT is %s\n",
           math_errhandling &amp; MATH_ERREXCEPT ? "set" : "not set");
 
    printf("0.0/0.0 = %f\n", 0.0/0.0);
    show_fe_exceptions();
 
    printf("1.0/0.0 = %f\n", 1.0/0.0);
    show_fe_exceptions();
 
    printf("1.0/10.0 = %f\n", 1.0/10.0);
    show_fe_exceptions();
 
    printf("sqrt(-1) = %f\n", sqrt(-1));
    show_fe_exceptions();
 
    printf("DBL_MAX*2.0 = %f\n", DBL_MAX*2.0);
    show_fe_exceptions();
 
    printf("nextafter(DBL_MIN/pow(2.0,52),0.0) = %.1f\n",
                      nextafter(DBL_MIN/pow(2.0,52),0.0));
    show_fe_exceptions();
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">MATH_ERREXCEPT is set
0.0/0.0 = nan
exceptions raised: FE_INVALID
1.0/0.0 = inf
exceptions raised: FE_DIVBYZERO
1.0/10.0 = 0.100000
exceptions raised: FE_INEXACT
sqrt(-1) = -nan
exceptions raised: FE_INVALID
DBL_MAX*2.0 = inf
exceptions raised: FE_INEXACT FE_OVERFLOW
nextafter(DBL_MIN/pow(2.0,52),0.0) = 0.0
exceptions raised: FE_INEXACT FE_UNDERFLOW</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.6/6 Floating-point environment &lt;fenv.h&gt; (p: 207) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.6/5 Floating-point environment &lt;fenv.h&gt; (p: 188) </li></ul>
</ul>      <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="../math/math_errhandling" title="c/numeric/math/math errhandling"> <span class="t-lines"><span>math_errhandling</span><span>MATH_ERRNO</span><span>MATH_ERREXCEPT</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> defines the error handling mechanism used by the common mathematical functions <br> <span class="t-mark">(macro constant)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/fenv/FE_exceptions" title="cpp/numeric/fenv/FE exceptions">C++ documentation</a></span> for <span class=""><span>floating-point exception macros</span></span> </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/fenv/FE_exceptions" class="_attribution-link">https://en.cppreference.com/w/c/numeric/fenv/FE_exceptions</a>
  </p>
</div>