首页 > 后端开发 > C++ > 正文

C++程序来找到给定值的反余弦值

王林
发布: 2023-08-26 10:01:06
转载
903人浏览过

c++程序来找到给定值的反余弦值

Sine, cosine, tangent, and a few more ratios are some of the ones we utilize the most in trigonometry. These ratios can be computed from an angle. However, we can also determine the angle using inverse trigonometric functions if we know the ratio values.

在本教程中,我们将介绍如何使用C++的反余弦(arccosine)函数将余弦值转换为弧度角。

The acos() function

反余弦函数被用来使用acos()方法计算角度。这个函数可以在C++标准库中找到。为了使用这个方法,我们必须导入cmath库。这个函数接受余弦值作为参数,并返回以弧度为单位的角度。下面使用简单的语法:

Syntax

#include < cmath >
acos( <cosine value> )
登录后复制

The cosine value must be in the range [-1 to +1] (both included). Otherwise, a domain error will be raised, and it will return Not-A-Number (nan). The returned value will be in the range [0, π] (both included)

立即学习C++免费学习笔记(深入)”;

算法

  • 将余弦值 x 作为输入
  • 使用 acos( x ) 来计算 cos−1(x)
  • Return result.

Example

#include <iostream>
#include <cmath>
using namespace std;

float solve( float x ) {
   float answer;
   answer = acos( x );
   return answer;
}

int main()
{
   float angle, ang_deg;
   angle = solve( 0.7071067 );
   ang_deg = angle * 180 / 3.14159;

   cout << "The angle (in radian) for given cosine value 0.7071067 is: " << angle << " = " << ang_deg << " (in degrees)" << endl;

   angle = solve( 0.866025 );
   ang_deg = angle * 180 / 3.14159;

   cout << "The angle (in radian) for given cosine value 0.866025 is: " << angle << " = " << ang_deg << " (in degrees)" << endl;

   angle = solve( 1 );
   ang_deg = angle * 180 / 3.14159;

   cout << "The angle (in radian) for given cosine value 1 is: " << angle << " = " << ang_deg << " (in degrees)" << endl;
   
   angle = solve( 0 );
   ang_deg = angle * 180 / 3.14159;

   cout << "The angle (in radian) for given cosine value 0 is: " << angle << " = " << ang_deg << " (in degrees)" << endl;
}
登录后复制

Output

The angle (in radian) for given cosine value 0.7071067 is: 0.785398 = 45 (in degrees)
The angle (in radian) for given cosine value 0.866025 is: 0.5236 = 30.0001 (in degrees)
The angle (in radian) for given cosine value 1 is: 0 = 0 (in degrees)
The angle (in radian) for given cosine value 0 is: 1.5708 = 90.0001 (in degrees)
登录后复制

在这里,正弦值被传递给acos()方法,该方法返回以弧度格式表示的角度。使用以下公式,我们将这个输出从弧度转换为度。

$$\mathrm{\theta_{deg}\:=\:\theta_{rad}\:\times\:\frac{180}{\pi}}$$

Conclusion

要从余弦值执行反三角函数操作,我们使用cmath库中的acos()函数。该函数以余弦值作为输入,并返回以弧度为单位的给定角度。在旧版本的C / C++中,返回类型为double,但后来的C++版本还使用了针对float和long-double的重载形式。当将整数值作为参数传递时,它将把输入参数转换为double并调用与double类型参数对应的acos()方法。

以上就是C++程序来找到给定值的反余弦值的详细内容,更多请关注php中文网其它相关文章!

c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号