#includeintmain(void){inti=0;floatamounts[5];longdollars[5];longcents[5];for(i=1;i<=4;i++){printf("pleaseenteramounts[%d]:\n",i-1);scanf("%f",&amounts[i-1]);doll...
#include int main(void){ int i=0; float amounts[5]; long dollars[5]; long cents[5]; for(i=1;i<=4;i++) { printf("please enter amounts[%d]:\n",i-1); scanf("%f",&amounts[i-1]); dollars[i-1]=(int)amounts[i-1]; printf("dollars[%d]=%d\n",i-1,dollars[i-1]); cents[i-1]=100*(amounts[i-1]-dollars[i-1]); printf("cents[%d]=%d\n",i-1,cents[i-1]); }return 0;}
这是我写的代码,假如输入1.54的话,cent会显示成53。我将cent改为float类型的后,发现其实cent是53.999996。请问这0.000004的误差是哪里来的?(其实解决办法也不是没有,我就是想知道这点误差是怎么来的,为什么是0.000004,这是一个随机的么?,另外,也不是减的时候类型不匹配的问题,我曾把dollars乘以1.0试过,还是有误差)
抱歉,分数不多了。还望不吝赐教。多谢
哎?这代码怎么这么显示啊?我再贴一次试试
#include int main(void){ int i=0; float amounts[5]; long dollars[5]; float cents[5]; for(i=1;i<=4;i++) { printf("please enter amounts[%d]:\n",i-1); scanf("%f",&amounts[i-1]); dollars[i-1]=(int)amounts[i-1]; printf("dollars[%d]=%d\n",i-1,dollars[i-1]); cents[i-1]=100*(amounts[i-1]-dollars[i-1]); printf("cents[%d]=%f\n",i-1,cents[i-1]); }return 0;}
展开