C 標(biāo)準(zhǔn)庫(kù) <math.h>
cos(x)函數(shù)返回參數(shù)x的余弦值。
double cos(double x);
函數(shù)cos()接受一個(gè)以弧度為單位的參數(shù),并返回一個(gè)double類型的值。
cos()返回的值始終在-1到1之間。
它在<math.h>頭文件中定義。
[Mathematics] cosx = cos(x) [In C Programming]
為了將cos()用于long double或float,可以使用以下原型:
long double cosl(long double x); float cosf(float x);
傳遞給cos()函數(shù)的參數(shù)可以是任何值,可以是負(fù)數(shù)或正數(shù)。
#include <stdio.h>
#include <math.h>
#define PI 3.141592654
int main()
{
double arg = 30, result;
// 轉(zhuǎn)換成弧度
arg = (arg * PI) / 180;
result = cos(arg);
printf("cos %.2lf 余弦值= %.2lf", arg, result);
return 0;
}輸出結(jié)果
cos 0.52 余弦值 = 0.87