Name

remainder

Synopsis

Calculates the remainder of a floating-point division

#include <math.h>
doubleremainder( double x, double y );
float remainderf( float x, float y );
long double remainderl( long double x, long double y );

The remainder() functions return the remainder r of x / y, such that r = xny, where n is the nearest integer to the exact value of x / y (regardless of the current rounding direction). If the exact quotient is exactly halfway between two integers, then the quotient n is the nearest even integer. By this definition, the remainder can be positive or negative. If the remainder is zero, then remainder() returns 0 with the same sign as the argument x.

Example

double apples, people, share, left;
printf( "\nHow many people? ");
scanf( "%lf", &people );
printf( "\nHow many apples? ");
scanf( "%lf", &apples );

left =remainder( apples, people );    // left may be negative!
share = ( apples − left ) / people;

printf( "If there are %.1lf of us and %.1lf apples, "
        "each of us gets %.1lf of %s, with %.1lf left over.\n",
        people, apples, share, ( share < 1 ) ? "one" : "them", left );

See Also

remquo(), fmod()

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.