Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.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/oop/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
在制作Gregory-Leibniz级数以找到pi值时遇到困难 我是C++新手,我很难弄清楚我的问题在哪里。我试图询问用户,他们希望使用Gregory-Leibniz数列找到π的距离,但是每当我输入任何数字时,我总是得到4(从循环中得到1)。提前感谢:) #包括 #包括 #包括 使用名称空间std; int main() { 双x=0; 双双项=0; cout双项; cout_C++ - Fatal编程技术网

在制作Gregory-Leibniz级数以找到pi值时遇到困难 我是C++新手,我很难弄清楚我的问题在哪里。我试图询问用户,他们希望使用Gregory-Leibniz数列找到π的距离,但是每当我输入任何数字时,我总是得到4(从循环中得到1)。提前感谢:) #包括 #包括 #包括 使用名称空间std; int main() { 双x=0; 双双项=0; cout双项; cout

在制作Gregory-Leibniz级数以找到pi值时遇到困难 我是C++新手,我很难弄清楚我的问题在哪里。我试图询问用户,他们希望使用Gregory-Leibniz数列找到π的距离,但是每当我输入任何数字时,我总是得到4(从循环中得到1)。提前感谢:) #包括 #包括 #包括 使用名称空间std; int main() { 双x=0; 双双项=0; cout双项; cout,c++,C++,1/(2*n+1);将以整数算法执行,因此任何分数部分都将被截断 一个修正是写代码>1(/ 2×n+1),而不是:然后,由于类型升级的规则,这个术语将在浮点中被评估。你可以检查关于你的分区的情况:作为一个建议,你可以考虑使用一个更有效但非常简单的系列,NILKANTANA系列。 #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() {

1/(2*n+1);
将以整数算法执行,因此任何分数部分都将被截断


一个修正是写代码>1(/ 2×n+1),而不是<代码>:然后,由于类型升级的规则,这个术语将在浮点中被评估。

你可以检查关于你的分区的情况:作为一个建议,你可以考虑使用一个更有效但非常简单的系列,NILKANTANA系列。
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
double x = 0;
double doubleTerms = 0;
cout << "How many terms of pi do you want?" << endl;
cin >> doubleTerms;

cout << "Thanks the amount of terms you want for pi is: " << doubleTerms << endl;
//n is the value for where each number is
//z is to change the number into a negative
//x is to add up into the total value
for(int n = 0; n < doubleTerms; n++)
{
    double z = 1 / (2 * n + 1);

    if((n % 2) == 1)
    {
        z = z * -1;
    }

    x = (x + z);
}
double finalValue = 4 * x;
cout << "The number is: " << finalValue << endl;


system("pause");
return 0;
}