C++ double型不能實施%操作符,作為除數被除數都不可以,但可以用fmod函數,則作為除數被除數都可以,即
fmod = numer - tquot * denom //tquot 是除法結果向“下”取整(向0的方向)的結果,denom是除數
一個例子是
/* fmod example */
#include <stdio.h> /* printf */
#include <math.h> /* fmod */
int main ()
{
printf ( "fmod of 5.3 / 2 is %f\n", fmod (5.3,2) );
printf ( "fmod of 18.5 / 4.2 is %f\n", fmod (18.5,4.2) );
return 0;
}
結果是
fmod of 5.3 / 2 is 1.300000
fmod of 18.5 / 4.2 is 1.700000