Previous Page
Next Page

remainder

Calculates the remainder of a floating-point division

#include <math.h>
double remainder ( 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 = x - ny, 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( )


Previous Page
Next Page