c语言输入精度求 值,C语言 输入精度e 和实数x,用下列公式求cos x 的近似值,精确到最后一项的绝对值小于e。输入精度e 和实数x,用下列公式求cos x 的近似值,精确到最后一项的绝对值小于e...

#include  //头文件置顶

#include 

double fact(int n)

{

\x09double s = 1;

\x09for(int i = 1; i <= n; i++)

\x09{

\x09\x09s *= i;

\x09}

\x09return s;

}

double funcos(double e,int x)

{

\x09//运算次序要理清

\x09double cos  = 0;

\x09double item = 1;

\x09int       i = 0;

\x09int    flag = 1;

\x09while(abs(item) >= e)

\x09{

\x09\x09item = flag * pow(double (x), 2 * i) / fact(2 * i);

\x09\x09cos += item;

\x09\x09flag *= -1;

\x09\x09i++;

\x09}

\x09return cos;

}

int main()

{

\x09int    x = 0;

\x09double e = 0;

\x09printf("e: ");

\x09scanf("%lf",&e);

\x09printf("x: ");

\x09scanf("%d",&x);

\x09printf("cos(x) = %.3lf\n",funcos(e, x)); //唯一的缺陷就是3必须随e的变化手动改写

\x09return 0;

}

运行结果:

20103a392938e11eb367b22577c50fa4.png

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值