使用c代码查找比率

时间:2010-10-29 06:08:02

标签: c

是否可以使用C ??中的函数计算比率?或者我必须做一个公式??

1 个答案:

答案 0 :(得分:2)

float ratio (float a, float b){
  return a/b;
}

您可能遇到以下问题:

int a=5;
int b=4;
float ratio=b/a; // yields 1.0000 since it is interpreted as integer division
float ratioCorrect=(float)b/a; // yields 1.25 because yuo told the compiler to interpret numbers as floats