📜  C++ acos()

📅  最后修改于: 2020-09-25 07:19:46             🧑  作者: Mango

C++中的acos() 函数以弧度返回数字(参数)的反余弦值。

此函数在头文件中定义。

[Mathematics] cos-1x = acos(x) [In C++ Programming];

acos()原型[从C++ 11标准开始]

double acos(double x);
float acos(float x);
long double acos(long double x);
double acos (T x); // For integral type

acos()参数

acos() 函数采用[-1,1]范围内的单个必需参数。这是因为余弦值在1到-1的范围内。

acos()返回值

假定参数在[-1,1]范围内,则acos() 函数返回在[0,π]范围内的值。

如果参数大于1或小于-1,则acos()返回NaN即不是数字。

Parameter (x) Return Value
x = [-1, 1] [0, π] in radians
-1 > x or x > 1 NaN (Not a Number)

示例1:acos()如何工作?

#include 
#include 

using namespace std;

int main()
{
  double x = 0.0, result;

  result = acos(x);
  cout << "acos(x) = " << result << " radians" << endl;
  
  // result in degrees
  cout << "acos(x) = " << result*180/3.1415 << " degrees" << endl;
  
  return 0;
}

运行该程序时,输出为:

acos(x) = 1.5708 radians
acos(x) = 90.0027 degrees

示例2:具有整数类型的acos() 函数

#include 
#include 

using namespace std;

int main()
{
  int x = -1;
  double result;

  result = acos(x);
  
  cout << "acos(x) = " << result << " radians" << endl;
  // Converting result to degrees
  cout << "acos(x) = " << result*180/3.1415 << " degrees";
  
  return 0;
}

运行该程序时,输出为:

acos(x) = 3.14159 radians
acos(x) = 180.005 degrees