summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Ffenv%2Ffeclearexcept.html
blob: 04dbc3eeb6d751ed3c2f150d1bf0a7a133af6c26 (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
    <h1 id="firstHeading" class="firstHeading">feclearexcept</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">int feclearexcept( int excepts );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  </table> <p>Attempts to clear the floating-point exceptions that are listed in the bitmask argument <code>excepts</code>, which is a bitwise OR of the <a href="fe_exceptions" title="c/numeric/fenv/FE exceptions">floating-point exception macros</a>.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> excepts </td> <td> - </td> <td> bitmask listing the exception flags to clear </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p><code>​0​</code> if all indicated exceptions were successfully cleared or if <code>excepts</code> is zero. Returns a non-zero value on error.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;fenv.h&gt;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;float.h&gt;
 
/*
 * A possible implementation of hypot which makes use of many advanced
 * floating-point features.
 */
double hypot_demo(double a, double b) {
  const int range_problem = FE_OVERFLOW | FE_UNDERFLOW;
  feclearexcept(range_problem);
  // try a fast algorithm
  double result = sqrt(a * a + b * b);
  if (!fetestexcept(range_problem))  // no overflow or underflow
    return result;                   // return the fast result
  // do a more complicated calculation to avoid overflow or underflow
  int a_exponent,b_exponent;
  frexp(a, &amp;a_exponent);
  frexp(b, &amp;b_exponent);
 
  if (a_exponent - b_exponent &gt; DBL_MAX_EXP)
    return fabs(a) + fabs(b);        // we can ignore the smaller value
  // scale so that fabs(a) is near 1
  double a_scaled = scalbn(a, -a_exponent);
  double b_scaled = scalbn(b, -a_exponent);
  // overflow and underflow is now impossible 
  result = sqrt(a_scaled * a_scaled + b_scaled * b_scaled);
  // undo scaling
  return scalbn(result, a_exponent);
}
 
int main(void)
{
  // Normal case takes the fast route
  printf("hypot(%f, %f) = %f\n", 3.0, 4.0, hypot_demo(3.0, 4.0));
  // Extreme case takes the slow but more accurate route
  printf("hypot(%e, %e) = %e\n", DBL_MAX / 2.0, 
                                DBL_MAX / 2.0, 
                                hypot_demo(DBL_MAX / 2.0, DBL_MAX / 2.0));
 
  return 0;
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">hypot(3.000000, 4.000000) = 5.000000
hypot(8.988466e+307, 8.988466e+307) = 1.271161e+308</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.6.2.1 The feclearexcept function (p: 209) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.6.2.1 The feclearexcept function (p: 190) </li></ul>
</ul>      <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fetestexcept" title="c/numeric/fenv/fetestexcept"> <span class="t-lines"><span>fetestexcept</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> determines which of the specified floating-point status flags are set <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/fenv/feclearexcept" title="cpp/numeric/fenv/feclearexcept">C++ documentation</a></span> for <code>feclearexcept</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/fenv/feclearexcept" class="_attribution-link">https://en.cppreference.com/w/c/numeric/fenv/feclearexcept</a>
  </p>
</div>