function
<cmath> <ctgmath>

fmod

double fmod (double numer, double denom);
     double fmod  (double numer     , double denom);
      float fmodf (float numer      , float denom);
long double fmodl (long double numer, long double denom);
     double fmod (double numer     , double denom);
      float fmod (float numer      , float denom);
long double fmod (long double numer, long double denom);
     double fmod (double numer     , double denom);
      float fmod (float numer      , float denom);
long double fmod (long double numer, long double denom);
     double fmod (Type1 numer      , Type2 denom);       // additional overloads
Compute remainder of division
Returns the floating-point remainder of numer/denom (rounded towards zero):

fmod = numer - tquot * denom

Where tquot is the truncated (i.e., rounded towards zero) result of: numer/denom.

A similar function, remainder, returns the same but with the quotient rounded to the nearest integer (instead of truncated).

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 denom is zero, the function may either return zero or cause a domain error (depending on the library implementation).

If a domain error occurs, the global variable errno is set to EDOM.
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
/* fmod example */
#include <stdio.h>      /* printf */
#include <math.h>       /* fmod */

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


Output:

fmod of 5.3 / 2 is 1.300000
fmod of 18.5 / 4.2 is 1.700000

See also