我有相當直接的問題。以下代碼打印出攝氏和華氏。 雖然我的問題是迭代的次數。對於少數例如開始0,停在10,步長爲1.1。循環結束後,它將打印出正確的迭代次數。積累錯誤
但是對於大數字0-11000000,在步驟1.1中會打印出錯誤的迭代次數。爲什麼會發生?由於1100000/1.1應該在1000001附近,但我得到了990293.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float start, stop, step;
int count = 0;
cout << "start temperature: ";
cin >> start;
cout << "stop temperature: ";
cin >> stop;
cout << "step temperature: ";
cin >> step;
cout << setw(10) << "celsius" << setw(15) << "fahrenheit" << endl;
cout << setw(25) << "celsius" << setw(15) << "fahrenheit" << endl;
while(start <= stop)
{
count++;
float c, f;
c = (5.0/9)*(start-32);
f = 32+(9.0/5)*start;
cout << setw(10) << fixed << setprecision(2) << c << setw(15) << start << setw(15) << fixed << setprecision(2) << f << " count: " << count << endl;
start = start + step;
}
cout << "The program loop made " << count << " iterations." << endl;
return 0;
}
而不是'start = start + step;'你應該用一個整數類型來計算步長,並且從步數中計算浮點值:put'float temp = start + step * count;'在count ++前面;'在你的數學中使用'temp'而不是'start'。還要將循環更改爲'while(start + step * count <= stop)'。 – 2010-09-12 14:42:31
*已刪除*因爲我的評論在我發表評論前得到了答覆 – starzdust 2010-09-12 14:46:13