首页 >后端开发 >C++ >C++程序来找到给定值的反余弦值

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

王林
王林转载
2023-08-26 10:01:06778浏览

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)

算法

  • 将余弦值 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中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除