LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 595|回复: 1

float 有点晕

[复制链接]
发表于 2004-12-7 16:32:08 | 显示全部楼层 |阅读模式
void main()
{
  float price = 525.75;
   float sales_tax = 0.06;

   printf("The item cost is %f\n", price);

   float result;
   result=price * sales_tax;
   printf("Sales tax on the item is %f\n",result );

}

The item cost is 525.750000
Sales tax on the item is 31.545000

void main()
{

   float price = 525.75;
   float sales_tax = 0.06;

   printf("The item cost is %f\n", price);

   float result;

   printf("Sales tax on the item is %f\n",price * sales_tax );

}
The item cost is 525.750000
Sales tax on the item is 31.544999
发表于 2004-12-7 17:07:44 | 显示全部楼层
浮点数是不能精确表示的,运算过程会有误差。估计前一个程序是在将运算结果赋值给浮点变量result时,可能在截断尾数时进行了舍入。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表