Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 不同的IDE';什么是不同的输出?_C++_Ide_Compilation - Fatal编程技术网

C++ 不同的IDE';什么是不同的输出?

C++ 不同的IDE';什么是不同的输出?,c++,ide,compilation,C++,Ide,Compilation,我有以下问题:我用qtide编写代码。我被告知,当人们试图用其他IDE(如代码块或visual studio)编译它时,他们得到的输出是不同的,并且有不同的函数。你知道这是什么原因吗?我给大家举个例子: 这是牛顿的方法,函数的根是2.83。每次在Qt中运行时,我都会得到相同的、正确的计算结果。我在代码块中得到“nan”,在VisualStudio中也得到一些无关的东西。我不明白,我的代码中有错误吗?这是什么原因造成的 #include <iostream> #include <

我有以下问题:我用qtide编写代码。我被告知,当人们试图用其他IDE(如代码块或visual studio)编译它时,他们得到的输出是不同的,并且有不同的函数。你知道这是什么原因吗?我给大家举个例子:

这是牛顿的方法,函数的根是2.83。每次在Qt中运行时,我都会得到相同的、正确的计算结果。我在代码块中得到“nan”,在VisualStudio中也得到一些无关的东西。我不明白,我的代码中有错误吗?这是什么原因造成的

#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
#包括
#include//我们需要这个程序的abs()函数
使用名称空间std;
常数双EPS=1e-10;//“足够小”常数。全局变量,因为它是良好的编程风格
双牛顿定理(双x)
{
double old_x=x;//指定上一次迭代的值
double f_x1=old_x*old_x-8;//创建f(x[n+1]方程的顶部
double f_x2=2*old_x;//创建底部
double new_x=old_x-f_x1/f_x2;//计算f(x[n+1])
//cout是的,您遇到了未定义的行为:

if(abs(old_x-new_x)/*return*/newton_定理(new_x);//Uhm…UB?那是什么?如果else语句中有一个return,它只是将retrun作为“函数的根”打印出来。我现在对这个问题有了一个很好的想法。你知道我该如何解决这个问题吗?@Bloodcount不,我没有再检查任何代码,这只是突然出现了-这肯定是个问题-其余的问题你可以通过调试解决。既然我是用递归来解决的,除非满足特定条件,否则不必重新运行吗?@Bloodcount否,您必须在所有路径上都有返回。我感觉您不了解递归是如何工作的。例如,假设我要计算阶乘。我将编写
int-fact(int-x){如果(x==0)返回1,否则返回x*fact(x-1);}
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;
{
    /*return*/ newton_theorem(new_x); // <<--- HERE!
}