#include
#define HARMONIC_MEAN(X, Y) ( 1/((1/X+1/Y) / 2) )
int main(void)
{
printf("harmonic mean of and : %g\n", HARMONIC_MEAN(2, 3));
return 0;
}
調用錯了嗎?為什麼?
error C2124: divide or mod by zero
究其原因,是因為宏定義過程中,變量默認為整型處理,才會導致0除。
把程序稍微改一改,變成:
#define HARMONIC_MEAN(X, Y) ( 1.0/((1.0/X+1.0/Y) / 2.0) )
int main(void)
{
printf("harmonic mean of and : %f\n", double(HARMONIC_MEAN(2, 3)));
return 0;
}
就OK啦~注意數據類型哈!