以下是讓我困惑 -混淆C++乘法運算
如果你乘float
10(integer
)看看發生了什麼事,也 這些類型的操作似乎很奇怪!
#include <iostream>
using namespace std;
int main()
{
float x1 = 0.1;// x1 *10 = 1 ture
float x2 = 0.7;//x2 *10 = 7 false answer give 0.6
float x3 = 0.8;//x3 *10 = 8 true
float x4 = 0.9;// x4 * 10 = 9 false ops! the answer is 8
int f = x4 * 10 ;
cout<<f ;
return 0;
}
任何人都可以解釋一下嗎?
那麼[ideone](http://ideone.com/P4cCX2)輸出'9'。 –
您將返回的值存儲爲int而不是float。 – woodlumhoodlum
你複製相同的代碼? –