1 / 36

第十二章

第十二章. 常用的數學函數. 常用的數學函數. 數字函數 此類函數主要是一些常用的數學函數 定義在 <math.h> 標頭檔 C 提供之數學函數 pow(), sqrt(), exp() log(), log10() ceil(), floor() fabs(), ldexp() fmod(), modf(). 常用的數學函數. pow(x, y) 主要是計算 x 的 y 次方 如: pow(4, 3) 表示計算 4 的 3 次方

rod
Télécharger la présentation

第十二章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第十二章 常用的數學函數

  2. 常用的數學函數 • 數字函數 • 此類函數主要是一些常用的數學函數 • 定義在<math.h>標頭檔 • C提供之數學函數 • pow(), sqrt(), exp() • log(), log10() • ceil(), floor() • fabs(), ldexp() • fmod(), modf()

  3. 常用的數學函數 • pow(x, y) • 主要是計算x的y次方 • 如: pow(4, 3) 表示計算4的3次方 • 當x = 0且y <= 0,或者是x < 0且y不是一整數時,則表示定義域錯誤,如pow(0, -2)或pow(-2, 2.5) • 範例ex12-1a.c

  4. 常用的數學函數 //* File name: ex12-1a.c */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { double x, y; printf("Enter x^y: "); scanf("%lf^%lf", &x, &y); printf("Ans is %.2f", pow(x, y)); }

  5. 常用的數學函數 • sqrt(x) • 主要是計算x的開根號,其中x為double型態,且x>=0 • 如: sqrt(100) 表示計算100開根號 • 範例ex12-1b.c

  6. 常用的數學函數 /* File name: ex12-1b.c */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { double x; printf("Enter a number: "); scanf("%lf", &x); printf("sqrt(%.2f) is %.2f", x, sqrt(x)); }

  7. 常用的數學函數 • exp(x) • 主要是計算指數ex • 如: exp(10) 表示計算e10 • 範例ex12-1c.c

  8. 常用的數學函數 /* File name: ex12-1c.c */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { int i; double x; for(i = 0; i <= 10; i++) printf("exp(%2d) = %8.2f\n", i, exp(i)); }

  9. 常用的數學函數 • log(x) • 主要是計算loge(x),其中x > 0 • 如: log(10) 表示計算ln(10) • 範例ex12-1d.c

  10. 常用的數學函數 /* File name: ex12-1d.c */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { int i; double x; for(i = 10; i <= 100; i += 10) printf("log(%3d) = %.2f\n", i, log(i)); }

  11. 常用的數學函數 • log10(x) • 主要是計算log10(x),其中x > 0 • 如: log10(10) 表示計算log(10) • 範例ex12-1e.c

  12. 常用的數學函數 /* File name: ex12-1e.c */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { int i; double x; for(i = 10; i <= 100; i += 10) printf("log10(%3d) = %.2f\n", i, log10(i)); }

  13. 常用的數學函數 • ceil(x) • 主要是計算大於等於x的最小整數 • 如: ceil(2.5) 此函數會傳回3 ceil(-2.5) 此函數會傳回-2 • 範例ex12-1f.c

  14. 常用的數學函數 /* File name: ex12-1f.c */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { double x; printf("Enter a number: "); scanf("%lf", &x); printf("ceil(%f) = %.2f", x, ceil(x)); }

  15. 常用的數學函數 • floor(x) • 主要是計算小於等於x的最大整數 • 如: floor(2.5) 此函數會傳回2 floor(-2.5) 此函數會傳回-3 • 範例ex12-1g.c

  16. 常用的數學函數 /* File name: ex12-1g.c */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { double x; printf("Enter a number: "); scanf("%lf", &x); printf("floor(%f) = %.2f", x, floor(x)); }

  17. 常用的數學函數 • fabs(x) • 主要是計算x的絕對值 • 如: fabs(-2.5) 此函數會傳回2.5 • 範例ex12-1h.c

  18. 常用的數學函數 /* File name: ex12-1h.c */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { double x; printf("Enter a number: "); scanf("%lf", &x); printf("|%f| = %.2f", x, fabs(x)); }

  19. 常用的數學函數 • ldexp(x, y) • 主要是計算x * 2y • 如: ldexp(4, 2) 表示計算4 * 22 = 16 • 範例ex12-1i.c

  20. 常用的數學函數 /* File name: ex12-1i.c */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { double x = 10; int y = 3; printf("ldexp(%.2f, %d) = %.2f\n", x, y, ldexp(x, y)); }

  21. 常用的數學函數 • fmod(x, y) • 主要是計算 x / y 的餘數,所得結果與x同號;若y為0,則結果為0 • 如: fmod(3, 2) = 1 fmod(-3, 2) = -1 • 範例ex12-1j.c

  22. 常用的數學函數 /* File name: ex12-1j.c */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { double x, y; printf("Enter x/y: "); scanf("%lf/%lf", &x, &y); printf("fmod(%f, %f) = %f", x, y, fmod(x, y)); }

  23. 常用的數學函數 • modf(x, double *ip) • 主要是將x劃分為整數部份與小數部份,此兩部份與x同號;modf函數將整數部份存於*ip,並傳回小數部份 • 如: y = modf(123.456, &ip) 表示將123存於ip,並傳回0.456儲存於y • 範例ex12-1k.c

  24. 常用的數學函數 /* File name: ex12-1k.c */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { double x, y, ip; printf("Enter a double number: "); scanf("%lf", &x); y = modf(x, &ip); printf("y = %f\n", y); printf("ip = %.0f\n", ip); }

  25. 亂數相關函數 • 亂數相關函數 • 此類函數在於產生亂數之用 • 定義在<stdlib.h>標頭檔 • 常見的亂數函數 • rand() • srand()

  26. 亂數相關函數 • rand 函數 • 此函數相當於一亂數產生器,函數執行後會傳回一個介於0~32767間的整數 • 範例ex12-2a.c

  27. 亂數相關函數 /* File name: ex12-2a.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int i; puts("<<Random list>>"); for(i = 0; i < 10; i++) printf("%d\n", rand()); }

  28. 亂數相關函數 • srand 函數 • 此函數用以重設rand函數執行時所需之亂數種子 • 範例ex12-2c.c

  29. srand 函數 /* File name: ex12-2c.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int i, seed; printf("Please enter a new seed..."); scanf("%d", &seed); srand(seed); puts("<<Random list>>"); for(i = 0; i < 10; i++) printf("%d\n", rand()); }

  30. 資料型態轉換函數 • 資料型態轉換函數 • 此類函數是將某一型態的資料轉變為另一型態,如:字串->整數、字串->浮點數、整數->字串…等 • 定義在<stdlib.h>標頭檔 • 常見的資料型態轉換函數 • atoi() • atof() • Itoa()

  31. 資料型態轉換函數 • atoi 函數 • 此函數可將字串轉換為整數型態的資料 • 其語法如下: y = atoi(string); 此函數會將字串string轉換為整數後傳回,並儲存於整數型態變數y • 範例ex12-3a.c

  32. 資料型態轉換函數 /* File name: 12-3a.c */ #include <stdio.h> #include <stdlib.h> int main() { char string[10]; int result; printf("Enter a string: "); gets(string); result = atoi(string); printf("After atoi transfer: %d\n", result); }

  33. 資料型態轉換函數 • atof 函數 • 此函數可將字串轉換為浮點數型態的資料 • 其語法如下: y = atof(string); 此函數會將字串string轉換為double型態的浮點數後傳回,其轉換工作會一直持續到遇見一個無法處理的字元為止 • 範例ex12-3b.c

  34. 資料型態轉換函數 /* File name: ex12-3b.c */ #include <stdio.h> #include <stdlib.h> int main() { char string[10]; double result; printf("Enter a string: "); gets(string); result = atof(string); printf("After atof transfer: %lf\n", result); }

  35. 資料型態轉換函數 • itoa 函數 • 此函數可將整數轉換為字串型態的資料 • 其語法如下: y = itoa(x, &string, radix); 此函數需要三個參數,x是待轉換的整數;string會存放轉換後的結果;radix可做為數字系統的底數,其範圍介於2~36 • 範例ex12-3c.c

  36. 資料型態轉換函數 /* File name: 12-3c.c */ #include <stdio.h> #include <stdlib.h> void main(void) { char string[20]; int x, radix; printf("Enter a number: "); scanf("%d", &x); printf("Select radix: "); scanf("%d", &radix); itoa(x, string, radix); printf("After transfer: %s\n", string); }

More Related