zoukankan      html  css  js  c++  java
  • 使用函数求余弦函数的近似值(15 分)

    本题要求实现一个函数,用下列公式求cos(x)的近似值,精确到最后一项的绝对值小于e:

    cos(x)=x0​​/0!x2​​/2!+x4​​/4!x6​​/6!+

    函数接口定义:

    double funcos( double e, double x );
    

    其中用户传入的参数为误差上限e和自变量x;函数funcos应返回用给定公式计算出来、并且满足误差要求的cos(x)的近似值。输入输出均在双精度范围内。

    裁判测试程序样例:

    #include <stdio.h>
    #include <math.h>
    
    double funcos( double e, double x );
    
    int main()
    {    
        double e, x;
    
        scanf("%lf %lf", &e, &x);
        printf("cos(%.2f) = %.6f
    ", x, funcos(e, x));
    
        return 0;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例:

    0.01 -3.14
    

    输出样例:

    cos(-3.14) = -0.999899


    你需要粘贴的代码

    double funcos( double e, double x )
    {
        double tmp1=1,tmp2=1,tmp3=1,sum=1;
        int i,k;
    
        k=-1;
        for(i=2;tmp1>e;i+=2) 
        {    
            tmp2 = tmp2*x*x;        // 分子
            tmp3 = tmp3*i*(i-1);    // 分母
            sum =sum + k*tmp2/tmp3; //
            tmp1 = tmp2/tmp3;        // 误差上限
            k = -k;                    // 改变符号
        }
        return sum;
    }
  • 相关阅读:
    TensorFlow object detection API
    bounding box的简单理解
    OverFeat学习
    tensorflow调试tfdbg
    2018.7-2019.4记录
    人工智能未来读后感 ----by RayKurzweil
    matlab exe
    神经网络的基础
    研究生的论文
    卷积神经网络经验
  • 原文地址:https://www.cnblogs.com/chuijingjing/p/8664889.html
Copyright © 2011-2022 走看看