我正在用C ++编写一个cos(x)
函数,但是得到的结果是无穷大,只是它应该是-1/3
。
这是我的代码:
#include <iostream>
#include <math.h>
using namespace std;
阶乘函数:
int factorial(unsigned int n)
{
unsigned long factorial = 1;
for(int o=1;o<=n;o++)
{
factorial *= o;
}
}
int main()
{
double x;
double answre;
double input;
cin>>input;
for(int i=0;i<2;i++)
{
double y=2*(i)+2;
我在这里声明y
而不是直接实现其值,因为我认为它是瞬时除以阶乘,这也是所有括号的原因。
x=((pow(input,2*(i)+2))/(factorial(y)))*(pow(-1,(i)+1));
x=+x;
}
answere=1+x;
cout<<answere<<endl;
return 0;
}
答案 0 :(得分:2)
您不返回任何阶乘
答案 1 :(得分:0)
x=((pow(input,2*(i)+2))/(factorial(y)))*(pow(-1,(i)+1));
x=+x; // ???
如果要添加到x
,只需输入x += ...
。您所做的是将{em> assign 分配为x
一个(!),然后将x
设置为其自身。您从不添加任何东西。