非常に率直な質問があります。次のコードは、摂氏と華氏を出力します。私の質問は、それが繰り返される回数についてです。少数の場合、たとえば0を開始し、1.1のステップで10で停止します。ループが終了すると、実行した正しい反復回数が出力されます。
ただし、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;
}