gpt4 book ai didi

c++ - 同一个递归函数为什么VS和Linux的结果不同

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:07 24 4
gpt4 key购买 nike

我正在尝试解决 PI。为此,我构建了一个递归函数来解决它。这是代码 cpp.sh/5f76s .当我输入 1000 时在 Visual Studio 上使用控制台应用程序项目我得到 3.14159265558978351 , 但在 cpp.shonlinegdb我假设两者都运行 Linux,我得到 3.22741027776071876 . linux为什么关了。下次遇到这种情况应该用什么工作

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double Solver(int counter, int group,
int stop, long double partialanswer)
{
round(counter);
long double Counter = counter;
if (Counter >= stop)
{
return partialanswer;
}
//partialanswer = round(partialanswer *
pow(2, 54)) / pow(2, 54);
if (group % 2 != 0)
{
partialanswer = partialanswer + (4 /
((Counter) * (Counter + 1) * (Counter +
2)));
}
else
{
partialanswer = partialanswer - (4 /
((Counter) * (Counter + 1) * (Counter +
2)));
}
group = group++;
Solver((counter + 2), group, stop,
partialanswer);
}

int main()

{
int UserInput;
long double Answer = 0;
cin >> UserInput;
Answer = 3 + (Solver(2, 1, UserInput,
0));
cout << "PI = " << setprecision(18) <<
Answer << endl;
}

最佳答案

这不是递归的问题。相反,

 group = group++;

引起了问题。只是

group++;

有效。

关于c++ - 同一个递归函数为什么VS和Linux的结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58176367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com