不调用库实现反三角函数
自己实现反正切反余切函数大佬们看看我代码有什么错误 怎么运行总是错误啊
#include <iostream>
using namespace std;
double arctan(double);
double arccot(double);
double abs(double);
double abc;
const int pi=3.1415926;
int main()
{
cin>>abc;
cout<<abc<<"的arctan值为"<<arctan(abc)<<endl;
cout<<abc<<"的arccot值为"<<arccot(abc)<<endl;
return 0;
}
double arctan(double x)
{
if(-1<x<1)
{
double sum=x,x_pow=x,item;
int n=1,fact=1,sign=1;
do{fact = fact*(n + 1)*(n + 2);
x_pow *=x*x;fact;
sign=-sign;
item=x_pow/(n+2)*sign;
sum +=item;
n +=2;
}
while(abs(item)>0.000001);
return sum;
}
else if(x>=1)
{
double sum=x+pi/2,x_pow=x,item;
int n=1,sign=1;
do{x_pow *=x*x;
sign=-sign;
item=-1/(x_pow)*(n+2)*sign;
sum +=item;
n +=2;}
while(abs(item)>0.000001);
return sum;}
else if(x<=-1)
{double sum=x-pi/2,x_pow=x,item;
int n=1,sign=1;
do{
x_pow *=x*x;
sign=-sign;
item=-1/(x_pow)*(n+2)*sign;
sum +=item;
n +=2;}
while(abs(item)>0.000001);
return sum;
}
}
double arccot(double x)
{
double sum=1/x+1/(6*x*x*x),x_pow=1/(x*x*x),item;
int n=1,fact=2,faxt=1;
if(abs(x)>1)
{
do{
fact=fact*(n+2);
faxt=faxt*(n+2);
x_pow*=1/(x*x);
fact;faxt;
item=x_pow*faxt/(fact*(n+4));
sum+=item;n+=2;}
while(abs(item)>0.000001);
return pi/2-sum;}
}
double abs(double x)
{
return ((x >= 0 ? x : -x));
}