1. 程式人生 > >《算法競賽入門指導》Ch2的思考題2

《算法競賽入門指導》Ch2的思考題2

運行 std clas it is 程序 exp 提示 als ng-

題目2。下面的程序運行結果是什麽?“!=”運算符表示“不相等”。提示:請上機實
驗,不要憑主觀感覺回答。
#include<stdio.h>
int main()
{
double i;
for(i = 0; i != 10; i += 0.1)
printf("%.1f\n", i);
return 0;
}

事實上,這個算法會一直滾下去,就想死循環。

原來是因為

It is due to IEEE floating point standard.

0.1 + 0.2 = 0.3 => FALSE - Expected: 0.3 | Real: 0.30000000000000004

《算法競賽入門指導》Ch2的思考題2