C語言中求余弦值的相關函數(shù)總結
C語言cos()函數(shù):求余弦值
頭文件:
#include <math.h>
cos() 函數(shù)用來求余弦值,即求角的臨邊長度除以斜邊長度的比值,其原型為:
double cos(double x);
【參數(shù)】x 為一個弧度。
【返回值】返回-1 至1 之間的計算結果。
弧度與角度的關系為:
弧度 = 180 / π 角度
角度 = π / 180 弧度
使用 rtod( ) 函數(shù)可以將弧度值轉換為角度值。
注意,使用 GCC 編譯時請加入-lm。
【實例】求兩個角度的余弦值并輸出,
#include<stdio.h>
#include<math.h>
int main(void)
{
double angl,result;
angl = 1;
result = cos(angl);/*求余弦值*/
printf("cos(%lf) is %lf\n",angl,result);/*格式化輸出*/
angl = 3.1415926;
result = cos(angl);/*求余弦值*/
printf("cos(%lf) is %lf\n",angl,result);/*格式化輸出*/
return 0;
}
運行結果:
cos(1.000000) is 0.540302 cos(3.141593) is -1.000000
程序中的參數(shù)都是直接使用的弧度值,如果只知 道角度,可以使用角度乘以 π / 180 的方法得到弧度值。
C語言cosh()函數(shù):求雙曲余玄值
頭文件:
#include <math.h>
cosh()用來計算參數(shù)x 的雙曲余玄值,然后將結果返回。其原型為:
double cosh(double x);
雙曲余弦的數(shù)學定義式為:
(exp(x)+exp(x))/2
即

注意,使用 GCC 編譯時請加入-lm。
雙曲余弦在區(qū)間 -5 <= x <= 5 上的函數(shù)圖像。

【實例】求0.5的雙曲余弦值。
#include <math.h>
main(){
double answer = cosh(0.5);
printf("cosh(0.5) = %f\n", answer);
}
運行結果:
cosh(0.5) = 1.127626
又如,求雙曲余弦上某一點的值。
#include<stdio.h>
#include<math.h>
int main(void)
{
double resut;
double x =1;
resut = cosh(x);/*求雙曲余弦值*/
printf("cosh(%lf) = %lf\n",x,resut);/*格式化輸出*/
return 0;
}
運行結果:
cosh(1.000000) = 1.543081
程序先定義兩個double型變量,resut保存計算結果,x提供雙曲余弦函數(shù)點。語句resut = cosh(x);的作用是求該函數(shù)上x點對應的數(shù)值,然后把結果賦值給resut。
C語言acos()函數(shù):求反余弦的值
頭文件:
#include <math.h>
acos() 函數(shù)返回一個以弧度表示的反余弦值,其原型為:
double acos (double x);
【參數(shù)】x 為余弦值,范圍為 -1 到 1 之間,超出此范圍將會導致錯誤,并設置 errno 的值為 EDOM.
【返回值】返回 0 至 π 之間的計算結果,單位為弧度,在函數(shù)庫中角度均以弧度來表示。
弧度與角度的關系為:
弧度 = 180 / π 角度
角度 = π / 180 弧度
注意:使用 GCC 編譯時請加入-lm。
【實例】求 0.5 的反余弦。
#include <math.h>
main(){
double angle;
angle = acos(0.5);
printf("angle = %f\n", angle);
}
運行結果:
angle = 1.047198
又如,由余弦值求對應的角度。
#include<stdio.h>
#include<math.h>
int main(void)
{
double angl,result;
angl = 1;
result =acos(cos(angl));/*求反余弦值*/
printf("acos(%lf) is %lf\n",cos(angl),result);/*格式化輸出*/
angl = 3.1415926;
result = acos(cos(angl));/*求反余弦值*/
printf("acos(%lf) is %lf\n",cos(angl),result);/*格式化輸出*/
return 0;
}
運行結果:
acos(0.540302) is 1.000000 acos (-1.000000) is 3.141593
這個例子可以對照余弦函數(shù)例子學習,示例中都是使用余弦值作為參數(shù),然后再使用 acos() 函數(shù)求出該角度以便對比。

