首頁 >後端開發 >C++ >C++程式來找出給定值的反餘弦值

C++程式來找出給定值的反餘弦值

王林
王林轉載
2023-08-26 10:01:06749瀏覽

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. Howso, 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刪除