c++ acos函数
C ++ acos()函数 (C++ acos() function)
acos() function is a library function of cmath header, it is used to find the principal value of the arc cosine of the given number, it accepts a number (x) and returns the principal value of the arc cosine of x in radians.
ACOS()函数是CMATH报头的库函数,它被用于查找给定数的反余弦的主值,它接受一个数字(x)和返回x的以弧度为单位的反余弦的主要值。
Note: Value (x) must be between -1 to +1, else it will return a domain error (nan).
注意:值( x )必须介于-1到+1之间,否则它将返回域错误( nan )。
Syntax of acos() function:
acos()函数的语法:
acos(x);
Parameter(s): x – is the value whose arc cosine to be calculated.
参数: x –是要计算其反余弦值的值。
Return value: double – it returns double type value that is the principal value of the arc cosine of the given number x.
返回值: double-它返回double类型值,该类型值是给定数字x的反余弦的主要值。
Example:
例:
Input:
float x = 0.65;
Function call:
acos(x);
Output:
0.863212
C ++代码演示acos()函数的示例 (C++ code to demonstrate the example of acos() function)
// C++ code to demonstrate the example of
// acos() function
#include <iostream>
#include <cmath>
using namespace std;
// main() section
int main()
{
float x;
x = -1.0;
cout<<"acos("<<x<<"): "<<acos(x)<<endl;
x = -0.89;
cout<<"acos("<<x<<"): "<<acos(x)<<endl;
x = 0.65;
cout<<"acos("<<x<<"): "<<acos(x)<<endl;
x = 1;
cout<<"acos("<<x<<"): "<<acos(x)<<endl;
return 0;
}
Output
输出量
acos(-1): 3.14159
acos(-0.89): 2.66814
acos(0.65): 0.863212
acos(1): 0
Example with domain error
域错误示例
If we provide the value out of the range (except -1 to +1), it returns nan.
如果我们提供的值超出范围(-1到+1除外),它将返回nan 。
// C++ code to demonstrate the example of
// acos() function
#include <iostream>
#include <cmath>
using namespace std;
// main() section
int main()
{
float x;
x = -0.89; //no error
cout<<"acos("<<x<<"): "<<acos(x)<<endl;
x = 2.65; //error
cout<<"acos("<<x<<"): "<<acos(x)<<endl;
x = -1.25; //error
cout<<"acos("<<x<<"): "<<acos(x)<<endl;
return 0;
}
Output
输出量
acos(-0.89): 2.66814
acos(2.65): nan
acos(-1.25): nan
Reference: C++ acos() function
参考: C ++ acos()函数
翻译自: https://www.includehelp.com/cpp-tutorial/acos-function-with-example.aspx
c++ acos函数