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
|
<h1 id="firstHeading" class="firstHeading">nearbyint, nearbyintf, nearbyintl</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><math.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">float nearbyintf( float arg );</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">double nearbyint( double arg );</pre>
</td> <td> (2) </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">long double nearbyintl( long double arg );</pre>
</td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dsc-header"> <th> Defined in header <code><tgmath.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define nearbyint( arg )</pre>
</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> </table> <div class="t-li1">
<span class="t-li">1-3)</span> Rounds the floating-point argument <code>arg</code> to an integer value in floating-point format, using the <a href="../fenv/fe_round" title="c/numeric/fenv/FE round">current rounding mode</a>.</div> <div class="t-li1">
<span class="t-li">4)</span> Type-generic macro: If <code>arg</code> has type <code>long double</code>, <code>nearbyintl</code> is called. Otherwise, if <code>arg</code> has integer type or the type <code>double</code>, <code>nearbyint</code> is called. Otherwise, <code>nearbyintf</code> is called, respectively.</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> arg </td> <td> - </td> <td> floating point value </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>The nearest integer value to <code>arg</code>, according to the <a href="../fenv/fe_round" title="c/numeric/fenv/FE round">current rounding mode</a>, is returned.</p>
<h3 id="Error_handling"> Error handling</h3> <p>This function is not subject to any of the errors specified in <a href="math_errhandling" title="c/numeric/math/math errhandling">math_errhandling</a>.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> is never raised </li>
<li> If <code>arg</code> is ±∞, it is returned, unmodified </li>
<li> If <code>arg</code> is ±0, it is returned, unmodified </li>
<li> If <code>arg</code> is NaN, NaN is returned </li>
</ul> <h3 id="Notes"> Notes</h3> <p>The only difference between <code>nearbyint</code> and <code><a href="rint" title="c/numeric/math/rint">rint</a></code> is that <code>nearbyint</code> never raises <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code>.</p>
<p>The largest representable floating-point values are exact integers in all standard floating-point formats, so <code>nearbyint</code> never overflows on its own; however the result may overflow any integer type (including <code><a href="../../types/integer" title="c/types/integer">intmax_t</a></code>), when stored in an integer variable.</p>
<p>If the current rounding mode is <code><a href="../fenv/fe_round" title="c/numeric/fenv/FE round">FE_TONEAREST</a></code>, this function rounds to even in halfway cases (like <code><a href="rint" title="c/numeric/math/rint">rint</a></code>, but unlike <code><a href="round" title="c/numeric/math/round">round</a></code>).</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>
int main(void)
{
#pragma STDC FENV_ACCESS ON
fesetround(FE_TONEAREST);
printf("rounding to nearest:\nnearbyint(+2.3) = %+.1f ", nearbyint(2.3));
printf("nearbyint(+2.5) = %+.1f ", nearbyint(2.5));
printf("nearbyint(+3.5) = %+.1f\n", nearbyint(3.5));
printf("nearbyint(-2.3) = %+.1f ", nearbyint(-2.3));
printf("nearbyint(-2.5) = %+.1f ", nearbyint(-2.5));
printf("nearbyint(-3.5) = %+.1f\n", nearbyint(-3.5));
fesetround(FE_DOWNWARD);
printf("rounding down: \nnearbyint(+2.3) = %+.1f ", nearbyint(2.3));
printf("nearbyint(+2.5) = %+.1f ", nearbyint(2.5));
printf("nearbyint(+3.5) = %+.1f\n", nearbyint(3.5));
printf("nearbyint(-2.3) = %+.1f ", nearbyint(-2.3));
printf("nearbyint(-2.5) = %+.1f ", nearbyint(-2.5));
printf("nearbyint(-3.5) = %+.1f\n", nearbyint(-3.5));
printf("nearbyint(-0.0) = %+.1f\n", nearbyint(-0.0));
printf("nearbyint(-Inf) = %+.1f\n", nearbyint(-INFINITY));
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">rounding to nearest:
nearbyint(+2.3) = +2.0 nearbyint(+2.5) = +2.0 nearbyint(+3.5) = +4.0
nearbyint(-2.3) = -2.0 nearbyint(-2.5) = -2.0 nearbyint(-3.5) = -4.0
rounding down:
nearbyint(+2.3) = +2.0 nearbyint(+2.5) = +2.0 nearbyint(+3.5) = +3.0
nearbyint(-2.3) = -3.0 nearbyint(-2.5) = -3.0 nearbyint(-3.5) = -4.0
nearbyint(-0.0) = -0.0
nearbyint(-Inf) = -inf</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.9.3 The nearbyint functions (p: 251-252) </li>
<li> 7.25 Type-generic math <tgmath.h> (p: 373-375) </li>
<li> F.10.6.3 The nearbyint functions (p: 526) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.9.3 The nearbyint functions (p: 232) </li>
<li> 7.22 Type-generic math <tgmath.h> (p: 335-337) </li>
<li> F.9.6.3 The nearbyint functions (p: 463) </li>
</ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="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> <div><a href="round" title="c/numeric/math/round"> <span class="t-lines"><span>round</span><span>roundf</span><span>roundl</span><span>lround</span><span>lroundf</span><span>lroundl</span><span>llround</span><span>llroundf</span><span>llroundl</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 nearest integer, rounding away from zero in halfway cases <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../fenv/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/math/nearbyint" title="cpp/numeric/math/nearbyint">C++ documentation</a></span> for <code>nearbyint</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/math/nearbyint" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/nearbyint</a>
</p>
</div>
|