Names specified here
Name Description Notes Source Availability
div() Compute quotient and remainder of integer division (·) <stdlib.h> C89 C90 C95 C99 C11
div_t Result of integer division T <stdlib.h> C89 C90 C95 C99 C11
fmod() Compute remainder (·) <math.h> C89 C90 C95 C99 C11
fmod() Compute remainder M (·) <tgmath.h> C99 C11
fmodf() Compute remainder (·) <math.h> C99 C11
fmodl() Compute remainder (·) <math.h> C99 C11
imaxdiv() Compute quotient and remainder of integer division (·) <inttypes.h> C99 C11
imaxdiv_t Result of integer division T <inttypes.h> C99 C11
ldiv() Compute quotient and remainder of integer division (·) <stdlib.h> C99 C11
ldiv_t Result of integer division T <stdlib.h> C99 C11
lldiv() Compute quotient and remainder of integer division (·) <stdlib.h> C99 C11
lldiv_t Result of integer division T <stdlib.h> C99 C11
remainder() Compute IEC 60559 remainder M (·) <tgmath.h> C99 C11
remainder() Compute IEC 60559 remainder (·) <math.h> C99 C11
remainderf() Compute IEC 60559 remainder (·) <math.h> C99 C11
remainderl() Compute IEC 60559 remainder (·) <math.h> C99 C11
remquo() Compute IEC 60559 quotient and remainder M (·) <tgmath.h> C99 C11
remquo() Compute IEC 60559 quotient and remainder (·) <math.h> C99 C11
remquof() Compute IEC 60559 quotient and remainder (·) <math.h> C99 C11
remquol() Compute IEC 60559 quotient and remainder (·) <math.h> C99 C11
multiplicative-expression
multiplicative-expression / cast-expression
multiplicative-expression % cast-expression
assignment-expression
unary-expression assignment-operator assignment-expression
assignment-operator
/=
%=

Arithmetic division is performed with the operators / and %, and with an assortment of library functions. Some functions and operators perform only integer division, producing either the quotient alone (/), the remainder alone (%, remainder…), or both combined (div…, remquo…). Some functions compute the remainder of a non-integer division (fmod…).

The operands of / must be of arithmetic type. Standard arithmetic promotions are applied to the operands.

To perform real division of integers, you must convert at least one operand to a real type, e.g., (double) x / y.

The operator % works only on integers, and yields the remainder rather than the quotient.

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
  const int d = 3;
  int i;
  for (i = 6; i >= -6; i--)
    printf("%3d %% %3d = %3d\n", i, d, i % d);

  return 0;
}
  6 %   3 =   0
  5 %   3 =   2
  4 %   3 =   1
  3 %   3 =   0
  2 %   3 =   2
  1 %   3 =   1
  0 %   3 =   0
 -1 %   3 =  -1
 -2 %   3 =  -2
 -3 %   3 =   0
 -4 %   3 =  -1
 -5 %   3 =  -2
 -6 %   3 =   0

If you want only positive results, you could add the divisor and use % again:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
  const int d = 3;
  int i;
  for (i = 6; i >= -6; i--)
    printf("%3d %%' %3d = %3d\n", i, d, (i % d + d) % d);

  return 0;
}
  6 %'   3 =   0
  5 %'   3 =   2
  4 %'   3 =   1
  3 %'   3 =   0
  2 %'   3 =   2
  1 %'   3 =   1
  0 %'   3 =   0
 -1 %'   3 =   2
 -2 %'   3 =   1
 -3 %'   3 =   0
 -4 %'   3 =   2
 -5 %'   3 =   1
 -6 %'   3 =   0
#include <stdlib.h>
div_t div(int x, int y);
ldiv_t ldiv(long x, long y);
lldiv_t lldiv(long long x, long long y);
#include <inttypes.h>
imaxdiv_t imaxdiv(intmax_t x, intmax_t y);

The div functions divide x by y, and return the quotient and remainder as a structure type div_t, ldiv_t, lldiv_t or imaxdiv_t. The results are the same as for / and %, but computed in a single operation. Each structure has two members quot and rem to hold the quotient and remainder respectively. The types of these members match the types of the parameters of the functions that return them.

The headers <stdlib.h>, <inttypes.h>, <math.h> and <tgmath.h> provide additional functions for performing divisions when the operators are insufficient.

#include <math.h>
float fmodf(float x, float y);
double fmod(double x, double y);
long double fmodl(long double x, long double y);
#include <tgmath.h>
real-floating-type fmod(real-floating-type x, real-floating-type y);

The fmod functions divide x by y and return the remainder. The result is xny for some n∈ℤ, such that the result has the sign of x and a magnitude less than |y|.

The following program demonstrates the behaviour of fmod with negative inputs:

#include <stdio.h>
#include <math.h>

int main(void)
{
  double m = 3.0;
  int i;
  for (i = 6; i >= -6; i--)
    printf("fmod(%3d, %3g) = %3g   "
           "fmod(%3d, %3g) = %3g\n",
           i, m, fmod(i, m),
           i, -m, fmod(i, -m));
  return 0;
}
fmod(  6,   3) =   0   fmod(  6,  -3) =   0
fmod(  5,   3) =   2   fmod(  5,  -3) =   2
fmod(  4,   3) =   1   fmod(  4,  -3) =   1
fmod(  3,   3) =   0   fmod(  3,  -3) =   0
fmod(  2,   3) =   2   fmod(  2,  -3) =   2
fmod(  1,   3) =   1   fmod(  1,  -3) =   1
fmod(  0,   3) =   0   fmod(  0,  -3) =   0
fmod( -1,   3) =  -1   fmod( -1,  -3) =  -1
fmod( -2,   3) =  -2   fmod( -2,  -3) =  -2
fmod( -3,   3) =  -0   fmod( -3,  -3) =  -0
fmod( -4,   3) =  -1   fmod( -4,  -3) =  -1
fmod( -5,   3) =  -2   fmod( -5,  -3) =  -2
fmod( -6,   3) =  -0   fmod( -6,  -3) =  -0

If you want only positive results, you could add the divisor and use fmod again:

#include <stdio.h>
#include <math.h>

int main(void)
{
  double m = 3.0;
  int i;
  for (i = 6; i >= -6; i--)
    printf("fmod'(%3d, %3g) = %3g   "
           "fmod'(%3d, %3g) = %3g\n",
           i, m, fmod(fmod(i, m) + m, m),
           i, -m, fmod(fmod(i, -m) - m, m));
  return 0;
}
fmod'(  6,   3) =   0   fmod'(  6,  -3) =  -0
fmod'(  5,   3) =   2   fmod'(  5,  -3) =  -1
fmod'(  4,   3) =   1   fmod'(  4,  -3) =  -2
fmod'(  3,   3) =   0   fmod'(  3,  -3) =  -0
fmod'(  2,   3) =   2   fmod'(  2,  -3) =  -1
fmod'(  1,   3) =   1   fmod'(  1,  -3) =  -2
fmod'(  0,   3) =   0   fmod'(  0,  -3) =  -0
fmod'( -1,   3) =   2   fmod'( -1,  -3) =  -1
fmod'( -2,   3) =   1   fmod'( -2,  -3) =  -2
fmod'( -3,   3) =   0   fmod'( -3,  -3) =  -0
fmod'( -4,   3) =   2   fmod'( -4,  -3) =  -1
fmod'( -5,   3) =   1   fmod'( -5,  -3) =  -2
fmod'( -6,   3) =   0   fmod'( -6,  -3) =  -0
#include <math.h>
float remainderf(float x, float y);
double remainder(double x, double y);
long double remainderl(long double x, long double y);
float remquof(float x, float y, int *quo);
double remquo(double x, double y, int *quo);
long double remquol(long double x, long double y, int *quo);
#include <tgmath.h>
real-floating-type remainder(real-floating-type x, real-floating-type y);
real-floating-type remquo(real-floating-type x, real-floating-type y, int *quo);

The remainder functions return x REM y, where REM is defined by IEC 60559. n is chosen as the nearest integer to x/y, preferring the even choice if x/y is exactly half-way between two integers. Then the result r is computed as xny.

The remquo functions do the same, but they also write Q to *quo, where the sign of Q is the sign of x/y, |Q| ≡ q mod 2n, with implementation-defined n≥3 and n∈ℤ, and q is the integer quotient of x/y. [It's not immediately clear that n is a constant, but it must be, surely?]

The descriptions of remainder and remquo both use n, but with different meanings.

The following program demonstrates the behaviour of remainder and remquo with negative inputs:

#include <stdio.h>
#include <math.h>

int main(void)
{
  double m = 4.0;

  int i, q1, q2;
  double r1, r2;
  for (i = +99; i >= -99; i -= 7) {
    r1 = remquo(i, m, &q1);
    r2 = remquo(i, -m, &q2);
    printf("%3d REM %3g: %3g or %3g with %3d        "
           "%3d REM %3g: %3g or %3g with %3d\n",
           i, m, remainder(i, m), r1, q1,
           i, -m, remainder(i, -m), r2, q2);
  }
  return 0;
}
[ Work in progress : Find out why remquo and remainder are not giving the same result! The results below are from a Kubuntu 14.04 x86_64 system.]
 99 REM   4:  -1 or  67 with   8         99 REM  -4:  -1 or  67 with  -8
 92 REM   4:   0 or  60 with   8         92 REM  -4:   0 or  60 with  -8
 85 REM   4:   1 or  53 with   8         85 REM  -4:   1 or  53 with  -8
 78 REM   4:  -2 or  46 with   8         78 REM  -4:  -2 or  46 with  -8
 71 REM   4:  -1 or  39 with   8         71 REM  -4:  -1 or  39 with  -8
 64 REM   4:   0 or  32 with   8         64 REM  -4:   0 or  32 with  -8
 57 REM   4:   1 or  25 with   8         57 REM  -4:   1 or  25 with  -8
 50 REM   4:   2 or  18 with   8         50 REM  -4:   2 or  18 with  -8
 43 REM   4:  -1 or  11 with   8         43 REM  -4:  -1 or  11 with  -8
 36 REM   4:   0 or   4 with   8         36 REM  -4:   0 or   4 with  -8
 29 REM   4:   1 or   1 with   7         29 REM  -4:   1 or   1 with  -7
 22 REM   4:  -2 or  -2 with   6         22 REM  -4:  -2 or  -2 with  -6
 15 REM   4:  -1 or  -1 with   4         15 REM  -4:  -1 or  -1 with  -4
  8 REM   4:   0 or   0 with   2          8 REM  -4:   0 or   0 with  -2
  1 REM   4:   1 or   1 with   0          1 REM  -4:   1 or   1 with   0
 -6 REM   4:   2 or   2 with  -2         -6 REM  -4:   2 or   2 with   2
-13 REM   4:  -1 or  -1 with  -3        -13 REM  -4:  -1 or  -1 with   3
-20 REM   4:  -0 or  -0 with  -5        -20 REM  -4:  -0 or  -0 with   5
-27 REM   4:   1 or   1 with  -7        -27 REM  -4:   1 or   1 with   7
-34 REM   4:  -2 or  -2 with  -8        -34 REM  -4:  -2 or  -2 with   8
-41 REM   4:  -1 or  -9 with  -8        -41 REM  -4:  -1 or  -9 with   8
-48 REM   4:  -0 or -16 with  -8        -48 REM  -4:  -0 or -16 with   8
-55 REM   4:   1 or -23 with  -8        -55 REM  -4:   1 or -23 with   8
-62 REM   4:   2 or -30 with  -8        -62 REM  -4:   2 or -30 with   8
-69 REM   4:  -1 or -37 with  -8        -69 REM  -4:  -1 or -37 with   8
-76 REM   4:  -0 or -44 with  -8        -76 REM  -4:  -0 or -44 with   8

CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2023-10-04T20:24:03.205+0000