blob: 55c9bb86a4b54d614c69a82ec89cd98c9fc31528 (
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
<h1 id="firstHeading" class="firstHeading">fegetenv, fesetenv</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><fenv.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">int fegetenv( fenv_t* envp );</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">int fesetenv( const fenv_t* envp );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> </table> <p>1) Attempts to store the status of the floating-point environment in the object pointed to by <code>envp</code>.</p>
<p>2) Attempts to establish the floating-point environment from the object pointed to by <code>envp</code>. The value of that object must be previously obtained by a call to <code><a href="feholdexcept" title="c/numeric/fenv/feholdexcept">feholdexcept</a></code> or <code>fegetenv</code> or be a floating-point macro constant. If any of the floating-point status flags are set in <code>envp</code>, they become set in the environment (and are then testable with <code><a href="fetestexcept" title="c/numeric/fenv/fetestexcept">fetestexcept</a></code>), but the corresponding floating-point exceptions are not raised (execution continues uninterrupted)</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> envp </td> <td> - </td> <td> pointer to the object of type <code>fenv_t</code> which holds the status of the floating-point environment </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p><code>0</code> on success, non-zero otherwise.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
#include <math.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
void show_fe_exceptions(void)
{
printf("current 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");
if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
printf("\n");
}
void show_fe_rounding_method(void)
{
printf("current rounding method: ");
switch (fegetround()) {
case FE_TONEAREST: printf ("FE_TONEAREST"); break;
case FE_DOWNWARD: printf ("FE_DOWNWARD"); break;
case FE_UPWARD: printf ("FE_UPWARD"); break;
case FE_TOWARDZERO: printf ("FE_TOWARDZERO"); break;
default: printf ("unknown");
};
printf("\n");
}
void show_fe_environment(void)
{
show_fe_exceptions();
show_fe_rounding_method();
}
int main(void)
{
fenv_t curr_env;
int rtn;
/* Show default environment. */
show_fe_environment();
printf("\n");
/* Perform some computation under default environment. */
printf("+11.5 -> %+4.1f\n", rint(+11.5)); /* midway between two integers */
printf("+12.5 -> %+4.1f\n", rint(+12.5)); /* midway between two integers */
show_fe_environment();
printf("\n");
/* Save current environment. */
rtn = fegetenv(&curr_env);
/* Perform some computation with new rounding method. */
feclearexcept(FE_ALL_EXCEPT);
fesetround(FE_DOWNWARD);
printf("1.0/0.0 = %f\n", 1.0/0.0);
printf("+11.5 -> %+4.1f\n", rint(+11.5));
printf("+12.5 -> %+4.1f\n", rint(+12.5));
show_fe_environment();
printf("\n");
/* Restore previous environment. */
rtn = fesetenv(&curr_env);
show_fe_environment();
return 0;
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">current exceptions raised: none
current rounding method: FE_TONEAREST
+11.5 -> +12.0
+12.5 -> +12.0
current exceptions raised: FE_INEXACT
current rounding method: FE_TONEAREST
1.0/0.0 = inf
+11.5 -> +11.0
+12.5 -> +12.0
current exceptions raised: FE_DIVBYZERO FE_INEXACT
current rounding method: FE_DOWNWARD
current exceptions raised: FE_INEXACT
current rounding method: FE_TONEAREST</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.6.4.1 The fegetenv function (p: 213) </li>
<li> 7.6.4.3 The fesetenv function (p: 214) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.6.4.1 The fegetenv function (p: 194) </li>
<li> 7.6.4.3 The fesetenv function (p: 195) </li>
</ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="feholdexcept" title="c/numeric/fenv/feholdexcept"> <span class="t-lines"><span>feholdexcept</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> saves the environment, clears all status flags and ignores all future errors <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="feupdateenv" title="c/numeric/fenv/feupdateenv"> <span class="t-lines"><span>feupdateenv</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> restores the floating-point environment and raises the previously raise exceptions <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="fe_dfl_env" title="c/numeric/fenv/FE DFL ENV"> <span class="t-lines"><span>FE_DFL_ENV</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> default floating-point environment <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/feenv" title="cpp/numeric/fenv/feenv">C++ documentation</a></span> for <code>fegetenv, fesetenv</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© 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/feenv" class="_attribution-link">https://en.cppreference.com/w/c/numeric/fenv/feenv</a>
</p>
</div>
|