diff options
Diffstat (limited to 'devdocs/c/numeric%2Fmath%2Fdiv.html')
| -rw-r--r-- | devdocs/c/numeric%2Fmath%2Fdiv.html | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/devdocs/c/numeric%2Fmath%2Fdiv.html b/devdocs/c/numeric%2Fmath%2Fdiv.html new file mode 100644 index 00000000..529a3c32 --- /dev/null +++ b/devdocs/c/numeric%2Fmath%2Fdiv.html @@ -0,0 +1,120 @@ + <h1 id="firstHeading" class="firstHeading">div, ldiv, lldiv, imaxdiv</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdlib.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">div_t div( int x, int y );</pre> +</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl"> <td> <pre data-language="c">ldiv_t ldiv( long x, long y );</pre> +</td> <td> (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">lldiv_t lldiv( long long x, long long 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><inttypes.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">imaxdiv_t imaxdiv( intmax_t x, intmax_t y );</pre> +</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> </table> <p>Computes both the quotient and the remainder of the division of the numerator <code>x</code> by the denominator <code>y</code>.</p> +<table class="t-rev-begin"> <tr class="t-rev t-until-c99"> +<td> <p>Computes quotient and remainder simultaneously. The quotient is the algebraic quotient with any fractional part discarded (truncated towards zero). The remainder is such that <code>quot * y + rem == x</code>.</p> +</td> <td><span class="t-mark-rev t-until-c99">(until C99)</span></td> +</tr> <tr class="t-rev t-since-c99"> +<td> <p>Computes the quotient (the result of the expression <code>x / y</code>) and remainder (the result of the expression <code>x % y</code>) simultaneously.</p> +</td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td> +</tr> </table> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> x, y </td> <td> - </td> <td> integer values </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p>If both the remainder and the quotient can be represented as objects of the corresponding type (<code>int</code>, <code>long</code>, <code>long long</code>, <code><a href="http://en.cppreference.com/w/c/types/integer"><span class="kw118">intmax_t</span></a></code>, respectively), returns both as an object of type <code>div_t</code>, <code>ldiv_t</code>, <code>lldiv_t</code>, <code>imaxdiv_t</code> defined as follows:</p> +<div class="t-member"> <h2 id="div_t"> div_t</h2> <div class="c source-c"><pre data-language="c">struct div_t { int quot; int rem; };</pre></div> <p>or</p> +<div class="c source-c"><pre data-language="c">struct div_t { int rem; int quot; };</pre></div> </div> <div class="t-member"> <h2 id="ldiv_t"> ldiv_t</h2> <div class="c source-c"><pre data-language="c">struct ldiv_t { long quot; long rem; };</pre></div> <p>or</p> +<div class="c source-c"><pre data-language="c">struct ldiv_t { long rem; long quot; };</pre></div> </div> <div class="t-member"> <h2 id="lldiv_t"> lldiv_t</h2> <div class="c source-c"><pre data-language="c">struct lldiv_t { long long quot; long long rem; };</pre></div> <p>or</p> +<div class="c source-c"><pre data-language="c">struct lldiv_t { long long rem; long long quot; };</pre></div> </div> <div class="t-member"> <h2 id="imaxdiv_t"> imaxdiv_t</h2> <div class="c source-c"><pre data-language="c">struct imaxdiv_t { intmax_t quot; intmax_t rem; };</pre></div> <p>or</p> +<div class="c source-c"><pre data-language="c">struct imaxdiv_t { intmax_t rem; intmax_t quot; };</pre></div> </div> <p>If either the remainder or the quotient cannot be represented, the behavior is undefined.</p> +<h3 id="Notes"> Notes</h3> <p>Until C99, the rounding direction of the quotient and the sign of the remainder in the built-in division and remainder operators was implementation-defined if either of the operands was negative, but it was well-defined in <code>div</code> and <code>ldiv</code>.</p> +<p>On many platforms, a single CPU instruction obtains both the quotient and the remainder, and this function may leverage that, although compilers are generally able to merge nearby <code>/</code> and <code>%</code> where suitable.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <assert.h> +#include <limits.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> + +void reverse(char* first, char* last) +{ + for (--last; first < last; ++first, --last) + { + char c = *last; + *last = *first; + *first = c; + } +} + +// returns empty buffer in case of buffer overflow +char* itoa(int n, int base, char* buf, size_t buf_size) +{ + assert(2 <= base && base <= 16 && buf && buf_size); + div_t dv = {.quot = n}; + char* p = buf; + do + { + if (!--buf_size) + return (*buf = '\0'), buf; + dv = div(dv.quot, base); + *p++ = "0123456789abcdef"[abs(dv.rem)]; + } + while(dv.quot); + if (n < 0) + *p++ = '-'; + *p = '\0'; + reverse(buf, p); + return buf; +} + +int main(void) +{ + char buf[16]; + printf("%s\n", itoa(0, 2, buf, sizeof buf)); + printf("%s\n", itoa(007, 3, buf, sizeof buf)); + printf("%s\n", itoa(12346, 10, buf, sizeof buf)); + printf("%s\n", itoa(-12346, 10, buf, sizeof buf)); + printf("%s\n", itoa(-42, 2, buf, sizeof buf)); + printf("%s\n", itoa(INT_MAX, 16, buf, sizeof buf)); + printf("%s\n", itoa(INT_MIN, 16, buf, sizeof buf)); +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">0 +21 +12346 +-12346 +-101010 +7fffffff +-80000000</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C23 standard (ISO/IEC 9899:2023): </li> +<ul> +<li> 7.8.2.2 The imaxdiv function (p: TBD) </li> +<li> 7.22.6.2 The div, ldiv and lldiv functions (p: TBD) </li> +</ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul> +<li> 7.8.2.2 The imaxdiv function (p: 159) </li> +<li> 7.22.6.2 The div, ldiv and lldiv functions (p: 259) </li> +</ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 7.8.2.2 The imaxdiv function (p: 219) </li> +<li> 7.22.6.2 The div, ldiv and lldiv functions (p: 356) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul> +<li> 7.8.2.2 The imaxdiv function (p: 200) </li> +<li> 7.20.6.2 The div, ldiv and lldiv functions (p: 320) </li> +</ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul> +<li> 4.10 div_t, ldiv_t </li> +<li> 4.10.6.2 The div function </li> +<li> 4.10.6.4 The ldiv function </li> +</ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <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="remainder" title="c/numeric/math/remainder"> <span class="t-lines"><span>remainder</span><span>remainderf</span><span>remainderl</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 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/div" title="cpp/numeric/math/div">C++ documentation</a></span> for <code>div</code> </td> +</tr> </table> <h3 id="External_links"> External links</h3> <table> <tr style="vertical-align:top;"> <td>1. </td> <td> +<a href="https://en.wikipedia.org/wiki/Euclidean_division" class="extiw" title="enwiki:Euclidean division">Euclidean division</a> — From Wikipedia. </td> +</tr> <tr style="vertical-align:top;"> <td>2. </td> <td> +<a href="https://en.wikipedia.org/wiki/Modulo" class="extiw" title="enwiki:Modulo">Modulo (and Truncated division)</a> — From Wikipedia. </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/div" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/div</a> + </p> +</div> |
