标题:不调用库实现反三角函数
取消只看楼主
莽夫啊
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-9-26
结帖率:100%
已结贴  问题点数:20 回复次数:0 
不调用库实现反三角函数
自己实现反正切反余切函数
大佬们看看我代码有什么错误   怎么运行总是错误啊
#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));
}
搜索更多相关主题的帖子: double abs return sum item 
2018-10-08 23:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-490254-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.113938 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved