December 2005
Beginner to intermediate
618 pages
20h 19m
English
fmod
Performs the modulo operation
#include <math.h> doublefmod( doublex, doubley); floatfmodf( floatx, floaty); (C99) long doublefmodl( long doublex, long doubley); (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.
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 settingThis 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.
The C99 functions remainder() and
remquo()
Read now
Unlock full access