自学C语言,求帮忙回答二个问题
float a=123456.123,b=654321.321;
printf("%f",a+b);
为何结果不是777777.444000
而是777777.437500
第二个问题:
printf("%7.2f\n",4.0/3*(PI*r*r*r));
输出列表里那个4.0我写成4,程序调试就报错
[解决办法]
二进制无法精确表示 float, google 小叔如何转化为二进制
4/3 = 0
4.0/3 = 1.333333...
[解决办法]
#include <stdio.h>
void main()
{
double a=123456.123;
double b=654321.321;
printf("%f\n",a+b);
}//777777.444000
// By MoreWindows( http://blog.csdn.net/MoreWindows )
#include <stdio.h>
int main()
{
float a=123456.123, b=654321.321;
printf("%f + %f = %f\n", a, b, a + b);
return 0;
}