C 语言 COS 函数

C 语言的 cos 函数是一个数学库函数,用于计算指定表达式的三角余弦值。cos 函数的语法如下所示。

double cos(double number);

COS 函数将返回介于 -1 和 1 之间的值。在开始介绍语法之前,让我们先看一下这个三角余弦函数背后的数学公式。

cos(x) = 邻边长度 / 斜边长度

C 语言 COS 函数示例

数学库中的 cos 函数允许您查找指定值的三角余弦。

C 程序 要求用户输入自己的值,然后计算用户指定值的余弦值。

提示:请参考 C 语言编程中的 ACOS 函数math 文章,以计算指定表达式的反正余弦。

#include <stdio.h>
#include <math.h>

int main()
{
  double cosValue, number;
  printf(" Please Enter the Value to calculate Cosine :  ");
  scanf("%lf", &number);
  
  cosValue = cos(number);
  
  printf("\n Cosine value of %lf = %f ", number, cosValue);
  
  return 0;
}
COS Function 1

COSINE 函数示例 2

在此余弦函数示例中,我们允许用户输入角度值。然后我们将角度转换为弧度。最后,我们查找该弧度的余弦值。

#include <stdio.h>
#include <math.h>

#define PI 3.14
int main()
{
  double cosValue, radianVal, degreeVal;
  printf(" Please Enter an Angle in degrees :  ");
  scanf("%lf", &degreeVal);
  
  // Convert Dgree Value to Radian 
  radianVal = degreeVal * (PI/180);
  cosValue = cos(radianVal);
  
  printf("\n Cosine value of %f = %f ", degreeVal, cosValue);
  
  return 0;
}
 Please Enter an Angle in degrees :  35

 Cosine value of 35.000000 = 0.819330