C ++中的cos()函數返回以弧度表示的角度(參數)的余弦值。
此函數在<cmath>頭文件中定義。
[Mathematics] cos x = cos(x) [C++]
double cos(double x); float cos(float x); long double cos(long double x); double cos(T x); //在此,T是整數類型。
cos()函數采用一個以弧度為單位的強制性參數。
cos()函數返回[-1,1]范圍內的值。返回的值是double,float或long double。
#include <iostream> #include <cmath> using namespace std; int main() { double x = 0.5, result; result = cos(x); cout << "cos(x) = " << result << endl; double xDegrees = 25; //將角度轉換為弧度 x = xDegrees*3.14159/180; result = cos(x); cout << "cos(x) = " << result << endl; return 0; }
運行該程序時,輸出為:
cos(x) = 0.877583 cos(x) = 0.906308
#include <iostream> #include <cmath> using namespace std; int main() { int x = 1; double result; result = cos(x); cout << "cos(x) = " << result << endl; return 0; }
運行該程序時,輸出為:
cos(x) = 0.540302