acos()是C++ STL中的內置函數,該函數返回弧度數(參數)的反餘弦值。 acos()函數返回的值始終位於–到+
用法:
acos(data_type x)
參數:此函數接受一個強製性參數x,該參數指定應計算其反餘弦值的值。它必須介於-1和+1之間,否則將拋出域錯誤(範圍錯誤)。該參數可以是double,float或long double數據類型。
返回:該函數返回介於–和+。逆時針角度以弧度為單位。
示例1:
// C++ program to demonstrate
// the acos() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
double x = 1.0;
// Function call to calculate acos(x) value
double result = acos(x);
cout << "acos(1.0) = " << result << " radians" << endl;
cout << "acos(1.0) = " << result * 180 / 3.141592
<< " degrees" << endl;
return 0;
}
輸出:
acos(1.0) = 0 radians acos(1.0) = 0 degrees
示例2:
// C++ program to demonstrate
// the acos() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
double result;
int x = -1;
// Function call to calculate acos(x) value
result = acos(x);
cout << "acos(-1) = " << result
<< " radians" << endl;
cout << "acos(-1) = " << result * 180 / 3.141592
<< " degrees" << endl;
return 0;
}
輸出:
acos(-1) = 3.14159 radians acos(-1) = 180 degrees
錯誤和異常:
- 當字符串或字符作為參數傳遞時,該函數不返回任何匹配函數來調用錯誤。
- 當傳遞超出範圍的數字作為參數時,該函數將返回nan。
以下示例程序旨在說明上述方法的錯誤和異常:
示例3:
// C++ program to demonstrate the acos()
// function errors and exceptions
#include <bits/stdc++.h>
using namespace std;
int main()
{
double result;
string x = "gfg";
result = acos(x);
cout << "acos(x) = " << result
<< " radians" << endl;
cout << "acos(x) = " << result * 180 / 3.141592
<< " degrees" << endl;
return 0;
}
輸出:
prog.cpp:10:17: error: no matching function for call to 'acos(std::__cxx11::string&)' result = acos(x);
示例4:
// C++ program to demonstrate the
// acos() function errors and exceptions
#include <bits/stdc++.h>
using namespace std;
int main()
{
double x = 3.7, result;
// Function call to calculate acos(x) value
result = acos(x);
cout << "acos(3.7) = " << result
<< " radians" << endl;
cout << "acos(3.7) = " << result * 180 / 3.141592
<< " degrees" << endl;
return 0;
}
輸出:
acos(3.7) = nan radians acos(3.7) = nan degrees
相關用法
- C++ valarray acos()用法及代碼示例
- C++ complex acos()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ div()用法及代碼示例
- C++ log()用法及代碼示例
- C++ towupper()用法及代碼示例
- C++ iswalpha()用法及代碼示例
- C++ wcstombs()用法及代碼示例
- C++ strtol()用法及代碼示例
- C++ exp2()用法及代碼示例
- C++ feupdateenv()用法及代碼示例
- C++ fread()用法及代碼示例
注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 acos() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。