C++ 作业帮助,程序不返回值
如果我输入的值超过2,函数taylor2就不能返回值,这使我遇到了麻烦。如果我输入0-2,它会输出正确的值,但如果值超过2,我只会得到一个闪烁的下划线,没有返回任何数据C++ 作业帮助,程序不返回值,c++,C++,如果我输入的值超过2,函数taylor2就不能返回值,这使我遇到了麻烦。如果我输入0-2,它会输出正确的值,但如果值超过2,我只会得到一个闪烁的下划线,没有返回任何数据 void taylor2(double x) { double total = 1; int i = 0; int count = 1; double temp = 1; do { { if (i % 2 == 1)
void taylor2(double x)
{
double total = 1;
int i = 0;
int count = 1;
double temp = 1;
do
{
{
if (i % 2 == 1)
{
temp = (pow(x, i * 2 + 2) / factorial(i * 2 + 2));
total += temp;
}
else {
temp = (pow(x, i * 2 + 2) / factorial(i * 2 + 2));
total -= temp;
}
}
count++;
i++;
} while (fabs(temp) >= .0001);
cout << "The last recoreded temporary value was: "<<temp << endl;
cout << "The computed value for cosine is : "<< total << endl;
cout << "It took " <<count << " values to calculate the value of the function to .0001 places"<< endl;
cout << endl;
}
void taylor2(双x)
{
双倍合计=1;
int i=0;
整数计数=1;
双温=1;
做
{
{
如果(i%2==1)
{
温度=(功率(x,i*2+2)/阶乘(i*2+2));
总+=温度;
}
否则{
温度=(功率(x,i*2+2)/阶乘(i*2+2));
总-=温度;
}
}
计数++;
i++;
}而(晶圆厂温度>0.0001);
CUT< P>我怀疑阶乘< /C> >返回<代码> int <代码>。如果<代码> int >代码> 32位(非常常见),则<代码>阶乘< /C> >一旦参数达到13(在您的情况下<代码> i=5 < /代码>)。C++中的符号整数溢出是未定义的行为。
您可以使用std::uint64\u t
(一个无符号64位整数)。这将允许您计算几个较大的阶乘
有关更多参考,请参阅
更好的方法是,在泰勒项之间使用递归关系。void
意味着您的函数无法返回值。factorial()
可能有问题,请向我们展示它的代码