次の問題があります。QtIDEを使用してコードを記述しています。他のIDE(コードブロックやビジュアルスタジオなど)でコンパイルしようとすると、出力が異なり、maufunctionsがあるとのことです。これを引き起こしている可能性のあるアイデアはありますか?例を挙げましょう:
これは、ルートが2.83somethingである関数を使用したニュートン法です。Qtで実行するたびに、同じ正しい計算が得られます。私はコードブロックで「nan」を取得し、ビジュアルスタジオでも無関係なものを取得します。わかりませんが、コードのどこかに間違いがありますか?これを引き起こしている可能性があるのは何ですか?
#include <iostream>
#include <cmath> // we need the abs() function for this program
using namespace std;
const double EPS = 1e-10; // the "small enough" constant. global variable, because it is good programming style
double newton_theorem(double x)
{
double old_x = x; // asign the value of the previous iteration
double f_x1 = old_x*old_x - 8; // create the top side of the f(x[n+1] equation
double f_x2 = 2 * old_x; // create the bottom side
double new_x = old_x - f_x1 / f_x2; // calculate f(x[n+1])
//cout << new_x << endl; // remove the // from this line to see the result after each iteration
if(abs(old_x - new_x) < EPS) // if the difference between the last and this iteration is insignificant, return the value as a correct answer;
{
return new_x;
}
else // if it isn't run the same function (with recursion YAY) with the latest iteration as a starting X;
{
newton_theorem(new_x);
}
}// newton_theorem
int main()
{
cout << "This program will find the root of the function f(x) = x * x - 8" << endl;
cout << "Please enter the value of X : ";
double x;
cin >> x;
double root = newton_theorem(x);
cout << "The approximate root of the function is: " << root << endl;
return 0;
}//main