栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ |不使用库函数实现 arctan x

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ |不使用库函数实现 arctan x

原理是利用 arctan(x) 的泰勒展开式;同时当 x 的绝对值大于 1 时,利用arctan(x) + arctan(1/x) = pi/2 计算。

#include 
#include 

using namespace std;
const double pi = 3.1415926;

double tarctan(double x){
    double sqr = x * x;
    double e = x;
    double r = 0;
    int i = 1;
    while (fabs(e / i) > 1e-15){
        double f = e / i;
        r = (i % 4 == 1)? r + f : r - f;
        e *= sqr;
        i += 2;
    }
    return r;
}

double arctan(double x){
    if (x >= -1 && x <= 1)
        return tarctan(x)
    else{
        if (x > 0)
            return (pi/2 - tarctan(1 / x));
        else
            return (-pi/2 - tarctan(1 / x));
   }//else
}//arctan

int main(){
    double x;
    cin >> x;
    cout << "arctan(x): " << arctan(x) << endl;
    cout << "atan(x): " << atan(x) << endl;
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779049.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号