使用泰勒级数执行计算 cos x 的代码时,无论我将角度设置为什么,答案都是 1
When executing the code for the computation of cos x using the Taylor Series, the answer comes out to be 1 no matter what I put the angle as
很抱歉占用了您宝贵的时间。我只是一个初级程序员,正在尝试编译一个相对简单的代码(对你们来说)。在我看来,我的功能是正确的,但是我的功能 'output' 的声明导致了一些问题,我发现很难找出我做错了什么。编译器在第 41 行和第 50 行指示了一条警告,我已在我的程序中突出显示了它。如果您能指出我的错误,我将不胜感激。
#include<simplecpp>
#include<cmath>
#define pi 3.1415926535897932384626433832795
class calculation{
private:
int i=1;
double t0=1;
double t1,R,sum=1;
double precision,degrees,radian,cosx;
public:
void getValue();
void radians();
double cosf(double radian);
void output();
};
void calculation::getValue(){
cout<<"Enter Angle: ";
cin>>degrees;
cout<<"Enter Precision: ";
cin>>precision;
}
void calculation::radians(){
degrees=abs(degrees);
radian=(degrees*pi)/180;
}
double calculation::cosf(double radian){
{
do{
R=-(radian*radian)/(2*i-1)/(2*i);
t1=R*t0;
sum=sum+t1;
t0=t1;
i++;
}
while(abs(t1/sum)>precision);
return sum;
}
}
void calculation::output(){
double cosx = cosf(radian);
cout<<"cosine of the angle will be: "<<cosx;
}
int main(){
calculation o1;
o1.getValue();
o1.radians();
double radian=0;
o1.cosf(radian);
o1.output();
}
编辑:我进行了必要的更改(按照@NotAProgrammer 的建议),但是在编译时,cos x 仍然显示值为 1。你能帮帮我吗?
您的代码中存在几个问题。首先,main program { ... }
不是 C++ 程序的有效入口(我假设您打算将其用作 main 函数)。每个 C++ 程序都必须包含一个 int main() { }
函数作为起点。所以你必须按如下方式重写它:
int main()
{
calculation o1;
o1.getValue();
o1.radians();
o1.cosf(a); //Function uses an undeclared variable
o1.output();
}
其次,您的输出问题是,在 calculation::output()
中,您使用的是本地未初始化变量 a
,用于计算 cosf
。 C++ 中有一些规则来管理变量在未提供初始值的情况下如何以及是否进行初始化,但它们很复杂,老实说,总是给它们一个(合理的)初始值会更容易。我假设 calculation
class 中的变量 x
代表弧度?在这种情况下,您必须将对象的 x
变量提供给函数。由于它是一个成员函数,您可以访问它的私有成员并且可以只引用 x。您还调用了 cosf()
两次,一次是在程序的主要部分(您忽略其输出),一次是在 output()
中 - 这是所需的功能吗?不仅如此,在 output()
中,您的 cosf()
函数 return 是一个 double
,但您将它分配给了一个 float
,这意味着潜在的精度损失。
第三,在你的 getValue()
函数中你读入了 x
作为角度,但是在 radians()
函数中你用弧度覆盖了你的度值。不推荐这样做——您如何知道在任何给定时间点您的变量是否包含以度数或弧度为单位的值?我还会以不同的方式命名您的变量 - 为什么将度数命名为 x
而将精度命名为 n
?只需称它们为 degrees
和 precision
。这使您的代码更清晰易读,而无需深入研究函数以了解这些变量的分配内容。
如果您想继续使用此代码,您可以将输出函数更改为此,假设 x 包含弧度,然后从那里开始。
void calculation::output() {
double cosx = cosf(x); //This is the calculation object variable that holds the radians
cout << "cosine of the angle will be: " << cosx;
}
一般来说,对于您要尝试做的事情,我认为不需要面向对象的方法。您可以改为编写一些采用角度和 return 弧度的自由函数,以及采用弧度和 returns 的 cosf 函数,无论总和是多少等等。
很抱歉占用了您宝贵的时间。我只是一个初级程序员,正在尝试编译一个相对简单的代码(对你们来说)。在我看来,我的功能是正确的,但是我的功能 'output' 的声明导致了一些问题,我发现很难找出我做错了什么。编译器在第 41 行和第 50 行指示了一条警告,我已在我的程序中突出显示了它。如果您能指出我的错误,我将不胜感激。
#include<simplecpp>
#include<cmath>
#define pi 3.1415926535897932384626433832795
class calculation{
private:
int i=1;
double t0=1;
double t1,R,sum=1;
double precision,degrees,radian,cosx;
public:
void getValue();
void radians();
double cosf(double radian);
void output();
};
void calculation::getValue(){
cout<<"Enter Angle: ";
cin>>degrees;
cout<<"Enter Precision: ";
cin>>precision;
}
void calculation::radians(){
degrees=abs(degrees);
radian=(degrees*pi)/180;
}
double calculation::cosf(double radian){
{
do{
R=-(radian*radian)/(2*i-1)/(2*i);
t1=R*t0;
sum=sum+t1;
t0=t1;
i++;
}
while(abs(t1/sum)>precision);
return sum;
}
}
void calculation::output(){
double cosx = cosf(radian);
cout<<"cosine of the angle will be: "<<cosx;
}
int main(){
calculation o1;
o1.getValue();
o1.radians();
double radian=0;
o1.cosf(radian);
o1.output();
}
编辑:我进行了必要的更改(按照@NotAProgrammer 的建议),但是在编译时,cos x 仍然显示值为 1。你能帮帮我吗?
您的代码中存在几个问题。首先,main program { ... }
不是 C++ 程序的有效入口(我假设您打算将其用作 main 函数)。每个 C++ 程序都必须包含一个 int main() { }
函数作为起点。所以你必须按如下方式重写它:
int main()
{
calculation o1;
o1.getValue();
o1.radians();
o1.cosf(a); //Function uses an undeclared variable
o1.output();
}
其次,您的输出问题是,在 calculation::output()
中,您使用的是本地未初始化变量 a
,用于计算 cosf
。 C++ 中有一些规则来管理变量在未提供初始值的情况下如何以及是否进行初始化,但它们很复杂,老实说,总是给它们一个(合理的)初始值会更容易。我假设 calculation
class 中的变量 x
代表弧度?在这种情况下,您必须将对象的 x
变量提供给函数。由于它是一个成员函数,您可以访问它的私有成员并且可以只引用 x。您还调用了 cosf()
两次,一次是在程序的主要部分(您忽略其输出),一次是在 output()
中 - 这是所需的功能吗?不仅如此,在 output()
中,您的 cosf()
函数 return 是一个 double
,但您将它分配给了一个 float
,这意味着潜在的精度损失。
第三,在你的 getValue()
函数中你读入了 x
作为角度,但是在 radians()
函数中你用弧度覆盖了你的度值。不推荐这样做——您如何知道在任何给定时间点您的变量是否包含以度数或弧度为单位的值?我还会以不同的方式命名您的变量 - 为什么将度数命名为 x
而将精度命名为 n
?只需称它们为 degrees
和 precision
。这使您的代码更清晰易读,而无需深入研究函数以了解这些变量的分配内容。
如果您想继续使用此代码,您可以将输出函数更改为此,假设 x 包含弧度,然后从那里开始。
void calculation::output() {
double cosx = cosf(x); //This is the calculation object variable that holds the radians
cout << "cosine of the angle will be: " << cosx;
}
一般来说,对于您要尝试做的事情,我认为不需要面向对象的方法。您可以改为编写一些采用角度和 return 弧度的自由函数,以及采用弧度和 returns 的 cosf 函数,无论总和是多少等等。