summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Fmath_errhandling.html
blob: 49d3048dff2d3fde9ba170e14e2a768b928b8c94 (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
    <h1 id="firstHeading" class="firstHeading">MATH_ERRNO, MATH_ERREXCEPT, math_errhandling</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">#define MATH_ERRNO        1</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 MATH_ERREXCEPT    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 math_errhandling  /*implementation defined*/</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  </table> <p>The macro constant <code>math_errhandling</code> expands to an expression of type <code>int</code> that is either equal to <code>MATH_ERRNO</code>, or equal to <code>MATH_ERREXCEPT</code>, or equal to their bitwise OR (<code>MATH_ERRNO | MATH_ERREXCEPT</code>).</p>
<p>The value of <code>math_errhandling</code> indicates the type of error handling that is performed by the floating-point operators and <a href="../math" title="c/numeric/math">functions</a>:</p>
<table class="t-dsc-begin"> <tr class="t-dsc-hitem"> <th> Constant </th> <th> Explanation </th>
</tr> <tr class="t-dsc"> <td> <code>MATH_ERREXCEPT</code> </td> <td> indicates that floating-point exceptions are used: at least <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_DIVBYZERO</a></code>, <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code>, and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_OVERFLOW</a></code> are defined in <code>&lt;fenv.h&gt;</code>. </td>
</tr> <tr class="t-dsc"> <td> <code>MATH_ERRNO</code> </td> <td> indicates that floating-point operations use the variable <code><a href="../../error/errno" title="c/error/errno">errno</a></code> to report errors. </td>
</tr> </table> <p>If the implementation supports IEEE floating-point arithmetic (IEC 60559), <code>math_errhandling &amp; MATH_ERREXCEPT</code> is required to be non-zero.</p>
<p>The following floating-point error conditions are recognized:</p>
<table class="wikitable"> <tr> <th>Condition</th> <th>Explanation</th> <th>errno</th> <th>floating-point exception</th> <th>Example </th>
</tr> <tr> <td>Domain error </td> <td>the argument is outside the range in which the operation is mathematically defined (the description of <a href="../math" title="c/numeric/math">each function</a> lists the required domain errors) </td> <td>
<code><a href="../../error/errno_macros" title="c/error/errno macros">EDOM</a></code> </td> <td>
<code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> </td> <td>
<code><a href="http://en.cppreference.com/w/c/numeric/math/acos"><span class="kw673">acos</span></a><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span></code> </td>
</tr> <tr> <td>Pole error </td> <td>the mathematical result of the function is exactly infinite or undefined </td> <td>
<code><a href="../../error/errno_macros" title="c/error/errno macros">ERANGE</a></code> </td> <td>
<code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_DIVBYZERO</a></code> </td> <td>
<code><a href="http://en.cppreference.com/w/c/numeric/math/log"><span class="kw660">log</span></a><span class="br0">(</span><span class="nu16">0.0</span><span class="br0">)</span></code>, <code>1.0/0.0</code> </td>
</tr> <tr> <td>Range error due to overflow </td> <td>the mathematical result is finite, but becomes infinite after rounding, or becomes the largest representable finite value after rounding down </td> <td>
<code><a href="../../error/errno_macros" title="c/error/errno macros">ERANGE</a></code> </td> <td>
<code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_OVERFLOW</a></code> </td> <td>
<code><a href="http://en.cppreference.com/w/c/numeric/math/pow"><span class="kw668">pow</span></a><span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/limits"><span class="kw378">DBL_MAX</span></a>,<span class="nu0">2</span><span class="br0">)</span></code> </td>
</tr> <tr> <td>Range error due to underflow </td> <td>the result is non-zero, but becomes zero after rounding, or becomes subnormal with a loss of precision </td> <td>
<code><a href="../../error/errno_macros" title="c/error/errno macros">ERANGE</a></code> or unchanged (implementation-defined) </td> <td>
<code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_UNDERFLOW</a></code> or nothing (implementation-defined) </td> <td>
<code>DBL_TRUE_MIN/2</code> </td>
</tr> <tr> <td>Inexact result </td> <td>the result has to be rounded to fit in the destination type </td> <td>unchanged </td> <td>
<code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> or nothing (unspecified) </td> <td>
<code><a href="http://en.cppreference.com/w/c/numeric/math/sqrt"><span class="kw665">sqrt</span></a><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span></code>, <code>1.0/10.0</code> </td>
</tr>
</table>  <h3 id="Notes"> Notes</h3> <p>Whether <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> is raised by the mathematical library functions is unspecified in general, but may be explicitly specified in the description of the function (e.g. <code><a href="rint" title="c/numeric/math/rint">rint</a></code> vs <code><a href="nearbyint" title="c/numeric/math/nearbyint">nearbyint</a></code>).</p>
<p>Before C99, floating-point exceptions were not specified, <code><a href="http://en.cppreference.com/w/c/error/errno_macros"><span class="kw426">EDOM</span></a></code> was required for any domain error, <code><a href="http://en.cppreference.com/w/c/error/errno_macros"><span class="kw478">ERANGE</span></a></code> was required for overflows and implementation-defined for underflows.</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;fenv.h&gt;
#include &lt;math.h&gt;
#include &lt;errno.h&gt;
#pragma STDC FENV_ACCESS ON
int main(void)
{
    printf("MATH_ERRNO is %s\n", math_errhandling &amp; MATH_ERRNO ? "set" : "not set");
    printf("MATH_ERREXCEPT is %s\n",
           math_errhandling &amp; MATH_ERREXCEPT ? "set" : "not set");
    feclearexcept(FE_ALL_EXCEPT);
    errno = 0;
    printf("log(0) = %f\n", log(0));
    if(errno == ERANGE)
        perror("errno == ERANGE");
    if(fetestexcept(FE_DIVBYZERO))
        puts("FE_DIVBYZERO (pole error) reported");
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">MATH_ERRNO is set
MATH_ERREXCEPT is set
log(0) = -inf
errno = ERANGE: Numerical result out of range
FE_DIVBYZERO (pole error) reported</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.12/9 MATH_ERRNO, MATH_ERREXCEPT, math_errhandling (p: 170) </li>
<li> F.10/4 MATH_ERREXCEPT, math_errhandling (p: 377) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12/9 MATH_ERRNO, MATH_ERREXCEPT, math_errhandling (p: 233) </li>
<li> F.10/4 MATH_ERREXCEPT, math_errhandling (p: 517) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12/9 MATH_ERRNO, MATH_ERREXCEPT, math_errhandling (p: 214) </li>
<li> F.9/4 MATH_ERREXCEPT, math_errhandling&gt; (p: 454) </li>
</ul>
</ul>              <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions"> <span class="t-lines"><span>FE_ALL_EXCEPT</span><span>FE_DIVBYZERO</span><span>FE_INEXACT</span><span>FE_INVALID</span><span>FE_OVERFLOW</span><span>FE_UNDERFLOW</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> floating-point exceptions <br> <span class="t-mark">(macro constant)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../../error/errno" title="c/error/errno"> <span class="t-lines"><span>errno</span></span></a></div> </td> <td> macro which expands to POSIX-compatible thread-local error number variable<br><span class="t-mark">(macro variable)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/math/math_errhandling" title="cpp/numeric/math/math errhandling">C++ documentation</a></span> for <code>math_errhandling</code> </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/math_errhandling" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/math_errhandling</a>
  </p>
</div>