vs19c++求arccos值

一、要求

利用标准库中的cos(x)和fabs(x)函数实现arccos(x)函数,x取值范围是[-1, 1],返回值为[0, PI]。要求结果准确到小数点后5位。(PI = 3.1415926)

输入:

一个数x

-1 <= x <= 1, 0 <= arccos(x) <= PI。

输出:

计算后的结果

二、分析

二分法

第一次[0,pi],再慢慢缩小范围,直到精度<0.000001

三、代码

#include <iostream>
#include<math.h>
using namespace std;
#define pi 3.1415926   //注意格式,没有等号!没有分号!
#define minn 0.000001
double n;
double twofen(double l, double h) {
    if (h - l < minn) return l;
    double result = (l + h) / 2;
    if (cos(result) > n)  return twofen(result, h);
    else if (cos(result) == n) return result;
    else if(cos(result) < n) return twofen(l,result);
}
int main() {
    cin >> n;
    printf("%.5lf", twofen(0, pi));
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值