From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/numeric%2Fmath%2Fdiv.html | 120 ++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 devdocs/c/numeric%2Fmath%2Fdiv.html (limited to 'devdocs/c/numeric%2Fmath%2Fdiv.html') 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 @@ +

div, ldiv, lldiv, imaxdiv

Defined in header <stdlib.h>
div_t     div( int x, int y );
+
(1)
ldiv_t    ldiv( long x, long y );
+
(2)
lldiv_t   lldiv( long long x, long long y );
+
(3) (since C99)
Defined in header <inttypes.h>
imaxdiv_t imaxdiv( intmax_t x, intmax_t y );
+
(4) (since C99)

Computes both the quotient and the remainder of the division of the numerator x by the denominator y.

+ + + + +

Computes quotient and remainder simultaneously. The quotient is the algebraic quotient with any fractional part discarded (truncated towards zero). The remainder is such that quot * y + rem == x.

+
(until C99)

Computes the quotient (the result of the expression x / y) and remainder (the result of the expression x % y) simultaneously.

+
(since C99)

Parameters

+ +
x, y - integer values

Return value

If both the remainder and the quotient can be represented as objects of the corresponding type (int, long, long long, intmax_t, respectively), returns both as an object of type div_t, ldiv_t, lldiv_t, imaxdiv_t defined as follows:

+

div_t

struct div_t { int quot; int rem; };

or

+
struct div_t { int rem; int quot; };

ldiv_t

struct ldiv_t { long quot; long rem; };

or

+
struct ldiv_t { long rem; long quot; };

lldiv_t

struct lldiv_t { long long quot; long long rem; };

or

+
struct lldiv_t { long long rem; long long quot; };

imaxdiv_t

struct imaxdiv_t { intmax_t quot; intmax_t rem; };

or

+
struct imaxdiv_t { intmax_t rem; intmax_t quot; };

If either the remainder or the quotient cannot be represented, the behavior is undefined.

+

Notes

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 div and ldiv.

+

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 / and % where suitable.

+

Example

#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));
+}

Possible output:

+
0
+21
+12346
+-12346
+-101010
+7fffffff
+-80000000

References

See also

+ + + +
+
(C99)(C99)
computes remainder of the floating-point division operation
(function)
+
(C99)(C99)(C99)
computes signed remainder of the floating-point division operation
(function)
+
(C99)(C99)(C99)
computes signed remainder as well as the three last bits of the division operation
(function)
C++ documentation for div
+ + +
1. +Euclidean division — From Wikipedia.
2. +Modulo (and Truncated division) — From Wikipedia.
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/numeric/math/div +

+
-- cgit v1.2.3