summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Frint.html
blob: cd195c3e011f75b4f47ec08f8f735c43386e2602 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    <h1 id="firstHeading" class="firstHeading">rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;math.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">float rintf( 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 rint( 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 rintl( 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>&lt;tgmath.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define rint( arg )</pre>
</td> <td> (4) </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>&lt;math.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long lrintf( float arg );</pre>
</td> <td> (5) </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 lrint( double arg );</pre>
</td> <td> (6) </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 lrintl( long double arg );</pre>
</td> <td> (7) </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>&lt;tgmath.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define lrint( arg )</pre>
</td> <td> (8) </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>&lt;math.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long long llrintf( float arg );</pre>
</td> <td> (9) </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 long llrint( double arg );</pre>
</td> <td> (10) </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 long llrintl( long double arg );</pre>
</td> <td> (11) </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>&lt;tgmath.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define llrint( arg )</pre>
</td> <td> (12) </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 current rounding mode.</div> <div class="t-li1">
<span class="t-li">5-7, 9-11)</span> Rounds the floating-point argument <code>arg</code> to an integer value in integer format, using the current rounding mode.</div> <div class="t-li1">
<span class="t-li">4,8,12)</span> Type-generic macros: If <code>arg</code> has type <code>long double</code>, <code>rintl</code>, <code>lrintl</code>, <code>llrintl</code> is called. Otherwise, if <code>arg</code> has integer type or the type <code>double</code>, <code>rint</code>, <code>lrint</code>, <code>llrint</code> is called. Otherwise, <code>rintf</code>, <code>lrintf</code>, <code>llrintf</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>If no errors occur, 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>Errors are reported as specified in <a href="math_errhandling" title="c/numeric/math/math errhandling"><code>math_errhandling</code></a>.</p>
<p>If the result of <code>lrint</code> or <code>llrint</code> is outside the range representable by the return type, a domain error or a range error may occur.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),   For the <code>rint</code> function:</p>
<ul>
<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>  For <code>lrint</code> and <code>llrint</code> functions:  <ul>
<li> If <code>arg</code> is ±∞, <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised and an implementation-defined value is returned </li>
<li> If the result of the rounding is outside the range of the return type, <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised and an implementation-defined value is returned </li>
<li> If <code>arg</code> is NaN, <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised and an implementation-defined value is returned </li>
</ul> <h3 id="Notes"> Notes</h3> <p><a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/lrint.html">POSIX specifies</a> that all cases where <code>lrint</code> or <code>llrint</code> raise <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> are domain errors.</p>
<p>As specified in <a href="math_errhandling" title="c/numeric/math/math errhandling"><code>math_errhandling</code></a>, <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> may be (but isn't required to be on non-IEEE floating-point platforms) raised by <code>rint</code> when rounding a non-integer finite value.</p>
<p>The only difference between <code>rint</code> and <code><a href="nearbyint" title="c/numeric/math/nearbyint">nearbyint</a></code> is that <code><a href="nearbyint" title="c/numeric/math/nearbyint">nearbyint</a></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>rint</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...</p>
<ul>
<li> <code><a href="../fenv/fe_round" title="c/numeric/fenv/FE round">FE_DOWNWARD</a></code>, then <code>rint</code> is equivalent to <code><a href="floor" title="c/numeric/math/floor">floor</a></code>. </li>
<li> <code><a href="../fenv/fe_round" title="c/numeric/fenv/FE round">FE_UPWARD</a></code>, then <code>rint</code> is equivalent to <code><a href="ceil" title="c/numeric/math/ceil">ceil</a></code>. </li>
<li> <code><a href="../fenv/fe_round" title="c/numeric/fenv/FE round">FE_TOWARDZERO</a></code>, then <code>rint</code> is equivalent to <code><a href="trunc" title="c/numeric/math/trunc">trunc</a></code> </li>
<li> <code><a href="../fenv/fe_round" title="c/numeric/fenv/FE round">FE_TONEAREST</a></code>, then <code>rint</code> differs from <code><a href="round" title="c/numeric/math/round">round</a></code> in that halfway cases are rounded to even rather than away from zero. </li>
</ul> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;fenv.h&gt;
#include &lt;limits.h&gt;
 
int main(void)
{
#pragma STDC FENV_ACCESS ON
    fesetround(FE_TONEAREST);
    printf("rounding to nearest (halfway cases to even):\n"
           "rint(+2.3) = %+.1f  ", rint(2.3));
    printf("rint(+2.5) = %+.1f  ", rint(2.5));
    printf("rint(+3.5) = %+.1f\n", rint(3.5));
    printf("rint(-2.3) = %+.1f  ", rint(-2.3));
    printf("rint(-2.5) = %+.1f  ", rint(-2.5));
    printf("rint(-3.5) = %+.1f\n", rint(-3.5));
 
    fesetround(FE_DOWNWARD);
    printf("rounding down: \nrint(+2.3) = %+.1f  ", rint(2.3));
    printf("rint(+2.5) = %+.1f  ", rint(2.5));
    printf("rint(+3.5) = %+.1f\n", rint(3.5));
    printf("rint(-2.3) = %+.1f  ", rint(-2.3));
    printf("rint(-2.5) = %+.1f  ", rint(-2.5));
    printf("rint(-3.5) = %+.1f\n", rint(-3.5));
    printf("rounding down with lrint: \nlrint(+2.3) = %ld  ", lrint(2.3));
    printf("lrint(+2.5) = %ld  ", lrint(2.5));
    printf("lrint(+3.5) = %ld\n", lrint(3.5));
    printf("lrint(-2.3) = %ld  ", lrint(-2.3));
    printf("lrint(-2.5) = %ld  ", lrint(-2.5));
    printf("lrint(-3.5) = %ld\n", lrint(-3.5));
 
    printf("lrint(-0.0) = %ld\n", lrint(-0.0));
    printf("lrint(-Inf) = %ld\n", lrint(-INFINITY)); // FE_INVALID raised
 
    // error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("rint(1.1) = %.1f\n", rint(1.1));
    if(fetestexcept(FE_INEXACT)) puts("    FE_INEXACT was raised");
 
    feclearexcept(FE_ALL_EXCEPT);
    printf("lrint(LONG_MIN-2048.0) = %ld\n", lrint(LONG_MIN-2048.0));
    if(fetestexcept(FE_INVALID)) puts("    FE_INVALID was raised");
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">rounding to nearest (halfway cases to even):
rint(+2.3) = +2.0  rint(+2.5) = +2.0  rint(+3.5) = +4.0
rint(-2.3) = -2.0  rint(-2.5) = -2.0  rint(-3.5) = -4.0
rounding down: 
rint(+2.3) = +2.0  rint(+2.5) = +2.0  rint(+3.5) = +3.0
rint(-2.3) = -3.0  rint(-2.5) = -3.0  rint(-3.5) = -4.0
rounding down with lrint: 
lrint(+2.3) = 2  lrint(+2.5) = 2  lrint(+3.5) = 3
lrint(-2.3) = -3  lrint(-2.5) = -3  lrint(-3.5) = -4
lrint(-0.0) = 0
lrint(-Inf) = -9223372036854775808
rint(1.1) = 1.0
    FE_INEXACT was raised
lrint(LONG_MIN-2048.0) = -9223372036854775808
    FE_INVALID was raised</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.12.9.4 The rint functions (p: 184) </li>
<li> 7.12.9.5 The lrint and llrint functions (p: 184) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 272-273) </li>
<li> F.10.6.4 The rint functions (p: 384) </li>
<li> F.10.6.5 The lrint and llrint functions (p: 384) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.9.4 The rint functions (p: 252) </li>
<li> 7.12.9.5 The lrint and llrint functions (p: 252) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.6.4 The rint functions (p: 527) </li>
<li> F.10.6.5 The lrint and llrint functions (p: 527) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.9.4 The rint functions (p: 232-233) </li>
<li> 7.12.9.5 The lrint and llrint functions (p: 233) </li>
<li> 7.22 Type-generic math &lt;tgmath.h&gt; (p: 335-337) </li>
<li> F.9.6.4 The rint functions (p: 463) </li>
<li> F.9.6.5 The lrint and llrint 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="trunc" title="c/numeric/math/trunc"> <span class="t-lines"><span>trunc</span><span>truncf</span><span>truncl</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 nearest integer not greater in magnitude than the given value <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="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="../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/rint" title="cpp/numeric/math/rint">C++ documentation</a></span> for <code>rint</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/math/rint" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/rint</a>
  </p>
</div>