diff options
Diffstat (limited to 'devdocs/c/numeric%2Ffenv%2Ffe_round.html')
| -rw-r--r-- | devdocs/c/numeric%2Ffenv%2Ffe_round.html | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/devdocs/c/numeric%2Ffenv%2Ffe_round.html b/devdocs/c/numeric%2Ffenv%2Ffe_round.html new file mode 100644 index 00000000..716eed9d --- /dev/null +++ b/devdocs/c/numeric%2Ffenv%2Ffe_round.html @@ -0,0 +1,71 @@ + <h1 id="firstHeading" class="firstHeading">FE_DOWNWARD, FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD</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">#define FE_DOWNWARD /*implementation defined*/</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_TONEAREST /*implementation defined*/</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_TOWARDZERO /*implementation defined*/</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_UPWARD /*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>Each of these macro constants expands to a nonnegative integer constant expression, which can be used with <code><a href="feround" title="c/numeric/fenv/feround">fesetround</a></code> and <code><a href="feround" title="c/numeric/fenv/feround">fegetround</a></code> to indicate one of the supported floating-point rounding modes. The implementation may define additional rounding mode constants in <a href="../fenv" title="c/numeric/fenv"><code><fenv.h></code></a>, which should all begin with <code>FE_</code> followed by at least one uppercase letter. Each macro is only defined if it is supported.</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_DOWNWARD</code> </td> <td> rounding towards negative infinity </td> +</tr> <tr class="t-dsc"> <td> <code>FE_TONEAREST</code> </td> <td> rounding towards nearest representable value </td> +</tr> <tr class="t-dsc"> <td> <code>FE_TOWARDZERO</code> </td> <td> rounding towards zero </td> +</tr> <tr class="t-dsc"> <td> <code>FE_UPWARD</code> </td> <td> rounding towards positive infinity </td> +</tr> </table> <p>Additional rounding modes may be supported by an implementation.</p> +<p>The current rounding mode affects the following:</p> +<ul><li> results of floating-point arithmetic operators outside of constant expressions </li></ul> <div class="c source-c"><pre data-language="c">double x = 1; +x / 10; // 0.09999999999999999167332731531132594682276248931884765625 or + // 0.1000000000000000055511151231257827021181583404541015625</pre></div> <ul><li> results of standard library <a href="../math" title="c/numeric/math">mathematical functions</a> </li></ul> <div class="c source-c"><pre data-language="c">sqrt(2); // 1.41421356237309492343001693370752036571502685546875 or + // 1.4142135623730951454746218587388284504413604736328125</pre></div> <ul><li> floating-point to floating-point implicit conversion and casts </li></ul> <div class="c source-c"><pre data-language="c">double d = 1 + DBL_EPSILON; +float f = d; // 1.00000000000000000000000 or + // 1.00000011920928955078125</pre></div> <ul><li> string conversions such as <code><a href="../../string/byte/strtof" title="c/string/byte/strtof">strtod</a></code> or <code><a href="../../io/fprintf" title="c/io/fprintf">printf</a></code> </li></ul> <div class="c source-c"><pre data-language="c">strtof("0.1", NULL); // 0.0999999940395355224609375 or + // 0.100000001490116119384765625</pre></div> <ul><li> the library rounding functions <code><a href="../math/nearbyint" title="c/numeric/math/nearbyint">nearbyint</a></code>, <code><a href="../math/rint" title="c/numeric/math/rint">rint</a></code>, <code><a href="../math/rint" title="c/numeric/math/rint">lrint</a></code> </li></ul> <div class="c source-c"><pre data-language="c">lrint(2.1); // 2 or 3</pre></div> <p>The current rounding mode does NOT affect the following:</p> +<ul> +<li> floating-point to integer implicit conversion and casts (always towards zero) </li> +<li> results of floating-point arithmetic operators in constant expressions executed at compile time (always to nearest) </li> +<li> the library functions <code><a href="../math/round" title="c/numeric/math/round">round</a></code>, <code><a href="../math/round" title="c/numeric/math/round">lround</a></code>, <code><a href="../math/round" title="c/numeric/math/round">llround</a></code>, <code><a href="../math/ceil" title="c/numeric/math/ceil">ceil</a></code>, <code><a href="../math/floor" title="c/numeric/math/floor">floor</a></code>, <code><a href="../math/trunc" title="c/numeric/math/trunc">trunc</a></code> </li> +</ul> <p>As with any <a href="../fenv" title="c/numeric/fenv">floating-point environment</a> functionality, rounding is only guaranteed if <code>#pragma STDC FENV_ACCESS ON</code> is set.</p> +<p>Compilers that do not support the pragma may offer their own ways to support current rounding mode. For example Clang and GCC have the option <code>-frounding-math</code> intended to disable optimizations that would change the meaning of rounding-sensitive 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> +#include <stdlib.h> + +// #pragma STDC FENV_ACCESS ON + +int main() +{ + fesetround(FE_DOWNWARD); + puts("rounding down: "); + printf(" pi = %.22f\n", acosf(-1)); + printf("strtof(\"1.1\") = %.22f\n", strtof("1.1", NULL)); + printf(" rint(2.1) = %.22f\n\n", rintf(2.1)); + fesetround(FE_UPWARD); + puts("rounding up: "); + printf(" pi = %.22f\n", acosf(-1)); + printf("strtof(\"1.1\") = %.22f\n", strtof("1.1", NULL)); + printf(" rint(2.1) = %.22f\n", rintf(2.1)); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">rounding down: + pi = 3.1415925025939941406250 +strtof("1.1") = 1.0999999046325683593750 + rint(2.1) = 2.0000000000000000000000 + +rounding up: + pi = 3.1415927410125732421875 +strtof("1.1") = 1.1000000238418579101563 + rint(2.1) = 3.0000000000000000000000</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C23 standard (ISO/IEC 9899:2023): </li> +<ul><li> 7.6/8 Floating-point environment <fenv.h> (p: TBD) </li></ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 7.6/8 Floating-point environment <fenv.h> (p: 151) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.6/8 Floating-point environment <fenv.h> (p: 207) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.6/7 Floating-point environment <fenv.h> (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="feround" title="c/numeric/fenv/feround"> <span class="t-lines"><span>fegetround</span><span>fesetround</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></div> </td> <td> gets or sets rounding direction <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/FE_round" title="cpp/numeric/fenv/FE round">C++ documentation</a></span> for <span class=""><span>floating-point rounding macros</span></span> </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/FE_round" class="_attribution-link">https://en.cppreference.com/w/c/numeric/fenv/FE_round</a> + </p> +</div> |
