acos()是C++ STL中的内置函数,它与数学中的余弦逆数相同。 acos()函数返回以弧度表示的角度[0,?]范围内的值。
句法 :
acos(data_type x)
参数:该函数接受一个强制性参数x,该参数x指定应计算其余弦逆数的值。 x必须在[-1,1]范围内才能找到有效的输出[0,?],否则acos(x)函数返回NaN(Not a Number)。参数x可以是double,float或long double数据类型。
返回值:该函数返回以弧度为单位的[0 ,?]范围内的角度。逆时针角度以弧度为单位。
程序1:
C++
// C++ program to demonstrate
// the acos() function
#include
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;
}
C++
// C++ program to demonstrate
// the acos() function
#include
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;
}
C++
// C++ program to demonstrate the acos()
// function errors and exceptions
#include
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;
}
C++
// C++ program to demonstrate the
// acos() function errors and exceptions
#include
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(1.0) = 0 radians
acos(1.0) = 0 degrees
程式2:
C++
// C++ program to demonstrate
// the acos() function
#include
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
错误和异常:
- 当字符串或字符作为参数传递时,该函数返回任何匹配函数来调用错误。
- 当将域外(domain [-1,1])编号作为参数传递时,该函数将返回nan。
下面的程序说明了上述方法的错误和异常:
程序3:
C++
// C++ program to demonstrate the acos()
// function errors and exceptions
#include
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;
}
Ouput:
prog.cpp:10:17: error: no matching function for call to 'acos(std::__cxx11::string&)'
result = acos(x);
当参数x> 1或x <-1时,它将给出nan(不是数字)。
计划4:
C++
// C++ program to demonstrate the
// acos() function errors and exceptions
#include
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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。