
- C程式設計教程
- C - 首頁
- C語言基礎
- C - 概述
- C - 特性
- C - 歷史
- C - 環境搭建
- C - 程式結構
- C - Hello World
- C - 編譯過程
- C - 註釋
- C - 詞法單元
- C - 關鍵字
- C - 識別符號
- C - 使用者輸入
- C - 基本語法
- C - 資料型別
- C - 變數
- C - 整數提升
- C - 型別轉換
- C - 型別強制轉換
- C - 布林值
- C語言中的常量和字面量
- C - 常量
- C - 字面量
- C - 轉義序列
- C - 格式說明符
- C語言中的運算子
- C - 運算子
- C - 算術運算子
- C - 關係運算符
- C - 邏輯運算子
- C - 位運算子
- C - 賦值運算子
- C - 一元運算子
- C - 自增和自減運算子
- C - 三元運算子
- C - sizeof 運算子
- C - 運算子優先順序
- C - 其他運算子
- C語言中的決策
- C - 決策
- C - if 語句
- C - if...else 語句
- C - 巢狀 if 語句
- C - switch 語句
- C - 巢狀 switch 語句
- C語言中的迴圈
- C - 迴圈
- C - while 迴圈
- C - for 迴圈
- C - do...while 迴圈
- C - 巢狀迴圈
- C - 無限迴圈
- C - break 語句
- C - continue 語句
- C - goto 語句
- C語言中的函式
- C - 函式
- C - 主函式
- C - 按值呼叫函式
- C - 按引用呼叫函式
- C - 巢狀函式
- C - 可變引數函式
- C - 使用者定義函式
- C - 回撥函式
- C - 返回語句
- C - 遞迴
- C語言中的作用域規則
- C - 作用域規則
- C - 靜態變數
- C - 全域性變數
- C語言中的陣列
- C - 陣列
- C - 陣列的特性
- C - 多維陣列
- C - 將陣列傳遞給函式
- C - 從函式返回陣列
- C - 變長陣列
- C語言中的指標
- C - 指標
- C - 指標和陣列
- C - 指標的應用
- C - 指標運算
- C - 指標陣列
- C - 指標到指標
- C - 將指標傳遞給函式
- C - 從函式返回指標
- C - 函式指標
- C - 指向陣列的指標
- C - 指向結構體的指標
- C - 指標鏈
- C - 指標與陣列
- C - 字元指標和函式
- C - 空指標
- C - void 指標
- C - 野指標
- C - 解引用指標
- C - 近指標、遠指標和巨指標
- C - 指標陣列的初始化
- C - 指標與多維陣列
- C語言中的字串
- C - 字串
- C - 字串陣列
- C - 特殊字元
- C語言中的結構體和聯合體
- C - 結構體
- C - 結構體和函式
- C - 結構體陣列
- C - 自引用結構體
- C - 查詢表
- C - 點(.)運算子
- C - 列舉(或 enum)
- C - 結構體填充和打包
- C - 巢狀結構體
- C - 匿名結構體和聯合體
- C - 聯合體
- C - 位域
- C - Typedef
- C語言中的檔案處理
- C - 輸入和輸出
- C - 檔案I/O(檔案處理)
- C預處理器
- C - 預處理器
- C - 編譯指示
- C - 預處理器運算子
- C - 宏
- C - 標頭檔案
- C語言中的記憶體管理
- C - 記憶體管理
- C - 記憶體地址
- C - 儲存類
- 其他主題
- C - 錯誤處理
- C - 可變引數
- C - 命令執行
- C - 數學函式
- C - static 關鍵字
- C - 隨機數生成
- C - 命令列引數
- C程式設計資源
- C - 問答
- C - 快速指南
- C - 速查表
- C - 有用資源
- C - 討論
C語言中的數學函式
C 數學函式
C語言提供各種函式來對數字執行數學運算,例如查詢三角函式值、計算對數和指數、舍入數字等。要在C程式中使用這些**數學函式**,您需要包含math.h 標頭檔案。
我們將數學函式分為以下幾類:
三角函式
math.h 庫定義了函式sin()、cos() 和 tan() - 返回相應的三角函式值,即給定角度的正弦、餘弦和正切。
這些函式返回給定雙精度型別(表示以弧度表示的角度)的相應比率。所有函式都返回雙精度值。
double sin(double x) double cos(double x) double tan(double x)
對於所有上述函式,引數“x”都是以弧度表示的角度。
示例
以下示例演示瞭如何在C中使用三角函式:
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double x, sn, cs, tn, val; x = 45.0; val = PI / 180; sn = sin(x*val); cs = cos(x*val); tn = tan(x*val); printf("sin(%f) : %f\n", x, sn); printf("cos(%f) : %f\n", x, cs); printf("tan(%f) : %f\n", x, tn); return(0); }
輸出
執行此程式碼時,將產生以下輸出:
sin(45.000000) : 0.707107 cos(45.000000) : 0.707107 tan(45.000000) : 1.000000
反三角函式
math.h 庫還包括反三角函式,也稱為反正弦函式或反三角函式。它們是基本三角函式的反函式。例如,asin(x) 等價於 $\mathrm{sin^{-1}(x)}$。其他反函式有 acos()、atan() 和 atan2()。
以下asin() 函式 返回“x”在區間 [-pi/2, +pi/2] 弧度內的反正弦值:
double asin(double x)
以下acos() 函式 返回“x”在區間 [0, pi] 弧度內的主值反餘弦值:
double acos(double x)
以下atan() 函式 返回“x”在區間 [-pi/2, +pi/2] 弧度內的主值反正切值。
double atan(double x)
示例 1
以下示例演示瞭如何在C程式中使用反三角函式:
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double x, asn, acs, atn, val; x = 0.9; val = 180/PI; asn = asin(x); acs = acos(x); atn = atan(x); printf("asin(%f) : %f in radians\n", x, asn); printf("acos(%f) : %f in radians\n", x, acs); printf("atan(%f) : %f in radians\n", x, atn); asn = (asn * 180) / PI; acs = (acs * 180) / PI; atn = (atn * 180) / PI; printf("asin(%f) : %f in degrees\n", x, asn); printf("acos(%f) : %f in degrees\n", x, acs); printf("atan(%f) : %f in degrees\n", x, atn); return(0); }
輸出
執行此程式碼時,將產生以下輸出:
asin(0.900000) : 1.119770 in radians acos(0.900000) : 0.451027 in radians atan(0.900000) : 0.732815 in radians asin(0.900000) : 64.158067 in degrees acos(0.900000) : 25.841933 in degrees atan(0.900000) : 41.987213 in degrees
The atan2() function returns the arc tangent in radians of "y/x" based on the signs of both values to determine the correct quadrant.
double atan2(double y, double x)
此函式返回“y / x”在區間 [-pi, +pi] 弧度內的主值反正切值。
示例 2
請檢視以下示例:
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double x, y, ret, val; x = -7.0; y = 7.0; val = 180.0 / PI; ret = atan2 (y,x) * val; printf("The arc tangent of x = %lf, y = %lf ", x, y); printf("is %lf degrees\n", ret); return(0); }
輸出
執行程式碼並檢查其輸出:
The arc tangent of x = -7.000000, y = 7.000000 is 135.000000 degrees
雙曲函式
在數學中,雙曲函式類似於三角函式,但使用雙曲線而不是圓來定義。math.h 標頭檔案提供sinh()、cosh() 和tanh() 函式。
double sinh(double x)
此函式返回 x 的雙曲正弦值。
double cosh(double x)
此函式返回 x 的雙曲餘弦值。
double tanh(double x)
此函式返回 x 的雙曲正切值。
示例
以下示例演示瞭如何在C程式中使用雙曲函式:
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double x,val, sh, ch, th; x = 45; val = PI/180.0 ; sh = sinh(x*val); ch = cosh(x*val); th = tanh(x*val); printf("The sinh(%f) = %lf\n", x, sh); printf("The cosh(%f) = %lf\n", x, ch); printf("The tanh(%f) = %lf\n", x, th); return(0); }
輸出
執行程式碼並檢查其輸出:
The sinh(45.000000) = 0.868671 The cosh(45.000000) = 1.324609 The tanh(45.000000) = 0.655794
指數和對數函式
"math.h" 庫包含以下與指數和對數相關的函式:
exp() 函式:它返回 e 的 x 次冪的值。(e 的值 - 尤拉數 - 約為 2.718)
double exp(double x)
log() 函式:它返回“x”的自然對數(以 e 為底的對數)。
double log(double x)
請注意,對數函式等價於指數函式的反函式。
log10() 函式:它返回“x”的常用對數(以 10 為底的對數)。
double log10(double x)
示例
以下示例演示瞭如何在C程式中使用指數和對數函式:
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main () { double x = 2; double e, ln, ls; e = exp(2); ln = log(e); printf("exp(%f): %f log(%f): %f\n",x, e, e, ln); ln = log(x); printf("log(%f): %f\n",x,ln); ls = log10(x); printf("log10(%f): %f\n",x,ls); return(0); }
輸出
執行此程式碼時,將產生以下輸出:
exp(2.000000): 7.389056 log(7.389056): 2.000000 log(2.000000): 0.693147 log10(2.000000): 0.301030
浮點數函式
The frexp() and ldexp() functions are used for floating-point manipulation.
frexp() 函式
"math.h" 標頭檔案還包含 frexp() 函式。它將浮點數分解為其有效數和指數。
double frexp(double x, int *exponent)
這裡,“x”是要計算的浮點值,而“exponent”是指向 int 物件的指標,其中要儲存指數的值。
此函式返回歸一化分數。
示例
請檢視以下示例:
#include <stdio.h> #include <math.h> int main () { double x = 1024, fraction; int e; fraction = frexp(x, &e); printf("x = %.2lf = %.2lf * 2^%d\n", x, fraction, e); return(0); }
輸出
執行程式碼並檢查其輸出:
x = 1024.00 = 0.50 * 2^11
ldexp() 函式
ldexp() 函式將有效數和指數組合起來形成一個浮點數。其語法如下:
double ldexp(double x, int exponent)
這裡,“x”是表示有效數的浮點值,“exponent”是指數的值。此函式返回 (x * 2 exp)
示例
以下示例演示瞭如何在C程式中使用此 ldexp() 函式:
#include <stdio.h> #include <math.h> int main () { double x, ret; int n; x = 0.65; n = 3; ret = ldexp(x ,n); printf("%f * 2 %d = %f\n", x, n, ret); return(0); }
輸出
執行程式碼並檢查其輸出:
0.650000 * 2^3 = 5.200000
冪和平方根函式
The pow() and sqrt() functions are used to calculate the power and square root of the given number.
pow() 函式
此函式返回 x 的 y 次冪,即 xy。
double pow(double x, double y)
sqrt() 函式
返回 x 的平方根。
double sqrt(double x)
sqrt(x) 函式返回的值與 pow(x, 0.5) 相同
示例
以下示例演示瞭如何在C程式中使用 pow() 和 sqrt() 函式:
#include <stdio.h> #include <math.h> int main() { double x = 9, y=2; printf("Square root of %lf is %lf\n", x, sqrt(x)); printf("Square root of %lf is %lf\n", x, pow(x, 0.5) ); printf("%lf raised to power %lf\n", x, pow(x, y)); return(0); }
輸出
執行此程式碼時,將產生以下輸出:
Square root of 9.000000 is 3.000000 Square root of 9.000000 is 3.000000 9.000000 raised to power 81.000000
舍入函式
math.h 庫包含ceil()、floor() 和 round() 函式,這些函式對給定的浮點數進行舍入。
ceil() 函式
它返回大於或等於 x 的最小整數。
double ceil(double x)
此函式返回不小於 x 的最小整數值。
floor() 函式
此函式返回小於或等於 x 的最大整數。
double floor(double x)
引數 x:這是浮點值。此函式返回不大於 x 的最大整數值。
round() 函式
此函式用於將作為引數傳遞給它的雙精度、浮點或長雙精度值舍入到最接近的整數值。
double round( double x )
返回值是以浮點數表示的最接近的整數。
示例
以下示例演示瞭如何在C程式中使用舍入函式:
#include <stdio.h> #include <math.h> int main() { float val1, val2, val3, val4; val1 = 1.2; val2 = 1.6; val3 = 2.8; val4 = -2.3; printf ("ceil(%lf) = %.1lf\n", val1, ceil(val1)); printf ("floor(%lf) = %.1lf\n", val2, floor(val2)); printf ("ceil(%lf) = %.1lf\n", val3, ceil(val3)); printf ("floor(%lf) = %.1lf\n", val4, floor(val4)); printf("round(%lf) = %.1lf\n", val1, round(val1)); printf("round(%lf) = %.1lf", val4, round(val4)); return(0); }
輸出
執行此程式碼時,將產生以下輸出:
ceil(1.200000) = 2.0 floor(1.600000) = 1.0 ceil(2.800000) = 3.0 floor(-2.300000) = -3.0 round(1.200000) = 1.0 round(-2.300000) = -2.0
模函式
此類別中"math.h" 庫包含以下函式
modf() 函式
The modf() function returns the fraction component (part after the decimal), and sets integer to the integer component.
double modf(double x, double *integer)
這裡,“x”是浮點值,“integer”是指向一個物件的指標,其中儲存整數部分。
此函式返回“x”的小數部分,符號相同。
示例
請檢視以下示例:
#include <stdio.h> #include <math.h> int main () { double x, fractpart, intpart; x = 8.123456; fractpart = modf(x, &intpart); printf("Integral part = %lf\n", intpart); printf("Fraction Part = %lf \n", fractpart); return(0); }
輸出
執行此程式碼時,將產生以下輸出:
Integral part = 8.000000 Fraction Part = 0.123456
fmod() 函式
The fmod() function returns the remainder of x divided by y.
double fmod(double x, double y)
這裡,“x”是分子,“y”是分母。該函式返回“x / y”的餘數。
示例
請檢視以下示例:
#include <stdio.h> #include <math.h> int main () { float a, b; int c; a = 9.2; b = 3.7; c = 2; printf("Remainder of %f / %d is %lf\n", a, c, fmod(a,c)); printf("Remainder of %f / %f is %lf\n", a, b, fmod(a,b)); return(0); }
輸出
執行此程式碼時,將產生以下輸出:
Remainder of 9.200000 / 2 is 1.200000 Remainder of 9.200000 / 3.700000 is 1.800000
請注意,模運算子 (%) 僅適用於整數運算元。