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

Go to the source code of this file.

Functions

ldiv_t ldiv (long num, long denom)
 Computes both the quotient and the remainder of the division of the numerator x by the denominator y. More...
 

Function Documentation

◆ ldiv()

ldiv_t ldiv ( long  x,
long  y 
)

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
xinteger values
yinteger values
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 35 of file ldiv.c.

36 {
37  ldiv_t r;
38 
39  /* see div.c for comments */
40 
41  r.quot = num / denom;
42  r.rem = num % denom;
43  if(num >= 0 && r.rem < 0)
44  {
45  r.quot++;
46  r.rem -= denom;
47  }
48  return (r);
49 }
Division type for long integers.
Definition: stdlib.h:22
long rem
Definition: stdlib.h:25
long quot
Definition: stdlib.h:24

References ldiv_t::quot, and ldiv_t::rem.