标题:不调用库实现反三角函数
只看楼主
莽夫啊
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-9-26
结帖率:100%
已结贴  问题点数:20 回复次数:2 
不调用库实现反三角函数
自己实现反正切反余切函数
大佬们看看我代码有什么错误   怎么运行总是错误啊
#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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
我用VC++,报
const int pi=3.1415926; warning C4244: 'initializing' : conversion from 'double' to 'const int', possible loss of data
if(-1<x<1) warning C4804: '<' : unsafe use of type 'bool' in operation
warning C4715: 'arctan' : not all control paths return a value
warning C4715: 'arccot' : not all control paths return a value

2018-10-09 08:43
rohalloway
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:97
专家分:405
注 册:2018-9-28
得分:0 
共发现3处错误
第一
const double pi = 3.1415926; 这里用int pi = 3

第二
if(-1<x<1) 我觉得改成if ((x>-1) && (x<1))好点,
因为会先计算-1<x 无论结果是true还是false,结果可能会编程 true<1  或 false < 1
我也是新手,对这点不确定,但你的代码在我的VS里是有警告的

第三
abs()是C++的绝对值函数,你如果想自己实现它,不要起这个名字,有冲突


程序代码:
#include<iostream>
using namespace std;

const double pi = 3.1415926;

double arctan(double);
double arccot(double);
double absXXXXX(double);
double abc;

int main()
{

    cin >> abc;
    cout << abc << "的arctan值为" << arctan(abc) << endl;
    cout << abc << "的arccot值为" << arccot(abc) << endl;
    
    system("pause");
    return 0;
}

double arctan(double x)
{

    if ((x>-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 (absXXXXX(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 (absXXXXX(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 (absXXXXX(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 (absXXXXX(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 (absXXXXX(item)>0.000001);
        return pi / 2 - sum;
    }
}
double absXXXXX(double x)
{
    return ((x >= 0 ? x : -x));
}

2018-10-15 00:06



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




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

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