1. 前言
C 語言廣泛應用於系統開發與嵌入式系統,並在需要高速處理的場景中展現其效能。特別是在數學計算中,三角函數被廣泛用於物理模擬、圖形繪製、訊號處理等多種情境。
本文將詳細解說 C 語言中三角函數的基礎用法與進階應用。初學者可藉此扎實掌握基礎,中高階開發者則可透過應用範例提升實作能力。
本文將學到的內容
- C 語言三角函數的基本用法
- 各函數的運作方式與用途
- 應用範例與效能最佳化要點
接下來將結合具體的函數說明與範例程式碼進行講解,請務必閱讀到最後。
2. C 語言三角函數一覽與功能解說
在 C 語言中,要使用數學函數必須先引入標準函式庫 <math.h>
。該函式庫提供了多種處理三角函數的工具。以下將依功能分類介紹。
基本函數
sin(double x)
– 回傳以弧度為單位指定角度的正弦值。cos(double x)
– 回傳以弧度為單位指定角度的餘弦值。tan(double x)
– 回傳以弧度為單位指定角度的正切值。
反三角函數
asin(double x)
– 計算輸入值的反正弦(結果以弧度表示)。acos(double x)
– 計算輸入值的反餘弦。atan(double x)
– 計算輸入值的反正切。
特殊函數
atan2(double y, double x)
– 回傳座標 (x, y) 對應的角度(以弧度表示)。由於能分開處理分子與分母,可避免除以零的錯誤。
輔助函數
hypot(double x, double y)
– 使用畢氏定理計算點 (x, y) 到原點的距離。
注意事項:角度單位
C 語言的三角函數運算一律以弧度為單位,因此若使用角度制輸入,需先進行轉換。
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
int main() {
double degree = 45.0;
double radian = degree * (PI / 180.0); // 度轉弧度
printf("sin(45度) = %fn", sin(radian));
return 0;
}
此程式會計算 45 度的正弦值並輸出結果。請留意角度制與弧度的差異。

3. 三角函數的基本用法
本節將透過具體的程式碼範例,說明如何在 C 語言中使用三角函數。
正弦、餘弦、正切函數的使用範例
範例程式: 基本使用 sin(), cos(), tan()
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
int main() {
double angle = 45.0; // 角度制
double radian = angle * (PI / 180.0); // 轉換為弧度
printf("sin(%.2f度) = %.6fn", angle, sin(radian));
printf("cos(%.2f度) = %.6fn", angle, cos(radian));
printf("tan(%.2f度) = %.6fn", angle, tan(radian));
return 0;
}
輸出範例:
sin(45.00度) = 0.707107
cos(45.00度) = 0.707107
tan(45.00度) = 1.000000
反三角函數的使用範例
反三角函數用於計算角度。
範例程式: 基本使用 asin(), acos(), atan()
#include <stdio.h>
#include <math.h>
int main() {
double value = 0.5; // 輸入值
printf("asin(%.2f) = %.6f (弧度)n", value, asin(value));
printf("acos(%.2f) = %.6f (弧度)n", value, acos(value));
printf("atan(%.2f) = %.6f (弧度)n", value, atan(value));
return 0;
}
輸出範例:
asin(0.50) = 0.523599 (弧度)
acos(0.50) = 1.047198 (弧度)
atan(0.50) = 0.463648 (弧度)
atan2() 函數的使用範例
atan2()
函數在計算直角座標 (x, y) 對應的角度時非常方便。
範例程式: 使用 atan2() 計算角度
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
int main() {
double x = 1.0;
double y = 1.0;
double angle = atan2(y, x) * (180.0 / PI); // 弧度轉角度
printf("點 (%.1f, %.1f) 的角度 = %.2f 度n", x, y, angle);
return 0;
}
輸出範例:
點 (1.0, 1.0) 的角度 = 45.00 度
此程式透過 atan2() 計算點 (1.0, 1.0) 的角度並以角度制輸出,可避免除以零的錯誤,因此非常安全。
4. 應用範例
以下將介紹三角函數在實務中的應用案例。
圖形中的旋轉變換
三角函數常用於 2D 與 3D 圖形的座標旋轉運算。
範例程式: 2D 座標旋轉
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
void rotate_point(double x, double y, double angle) {
double radian = angle * (PI / 180.0);
double x_new = x * cos(radian) - y * sin(radian);
double y_new = x * sin(radian) + y * cos(radian);
printf("旋轉後的座標: (%.2f, %.2f)n", x_new, y_new);
}
int main() {
double x = 1.0, y = 0.0;
double angle = 45.0;
printf("原始座標: (%.2f, %.2f)n", x, y);
rotate_point(x, y, angle);
return 0;
}
輸出範例:
原始座標: (1.00, 0.00)
旋轉後的座標: (0.71, 0.71)
此程式計算點 (1.0, 0.0) 旋轉 45 度後的新座標。
物理模擬中的應用範例
範例: 擺錘運動模擬
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
int main() {
double length = 1.0; // 擺長 (m)
double gravity = 9.81; // 重力加速度 (m/s^2)
double time = 0.0; // 時間
double period = 2 * PI * sqrt(length / gravity); // 週期
printf("時間 (s) 角度 (rad)n");
for (int i = 0; i <= 10; i++) {
double angle = 0.1 * cos(2 * PI * time / period); // 小振幅近似公式
printf("%.2f %.4fn", time, angle);
time += 0.1;
}
return 0;
}
輸出範例:
時間 (s) 角度 (rad)
0.00 0.1000
0.10 0.0998
0.20 0.0993
0.30 0.0985
此程式模擬擺錘的運動,並輸出隨時間變化的角度。
5. 計算精度與效能最佳化
在 C 語言中處理三角函數時,必須兼顧計算精度與效能。本節將說明如何在兩者之間取得平衡。
計算精度注意事項
捨入誤差的影響
浮點數運算可能產生捨入誤差,尤其在處理極小或極大的值時,誤差可能累積。
範例: 捨入誤差
#include <stdio.h>
#include <math.h>
int main() {
double angle = 90.0; // 角度制
double radian = angle * (M_PI / 180.0); // 弧度轉換
double result = cos(radian);
printf("cos(90度) = %.15fn", result); // 理論值應為 0.000000000000000
return 0;
}
輸出範例:
cos(90度) = 0.000000000000001
對策:
- 使用近似判斷: 例如
fabs(result) < 1e-10
來避免誤差影響。
高速運算演算法
加速技巧
三角函數計算通常消耗較多 CPU 資源,在效能要求高的應用中可採用近似公式或專用演算法。
範例程式: 高速 sin 函數(泰勒展開)
double fast_sin(double x) {
double x2 = x * x;
return x * (1.0 - x2 / 6.0 + x2 * x2 / 120.0); // 泰勒展開近似
}
此方法雖降低部分精度,但計算速度更快。
效能比較測試
效能評估方法
可透過標準時間函數測量執行時間。
範例程式: 測量執行時間
#include <stdio.h>
#include <math.h>
#include <time.h>
double fast_sin(double x) {
double x2 = x * x;
return x * (1.0 - x2 / 6.0 + x2 * x2 / 120.0);
}
int main() {
clock_t start, end;
double result;
start = clock(); // 開始計時
for (int i = 0; i < 1000000; i++) {
result = sin(1.0);
}
end = clock(); // 結束計時
printf("標準 sin() 執行時間: %f 秒n", (double)(end - start) / CLOCKS_PER_SEC);
start = clock();
for (int i = 0; i < 1000000; i++) {
result = fast_sin(1.0);
}
end = clock();
printf("高速 sin() 執行時間: %f 秒n", (double)(end - start) / CLOCKS_PER_SEC);
return 0;
}
輸出範例:
標準 sin() 執行時間: 0.030000 秒
高速 sin() 執行時間: 0.010000 秒
此範例比較了標準與高速版本的執行時間,可依需求選擇。

6. 注意事項與最佳實務
在使用三角函數時,請注意以下幾點來撰寫程式,以確保正確性與穩定性。
1. 角度單位的管理
- 問題: 混用角度制與弧度制容易導致程式錯誤。
- 對策: 在函數名稱或變數名稱中明確標示單位。
例如:在變數名稱中加入 angle_deg
或 angle_rad
。
2. 錯誤處理
當輸入值不合法時,三角函數可能會回傳 NaN
(非數值)。務必正確處理這種情況。
範例程式: NaN 檢查
#include <stdio.h>
#include <math.h>
int main() {
double value = 2.0; // asin 的有效範圍為 -1 <= x <= 1
double result = asin(value);
if (isnan(result)) {
printf("錯誤:輸入值無效。n");
} else {
printf("結果:%.6fn", result);
}
return 0;
}
輸出範例:
錯誤:輸入值無效。
7. 總結
本文從 C 語言中三角函數的基礎、應用,到精度與效能最佳化的技巧進行了完整介紹。
學到的重點:
- 三角函數的基本用法與範例程式碼
- 應用範例:圖形旋轉與物理模擬
- 精度與效能的優化技巧
下一步建議:
- 學習其他數學函數(例如指數函數與對數函數)的應用
- 深入理解進階的數值分析演算法
C 語言的三角函數是許多應用場合中不可或缺的強大工具。請善用本文的內容,將其應用於您的專案中!