In C, remainder() returns the IEEE remainder of x / y. It chooses the remainder so that the quotient is the integer nearest to the exact x / y (ties to even).
You pass two double values. Both are required. The result is a double whose magnitude is at most half of |y|. Include <math.h>. This is different from fmod(), which truncates the quotient toward zero.
When you need the remainder that matches IEEE 754 rules, prefer remainder. When you want the classic “what is left after truncating division,” use fmod.
Syntax
Pass the dividend, then the divisor. Start with this call.
C
remainder(5.1, 3.0);
Call
What it does
remainder(5.1, 3.0);
Returns about -0.9, the IEEE remainder of 5.1 / 3.
Include <math.h>. On GCC, link with -lm. Do not pass 0 for y.
Library declarations
C
double remainder(double x, double y);
float remainderf(float x, float y);
long double remainderl(long double x, long double y);
Parameters
Parameter
Description
x
Required. The dividend.
y
Required. The divisor. It must not be 0.
Return Value
Return Value
Description
IEEE remainder
x - n*y where n is the integer nearest to x/y (ties to even).
NaN
Returned for domain errors such as y == 0.
C Examples
Example 1 — Basic IEEE Remainder
Print a simple remainder and notice it can be negative even when both inputs are positive.