December 2005
Beginner to intermediate
618 pages
20h 19m
English
remainder
Calculates the remainder of a floating-point division
#include <math.h> doubleremainder( doublex, doubley); floatremainderf( floatx, floaty); long doubleremainderl( long doublex, long doubley);
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.
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 );Read now
Unlock full access