C Standard Library

C <math.h> - remainder() Function



The C <math.h> remainder() function returns the floating-point remainder of x/y (rounded to nearest integer). This can be mathematically expressed as below:

remainder = x - quotient * y

Where quotient is the result of x/y rounded towards nearest integer (with halfway cases rounded toward the even number).

Note: The fmod function is similar to the remainder function except the quotient is rounded towards zero instead of nearest integer. The remquo function is also very similar to the remainder function except it additionally stores the quotient internally used to determine the result.

Syntax

double remainder (double x, double y);
float remainderf (float x, float y);
long double remainderl (long double x, long double y);                  

Parameters

x Specify the value of numerator.
y Specify the value of denominator.

Return Value

Returns remainder of x/y. If y is zero, the function may either return zero or cause a domain error (depending on the library implementation).

Example:

In the example below, remainder() function is used to find out the remainder of a given division.

#include <stdio.h>
#include <math.h>
 
int main (){
  printf("%lf\n", remainder(23, 4));
  printf("%lf\n", remainder(20, 4.9));
  printf("%lf\n", remainder(20.5, 2.1));
  return 0;
}

The output of the above code will be:

-1.000000
0.400000
-0.500000

❮ C <math.h> Library