summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Fremainder.html
blob: 1abb29954c74f5d4b690895f272288a47f8be947 (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
    <h1 id="firstHeading" class="firstHeading">remainder, remainderf, remainderl</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       remainderf( float x, float y );</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      remainder( double x, double y );</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 remainderl( long double x, long double y );</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 remainder( x, y )</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> Computes the IEEE remainder of the floating point division operation <code>x/y</code>.</div> <div class="t-li1">
<span class="t-li">4)</span> Type-generic macro: If any argument has type <code>long double</code>, <code>remainderl</code> is called. Otherwise, if any argument has integer type or has type <code>double</code>, <code>remainder</code> is called. Otherwise, <code>remainderf</code> is called.</div> <p>The IEEE floating-point remainder of the division operation <code>x/y</code> calculated by this function is exactly the value <code>x - n*y</code>, where the value <code>n</code> is the integral value nearest the exact value <code>x/y</code>. When |n-x/y| = ½, the value <code>n</code> is chosen to be even.</p>
<p>In contrast to <code><a href="fmod" title="c/numeric/math/fmod">fmod()</a></code>, the returned value is not guaranteed to have the same sign as <code>x</code>.</p>
<p>If the returned value is <code>0</code>, it will have the same sign as <code>x</code>.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> x, y </td> <td> - </td> <td> floating point values </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>If successful, returns the IEEE floating-point remainder of the division <code>x/y</code> as defined above.</p>
<p>If a domain error occurs, an implementation-defined value is returned (NaN where supported).</p>
<p>If a range error occurs due to underflow, the correct result is returned.</p>
<p>If <code>y</code> is zero, but the domain error does not occur, zero 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>Domain error may occur if <code>y</code> is zero.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> The current <a href="../fenv/fe_round" title="c/numeric/fenv/FE round">rounding mode</a> has no effect. </li>
<li> <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> is never raised, the result is always exact. </li>
<li> If <code>x</code> is ±∞ and <code>y</code> is not NaN, NaN is returned and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised. </li>
<li> If <code>y</code> is ±0 and <code>x</code> is not NaN, NaN is returned and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised. </li>
<li> If either argument is NaN, NaN is returned. </li>
</ul> <h3 id="Notes"> Notes</h3> <p><a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/remainder.html">POSIX requires</a> that a domain error occurs if <code>x</code> is infinite or <code>y</code> is zero.</p>
<p><code><a href="fmod" title="c/numeric/math/fmod">fmod</a></code>, but not <code>remainder</code> is useful for doing silent wrapping of floating-point types to unsigned integer types: <code><span class="br0">(</span><span class="nu16">0.0</span> <span class="sy1">&lt;=</span> <span class="br0">(</span>y <span class="sy1">=</span> <a href="http://en.cppreference.com/w/c/numeric/math/fmod"><span class="kw647">fmod</span></a><span class="br0">(</span><a href="http://en.cppreference.com/w/c/numeric/math/rint"><span class="kw692">rint</span></a><span class="br0">(</span>x<span class="br0">)</span>, <span class="nu16">65536.0</span><span class="br0">)</span><span class="br0">)</span> <span class="sy4">?</span> y <span class="sy4">:</span> <span class="nu16">65536.0</span> <span class="sy2">+</span> y<span class="br0">)</span></code> is in the range <code>[-0.0 .. 65535.0]</code>, which corresponds to <code>unsigned short</code>, but <code>remainder<span class="br0">(</span><a href="http://en.cppreference.com/w/c/numeric/math/rint"><span class="kw692">rint</span></a><span class="br0">(</span>x<span class="br0">)</span>, <span class="nu16">65536.0</span><span class="br0">)</span></code> is in the range <code>[-32767.0, +32768.0]</code>, which is outside of the range of <code>signed short</code>.</p>
<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;
 
#pragma STDC FENV_ACCESS ON
int main(void)
{
    printf("remainder(+5.1, +3.0) = %.1f\n", remainder(5.1,3));
    printf("remainder(-5.1, +3.0) = %.1f\n", remainder(-5.1,3));
    printf("remainder(+5.1, -3.0) = %.1f\n", remainder(5.1,-3));
    printf("remainder(-5.1, -3.0) = %.1f\n", remainder(-5.1,-3));
 
    // special values
    printf("remainder(-0.0, 1.0) = %.1f\n", remainder(-0.0, 1));
    printf("remainder(+5.1, Inf) = %.1f\n", remainder(5.1, INFINITY));
 
    // error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("remainder(+5.1, 0) = %.1f\n", remainder(5.1, 0));
    if(fetestexcept(FE_INVALID)) puts("    FE_INVALID raised");
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">remainder(+5.1, +3.0) = -0.9
remainder(-5.1, +3.0) = 0.9
remainder(+5.1, -3.0) = -0.9
remainder(-5.1, -3.0) = 0.9
remainder(+0.0, 1.0) = 0.0
remainder(-0.0, 1.0) = -0.0
remainder(+5.1, Inf) = 5.1
remainder(+5.1, 0) = -nan
    FE_INVALID raised</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.12.10.2 The remainder functions (p: 185-186) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 272-273) </li>
<li> F.10.7.2 The remainder functions (p: 385) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.10.2 The remainder functions (p: 254-255) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.7.2 The remainder functions (p: 529) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.10.2 The remainder functions (p: 235) </li>
<li> 7.22 Type-generic math &lt;tgmath.h&gt; (p: 335-337) </li>
<li> F.9.7.2 The remainder functions (p: 465) </li>
</ul>
</ul>                 <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="div" title="c/numeric/math/div"> <span class="t-lines"><span>div</span><span>ldiv</span><span>lldiv</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> computes quotient and remainder of integer division <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="fmod" title="c/numeric/math/fmod"> <span class="t-lines"><span>fmod</span><span>fmodf</span><span>fmodl</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> computes remainder of the floating-point division operation <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="remquo" title="c/numeric/math/remquo"> <span class="t-lines"><span>remquo</span><span>remquof</span><span>remquol</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> computes signed remainder as well as the three last bits of the division operation <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/remainder" title="cpp/numeric/math/remainder">C++ documentation</a></span> for <code>remainder</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/remainder" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/remainder</a>
  </p>
</div>