在 C++ 中使用泰勒级数计算 Pi 的函数
Function for calculating Pi using taylor series in c++
所以我不知道为什么我的代码不起作用,本质上我正在编写的函数使用泰勒级数计算 Pi 的估计值,每当我尝试 运行 该程序时它就会崩溃.
这是我的代码
#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
double get_pi(double accuracy)
{
double estimate_of_pi, latest_term, estimated_error;
int sign = -1;
int n;
estimate_of_pi = 0;
n = 0;
do
{
sign = -sign;
estimated_error = 4 * abs(1.0 / (2*n + 1.0)); //equation for error
latest_term = 4 * (1.0 *(2.0 * n + 1.0)); //calculation for latest term in series
estimate_of_pi = estimate_of_pi + latest_term; //adding latest term to estimate of pi
n = n + 1; //changing value of n for next run of the loop
}
while(abs(latest_term)< estimated_error);
return get_pi(accuracy);
}
int main()
{
cout << get_pi(100);
}
代码背后的逻辑如下:
- 定义所有变量
- 将 pi 的估计值设置为 0
- 计算泰勒级数中的一项并计算误差
这学期
- 然后它将最新项添加到 pi
的估计值中
- 然后程序应计算出系列中的下一项及其中的误差,并将其添加到 pi 的估计值中,直到满足 while 语句中的条件
感谢我可能得到的任何帮助
您的 return 陈述可能是原因。
尝试 returning "estimate_of_pi" 而不是 get_pi(准确性)。
你的中断条件可以改写为
2*n + 1 < 1/(2*n + 1) => (2*n + 1)^2 < 1
并且这永远不会是 true
任何积极的 n
。因此你的循环将永远不会结束。修复此问题后,您应该将 return 语句更改为
return estimated_error;
您目前正在无休止地递归调用该函数(假设您修复了停止条件)。
而且你有一个 sign
和你在计算中根本不使用的参数 accuracy
。
我对此类迭代的建议是始终在某个最大迭代次数时中断。在这种情况下,您知道它会收敛(假设您修正了数学),但通常您永远无法确定您的迭代会收敛。
您的函数中有几处错误。请参阅以“//注意:”开头的行的评论。
double get_pi(double accuracy)
{
double estimate_of_pi, latest_term, estimated_error;
int sign = -1;
int n;
estimate_of_pi = 0;
n = 0;
do
{
sign = -sign;
//NOTE: This is an unnecessary line.
estimated_error = 4 * abs(1.0 / (2*n + 1.0)); //equation for error
//NOTE: You have encoded the formula incorrectly.
// The RHS needs to be "sign*4 * (1.0 /(2.0 * n + 1.0))"
// ^^^^ ^
latest_term = 4 * (1.0 *(2.0 * n + 1.0)); //calculation for latest term in series
estimate_of_pi = estimate_of_pi + latest_term; //adding latest term to estimate of pi
n = n + 1; //changing value of n for next run of the loop
}
//NOTE: The comparison is wrong.
// The conditional needs to be "fabs(latest_term) > estimated_error"
// ^^^^ ^^^
while(abs(latest_term)< estimated_error);
//NOTE: You are calling the function again.
// This leads to infinite recursion.
// It needs to be "return estimate_of_pi;"
return get_pi(accuracy);
}
另外,main
中的函数调用是错误的。必须是:
get_pi(0.001)
表示如果项的绝对值小于0.001,函数可以return.
这是适合我的函数的更新版本。
double get_pi(double accuracy)
{
double estimate_of_pi, latest_term;
int sign = -1;
int n;
estimate_of_pi = 0;
n = 0;
do
{
sign = -sign;
latest_term = sign * 4 * (1.0 /(2.0 * n + 1.0)); //calculation for latest term in series
estimate_of_pi += latest_term; //adding latest term to estimate of pi
++n; //changing value of n for next run of the loop
}
while(fabs(latest_term) > accuracy);
return estimate_of_pi;
}
所以我不知道为什么我的代码不起作用,本质上我正在编写的函数使用泰勒级数计算 Pi 的估计值,每当我尝试 运行 该程序时它就会崩溃.
这是我的代码
#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
double get_pi(double accuracy)
{
double estimate_of_pi, latest_term, estimated_error;
int sign = -1;
int n;
estimate_of_pi = 0;
n = 0;
do
{
sign = -sign;
estimated_error = 4 * abs(1.0 / (2*n + 1.0)); //equation for error
latest_term = 4 * (1.0 *(2.0 * n + 1.0)); //calculation for latest term in series
estimate_of_pi = estimate_of_pi + latest_term; //adding latest term to estimate of pi
n = n + 1; //changing value of n for next run of the loop
}
while(abs(latest_term)< estimated_error);
return get_pi(accuracy);
}
int main()
{
cout << get_pi(100);
}
代码背后的逻辑如下:
- 定义所有变量
- 将 pi 的估计值设置为 0
- 计算泰勒级数中的一项并计算误差 这学期
- 然后它将最新项添加到 pi 的估计值中
- 然后程序应计算出系列中的下一项及其中的误差,并将其添加到 pi 的估计值中,直到满足 while 语句中的条件
感谢我可能得到的任何帮助
您的 return 陈述可能是原因。
尝试 returning "estimate_of_pi" 而不是 get_pi(准确性)。
你的中断条件可以改写为
2*n + 1 < 1/(2*n + 1) => (2*n + 1)^2 < 1
并且这永远不会是 true
任何积极的 n
。因此你的循环将永远不会结束。修复此问题后,您应该将 return 语句更改为
return estimated_error;
您目前正在无休止地递归调用该函数(假设您修复了停止条件)。
而且你有一个 sign
和你在计算中根本不使用的参数 accuracy
。
我对此类迭代的建议是始终在某个最大迭代次数时中断。在这种情况下,您知道它会收敛(假设您修正了数学),但通常您永远无法确定您的迭代会收敛。
您的函数中有几处错误。请参阅以“//注意:”开头的行的评论。
double get_pi(double accuracy)
{
double estimate_of_pi, latest_term, estimated_error;
int sign = -1;
int n;
estimate_of_pi = 0;
n = 0;
do
{
sign = -sign;
//NOTE: This is an unnecessary line.
estimated_error = 4 * abs(1.0 / (2*n + 1.0)); //equation for error
//NOTE: You have encoded the formula incorrectly.
// The RHS needs to be "sign*4 * (1.0 /(2.0 * n + 1.0))"
// ^^^^ ^
latest_term = 4 * (1.0 *(2.0 * n + 1.0)); //calculation for latest term in series
estimate_of_pi = estimate_of_pi + latest_term; //adding latest term to estimate of pi
n = n + 1; //changing value of n for next run of the loop
}
//NOTE: The comparison is wrong.
// The conditional needs to be "fabs(latest_term) > estimated_error"
// ^^^^ ^^^
while(abs(latest_term)< estimated_error);
//NOTE: You are calling the function again.
// This leads to infinite recursion.
// It needs to be "return estimate_of_pi;"
return get_pi(accuracy);
}
另外,main
中的函数调用是错误的。必须是:
get_pi(0.001)
表示如果项的绝对值小于0.001,函数可以return.
这是适合我的函数的更新版本。
double get_pi(double accuracy)
{
double estimate_of_pi, latest_term;
int sign = -1;
int n;
estimate_of_pi = 0;
n = 0;
do
{
sign = -sign;
latest_term = sign * 4 * (1.0 /(2.0 * n + 1.0)); //calculation for latest term in series
estimate_of_pi += latest_term; //adding latest term to estimate of pi
++n; //changing value of n for next run of the loop
}
while(fabs(latest_term) > accuracy);
return estimate_of_pi;
}