Previous Page
Next Page

fmod

Performs the modulo operation

#include <math.h>
double fmod ( double x , double y  );
float fmodf ( float x , float y  );      (C99)
long double fmodl ( long double x , long double y  );      (C99)

The fmod( ) function returns the remainder of the floating-point division of x by y, called "x modulo y." The remainder is equal to x minus the product of y and the largest integer quotient whose absolute value is not greater than that of y. This quotient is negative (or 0) if x and y have opposite signs, and the return value has the same sign as x. If the argument y is zero, fmod( ) may incur a domain error, or return 0.

Example

double people = -2.25, apples = 3.3, eachgets = 0.0, someleft = 0.0;
int saverounding = fegetround( );       // Save previous setting

fesetround(FE_TOWARDZERO);

eachgets = rint( apples / people );
someleft = fmod( apples, people );

printf( "If there are %+.2f of us and %+.2f apples, "
        "each of us gets %+.2f, with %+.2f left over.\n",
        people, apples, eachgets, someleft );

fesetround( saverounding );            // Restore previous setting

This code produces the following output:

If there are -2.25 of us and +3.30 apples, each of us gets -1.00, with +1.05 left over.

See Also

The C99 functions remainder( ) and remquo( )


Previous Page
Next Page