for 2, more details heresorry for my careless mistake you are actually using double here which is 64 bits in java, but the argument is the same. you can use the following program to find out what exactly happens:
class T {
public static void main (String args[]) {
double d = 0;
for(; d < 10.1; d += 0.1){
if(d > 9.8){
System.out.print(Long.toBinaryString(Double.doubleToLongBits(d))+" == >"+d+"\n");
}
}
System.out.print("\n\n"+Long.toBinaryString(Double.doubleToLongBits(10))+" == >10\n");
}
}
The output is like:
100000000100011110011001100110011001100110011001100110011000010 == >9.89999999999998
100000000100011111111111111111111111111111111111111111111110101 == >9.99999999999998
100000000100100001100110011001100110011001100110011001100101000 == >10.09999999999998
100000000100100000000000000000000000000000000000000000000000000 == >10
so ......
唔,第1,3,4题俺都懂了。谢谢学长....
第2题嘛,不好意思,没明白。可能俺还没学到那的缘故。是不是这个意思:计算机对于double 的数在represent时不会很精确,所以永远不会正好出现10.0, 因此就成了一个infinite loop?