爲什麼當我輸入0.1作爲步長時,我的代碼只能達到2.4的x值作爲輸出?如果我輸入一個像.01或.001這樣的值,它會上升到2.5。C++ Euler Approximation
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double step;
double x0 = 1.0;
double y0 = 1.0;
double diffY;
std::cout << std::fixed;
std::cout << std::setprecision(2);
cout << "Enter step value: ";
cin >> step;
while (x0 <= 2.5) {
diffY = x0 + ((3*y0)/x0);
cout << x0 << " " << y0 << "\n";
x0+=step;
y0+=step*(diffY);
}
return 0; //initially defined the main function to return an int
}
謝謝!
你應該循環使用基於整數的限制,並在循環內,向下縮放值。否則你的循環可能會根據編譯器,編譯器選項等而運行不同的次數。 – PaulMcKenzie