Why this condition is never true ? Both parts of the equation are integers, so there must be equality for index = 0, 10, 20, 30, 40. I am compiling this code using g++.
for(int index = 0; index < 50; index++){
if ((int) (10 * ( 0.1 * index) == (int)(10 * (int) (0.1 * index))))
{
std::cout << "equal";
}
}
隨着MSVS 2010編譯器不會發生這些問題的平等......C++,用G ++
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 10
11 10
12 10
13 10
14 10
15 10
16 10
17 10
18 10
19 10
20 20
21 20
22 20
23 20
24 20
25 20
26 20
27 20
28 20
29 20
30 30
31 30
32 30
33 30
34 30
35 30
36 30
37 30
38 30
39 30
40 40
41 40
42 40
40 40
44 40
45 40
46 40
47 40
48 40
49 40
這是因爲在平等的第二部分你用「(int)」來整數。 10 *(int)的(0.1 *指數),15就變成 10 *(int)的(0.1 * 15),這成爲 10 *((int)的1.5)變成 10 * 1 – sashoalm 2011-03-25 17:44:06
克的哪個版本++你正在用嗎?適用於我。 – jbp 2011-03-25 17:39:20