<cmath> (math.h)

function
<cmath> <ctgmath>

remainder

     double remainder  (double numer     , double denom);
      float remainderf (float numer      , float denom);
long double remainderl (long double numer, long double denom);
     double remainder (double numer     , double denom);
      float remainder (float numer      , float denom);
long double remainder (long double numer, long double denom);
     double remainder (Type1 numer      , Type2 denom);       // additional overloads
Compute remainder (IEC 60559)
Returns the floating-point remainder of numer/denom (rounded to nearest):

remainder = numer - rquot * denom

Where rquot is the result of: numer/denom, rounded toward the nearest integral value (with halfway cases rounded toward the even number).

A similar function, fmod, returns the same but with the quotient truncated (rounded towards zero) instead.
The function remquo has a behavior identical to this function, but it additionally provides access to the intermediate quotient value used.

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for other combinations of arithmetic types (Type1 and Type2): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case both are casted to long double instead).

Parameters

numer
Value of the quotient numerator.
denom
Value of the quotient denominator.

Return Value

The remainder of dividing the arguments.
If this remainder is zero, its sign shall be that of numer.
If denom is zero, the function may either return zero or cause a domain error (depending on the library implementation).

If a domain error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to EDOM.
- And math_errhandling has MATH_ERREXCEPT set: FE_INVALID is raised.

Example

1
2
3
4
5
6
7
8
9
10
/* remainder example */
#include <stdio.h>      /* printf */
#include <math.h>       /* remainder */

int main ()
{
  printf ( "remainder of 5.3 / 2 is %f\n", remainder (5.3,2) );
  printf ( "remainder of 18.5 / 4.2 is %f\n", remainder (18.5,4.2) );
  return 0;
}


Output:

remainder of 5.3 / 2 is -0.700000
remainder of 18.5 / 4.2 is 1.700000

See also