blob: 7bbe06b49d4703222413e0e12e84c78bfb3882b0 (
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
|
<h1 id="firstHeading" class="firstHeading">fegetround, fesetround</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 fesetround( int round );</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 fegetround();</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> </table> <p>1) Attempts to establish the floating-point rounding direction equal to the argument <code>round</code>, which is expected to be one of the <a href="fe_round" title="c/numeric/fenv/FE round">floating-point rounding macros</a>.</p>
<p>2) Returns the value of the <a href="fe_round" title="c/numeric/fenv/FE round">floating-point rounding macro</a> that corresponds to the current rounding direction.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> round </td> <td> - </td> <td> rounding direction, one of <a href="fe_round" title="c/numeric/fenv/FE round">floating-point rounding macros</a> </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>1) <code>0</code> on success, non-zero otherwise.</p>
<p>2) the <a href="fe_round" title="c/numeric/fenv/FE round">floating-point rounding macro</a> describing the current rounding direction or a negative value if the direction cannot be determined.</p>
<h3 id="Notes"> Notes</h3> <p>The current rounding mode, reflecting the effects of the most recent <code>fesetround</code>, can also be queried with <code><a href="../../types/limits/flt_rounds" title="c/types/limits/FLT ROUNDS">FLT_ROUNDS</a></code>.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <fenv.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
void show_fe_current_rounding_direction(void)
{
printf("current rounding direction: ");
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");
}
int main(void)
{
/* Default rounding direction */
show_fe_current_rounding_direction();
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 */
/* Save current rounding direction. */
int curr_direction = fegetround();
/* Temporarily change current rounding direction. */
fesetround(FE_DOWNWARD);
show_fe_current_rounding_direction();
printf("+11.5 -> %+4.1f\n", rint(+11.5));
printf("+12.5 -> %+4.1f\n", rint(+12.5));
/* Restore default rounding direction. */
fesetround(curr_direction);
show_fe_current_rounding_direction();
return 0;
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">current rounding direction: FE_TONEAREST
+11.5 -> +12.0
+12.5 -> +12.0
current rounding direction: FE_DOWNWARD
+11.5 -> +11.0
+12.5 -> +12.0
current rounding direction: FE_TONEAREST</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul>
<li> 7.6.3.1 The fegetround function (p: TBD) </li>
<li> 7.6.3.2 The fesetround function (p: TBD) </li>
</ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.6.3.1 The fegetround function (p: TBD) </li>
<li> 7.6.3.2 The fesetround function (p: TBD) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.6.3.1 The fegetround function (p: 212) </li>
<li> 7.6.3.2 The fesetround function (p: 212-213) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.6.3.1 The fegetround function (p: 193) </li>
<li> 7.6.3.2 The fesetround function (p: 193-194) </li>
</ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="../math/nearbyint" title="c/numeric/math/nearbyint"> <span class="t-lines"><span>nearbyint</span><span>nearbyintf</span><span>nearbyintl</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> rounds to an integer using current rounding mode <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../math/rint" title="c/numeric/math/rint"> <span class="t-lines"><span>rint</span><span>rintf</span><span>rintl</span><span>lrint</span><span>lrintf</span><span>lrintl</span><span>llrint</span><span>llrintf</span><span>llrintl</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><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><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> rounds to an integer using current rounding mode with <br> exception if the result differs <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/feround" title="cpp/numeric/fenv/feround">C++ documentation</a></span> for <code>fegetround, fesetround</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/feround" class="_attribution-link">https://en.cppreference.com/w/c/numeric/fenv/feround</a>
</p>
</div>
|