Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
imaxdiv.c File Reference
#include <stdlib.h>
Include dependency graph for imaxdiv.c:

Go to the source code of this file.

Functions

imaxdiv_t imaxdiv (intmax_t numer, intmax_t denom)
 Computes both the quotient and the remainder of the division of the numerator x by the denominator y. More...
 

Function Documentation

◆ imaxdiv()

imaxdiv_t imaxdiv ( intmax_t  numer,
intmax_t  denom 
)

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

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.

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

Parameters
numerintmax_t values (numerator)
denomintmax_t values (denominator)
Returns
If both the remainder and the quotient can be represented as objects of the corresponding type (int, long, long long, imaxdiv_t, respectively), returns both as an object of type
See also
div_t,
ldiv_t,
lldiv_t,
imaxdiv_t.

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

Definition at line 30 of file imaxdiv.c.

31 {
32  imaxdiv_t retval;
33 
34  retval.quot = numer / denom;
35  retval.rem = numer % denom;
36  if(numer >= 0 && retval.rem < 0)
37  {
38  retval.quot++;
39  retval.rem -= denom;
40  }
41  return (retval);
42 }
intmax_t rem
Definition: stdlib.h:39
intmax_t quot
Definition: stdlib.h:38
Division type for maximal integer storage.
Definition: stdlib.h:36

References imaxdiv_t::quot, and imaxdiv_t::rem.