C 语言 cos()

cos() 函数定义在 math.h 头文件中。它有助于返回给定弧度值的余弦值。


double cos(double x); #where x can be in int, float or long double

此外,cosf() 和 cosl() 两个函数分别用于 float 和 long double 类型。


float cosf(float x); 
long double cosl(long double x); 

cos() 参数

cos() 函数接受一个参数,该参数表示以弧度表示的角度值。

参数 描述 必需/可选
双精度浮点数值 一个 double 类型的值,表示以弧度表示的角度 必需

cos() 返回值

cos() 函数的返回值是介于 1 和 -1 之间的数字。

输入 返回值
-1 < x ≥ 1 一个大于或等于 0 的数字
x < -1 NaN (非数字)

cos() 示例

示例 1:cos() 函数在 C 语言中如何工作?


#include <stdio.h>
#include <math.h>
#define PI 3.14159265

int main () {
   double v, out, value;

   v = 60.0;
   value = PI / 180.0;
   out = cos( v*value );
   printf("Cosine value of %lf is %lf degrees\n", v, out);
   
   v = 90.0;
   value = PI / 180.0;
   out = cos( v*value );
   printf("Cosine value of %lf is %lf degrees\n", v, out);
   
   return(0);
}

输出


Cosine value of 60.000000 is 0.500000 degrees
Cosine value of 90.000000 is 0.000000 degrees

示例 2:cos() 函数在 C 语言中如何工作?


#include <stdio.h>
#include <math.h>
#define PI 3.14159265

int main()
{
   double pr = 30, output;

   // Converting to radian
   pr = (pr * PI) / 180;
   output = cos(pr);

   printf("The cos value of %.2lf radian = %.2lf", pr, output);

   return 0;
}

输出


The cos value of 0.52 radian = 0.87