注册 登录
编程论坛 C++教室

为什么结果是这样的?

zhouzhengbo 发布于 2007-10-15 22:36, 448 次点击

源程序:
#include <iostream>
using namespace std;

int main()
{
double a=3.3;
double b=1.1;
int c;
c=a/b;
cout<<c<<endl;
system("pause");
return 0;
}

上述程序输出为2,不知道是为什么.我又试了其他的,发现如果a=2.2,4.4,8.8,17.6...等1.1的2的次幂的倍数时
输出为2,4,8,16...但是其他的时,如3.3,5.5,6.6,7.7..时,输出为2,4,5,6...
不知道是不是和计算机内部的舍入误差有关.请教了~~~~谢谢!!

5 回复
#2
HJin2007-10-16 00:31

#include <iostream>
using namespace std;


/**
2.9999999999999996000
2
Press any key to continue . . .
*/

int main()
{
double a=3.3;
double b=1.1;
int c;

printf("%.19f\n", a/b);

c=a/b;
cout<<c<<endl;
system("pause");
return 0;
}

#3
zhouzhengbo2007-10-16 07:42

谢谢.可是怎么解释发现如果a=2.2,4.4,8.8,17.6...等1.1的2的次幂的倍数时
输出为2,4,8,16...?

#4
HJin2007-10-16 08:14
if you really want to know how things work inside a computer, you need to know how numbers are represented in the memory.

IEEE standard for floating-point numbers is a good starting point.

I think it is IEEE 754.
#5
duffebear2007-10-16 09:18
应该是浮点数的存储形式
#6
zhouzhengbo2007-10-17 17:40

谢谢各位!

1