除了一开始用float进行定义之外,后面进行除法运算的时候要加.0,否则算出的结果电脑会自动取整~~如:3/2的结果和3.0/2的结果就不同~~因为没有定义3/2为浮点型,所以3/2自动取整,结果等于1而3.0/2,由于预先用浮点型表示其结果显然为:1.5 除法运算符" / ",如果是两个整数相除结果为整数如果需要保留小数时 必须将其中一个除数转换为浮点数 #i nclude <stdio.h> #i nclude <math.h> main() { float x; float y; printf("Enter x:"); scanf("%d",&x); y=fabs((5*x+1)/(x*x+1)); printf("y is %f\n",y); } 或者 #i nclude <stdio.h> #i nclude <math.h> main() { int x; float y; printf("Enter x:"); scanf("%d",&x); y=fabs((float)(5*x+1)/(x*x+1)); printf("y is %f\n",y); }
温馨提示:答案为网友推荐,仅供参考