📜  C++ cos()

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

C++中的cos() 函数返回以弧度表示的角度(参数)的余弦值。

此函数在头文件中定义。

[Mathematics] cos x = cos(x) [In C++ Programming]

cos()原型(从C++ 11标准开始)

double cos(double x);
float cos(float x);
long double cos(long double x);
double cos(T x); // Here, T is an integral type.

cos()参数

cos() 函数采用一个以弧度为单位的强制性参数。

cos()返回值

cos() 函数返回[-1,1]范围内的值。返回的值可以是doublefloatlong double

示例1:cos()如何在C++中工作?

#include 
#include 

using namespace std;

int main()
{
  double x = 0.5, result;
  
  result = cos(x);
  cout << "cos(x) = " << result << endl;
  
  double xDegrees = 25;
  
  // converting degrees to radians
  x = xDegrees*3.14159/180;
  result = cos(x);
  
  cout << "cos(x) = " << result << endl;

  return 0;
}

运行该程序时,输出为:

cos(x) = 0.877583
cos(x) = 0.906308

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

#include 
#include 
using namespace std;

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

  // result is in double
  result = cos(x);
  cout << "cos(x) = " << result << endl;
  
  return 0;
}

运行该程序时,输出为:

cos(x) = 0.540302