Bài 26. Thư Viện Math.h Trong C - Lập Trình Không Khó
Có thể bạn quan tâm
- Sign in / Join
- Lập trình
- Học C/C++
Trong bài này, bạn sẽ cũng Lập Trình Không Khó đi tìm hiểu và sử dụng các hàm có sẵn trong thư viện math.h – thư viện toán học của ngôn ngữ C/C++. Thông qua việc sử dụng các hàm này sẽ cho các bạn 1 cái nhìn nhất định về hàm trong ngôn ngữ lập trình, làm tiền đề để chúng ta tìm hiểu kiến thức hàm trong ngôn ngữ C. Bài viết này sẽ trình bày các hàm thường được sử dụng trong thư viện math.h và các tài liệu cần thiết để các bạn có thể tra cứu khi cần.
Video hướng dẫn sử dụng thư viện math.h
Video dưới đây sẽ hướng dẫn bạn cách sử dụng các hàm phổ biến trong thư viện math.h và với mỗi hàm, sẽ có những ví dụ tương ứng để bạn hiểu và nắm được cách sử dụng.
Các hàm trong thư viện math.h
Sau đây là cú pháp và ví dụ của các hàm phổ biến trong thư viện toán học math.h, bạn nên lấy các ví dụ này để chạy thử. Hãy thử thay đổi code cũng như các tham số trong hàm để hiểu rõ hơn về hàm nhé.
Hàm lượng giác: sin, cos, tan, …
Dưới đây là cú pháp của hàm sin, các hàm khác có cú pháp và cách sử dụng tương tự.
double sin (double x); float sin (float x); long double sin (long double x);123 | doublesin(doublex);floatsin(floatx);longdoublesin(longdoublex); |
Các hàm trên trả về sin của một góc x, trong đó x được tính bằng radian. Tham số hàm sin nhận vào có thể là các kiểu dữ liệu số. Giá trị trả về là số thực kiểu float hoặc double.
Ví dụ 1 chương trình tính sin:
/* sin example */ #include <stdio.h> /* printf */ #include <math.h> /* sin */ #define PI 3.14159265 int main () { double param, result; param = 30.0; result = sin (param*PI/180); printf ("The sine of %f degrees is %f.\n", param, result ); return 0; }1234567891011121314 | /* sin example */#include <stdio.h> /* printf */#include <math.h> /* sin */ #define PI 3.14159265 intmain(){doubleparam,result;param=30.0;result=sin(param*PI/180);printf("The sine of %f degrees is %f.\n",param,result);return0;} |
Kết quả chạy chương trình:
The sine of 30.000000 degrees is 0.500000.1 | The sine of 30.000000 degrees is 0.500000. |
Các hàm lượng giác còn lại, nếu bạn muốn xem chi tiết vui lòng tham khảo mục tài liệu số (1) ở cuối bài viết này.
Hàm exp() – Tính ex
Cú pháp:
double exp (double x); float exp (float x); long double exp (long double x);123 | doubleexp(doublex);floatexp(floatx);longdoubleexp(longdoublex); |
Ví dụ:
/* exp example */ #include <stdio.h> /* printf */ #include <math.h> /* exp */ int main () { double param, result; param = 5.0; result = exp (param); printf ("e^%f = %f.\n", param, result ); return 0; }123456789101112 | /* exp example */#include <stdio.h> /* printf */#include <math.h> /* exp */ intmain(){doubleparam,result;param=5.0;result=exp(param);printf("e^%f = %f.\n",param,result);return0;} |
Kết quả:
e^5.000000 = 148.413159.1 | e^5.000000 = 148.413159. |
Hàm logarit – log()
Cú pháp:
double log (double x); float log (float x); long double log (long double x);123 | doublelog(doublex);floatlog(floatx);longdoublelog(longdoublex); |
Ví dụ:
/* log example */ #include <stdio.h> /* printf */ #include <math.h> /* log */ int main () { double param, result; param = 5.5; result = log (param); printf ("log(%f) = %f\n", param, result ); return 0; }123456789101112 | /* log example */#include <stdio.h> /* printf */#include <math.h> /* log */ intmain(){doubleparam,result;param=5.5;result=log(param);printf("log(%f) = %f\n",param,result);return0;} |
Kết quả:
log(5.500000) = 1.7047481 | log(5.500000) = 1.704748 |
Hàm pow() – Tính lũy thừa ab
Cú pháp:
double pow (double base , double exponent); float pow (float base , float exponent); long double pow (long double base, long double exponent);123 | doublepow(doublebase,doubleexponent);floatpow(floatbase,floatexponent);longdoublepow(longdoublebase,longdoubleexponent); |
Ví dụ:
/* pow example */ #include <stdio.h> /* printf */ #include <math.h> /* pow */ int main () { printf ("7 ^ 3 = %f\n", pow (7.0, 3.0) ); printf ("4.73 ^ 12 = %f\n", pow (4.73, 12.0) ); printf ("32.01 ^ 1.54 = %f\n", pow (32.01, 1.54) ); return 0; }1234567891011 | /* pow example */#include <stdio.h> /* printf */#include <math.h> /* pow */ intmain(){printf("7 ^ 3 = %f\n",pow(7.0,3.0));printf("4.73 ^ 12 = %f\n",pow(4.73,12.0));printf("32.01 ^ 1.54 = %f\n",pow(32.01,1.54));return0;} |
Kết quả:
7 ^ 3 = 343.000000 4.73 ^ 12 = 125410439.217423 32.01 ^ 1.54 = 208.036691123 | 7 ^ 3 = 343.0000004.73 ^ 12 = 125410439.21742332.01 ^ 1.54 = 208.036691 |
Hàm sqrt() – Tính căn bậc 2
Cú pháp:
double sqrt (double x); float sqrt (float x); long double sqrt (long double x);123 | doublesqrt(doublex);floatsqrt(floatx);longdoublesqrt(longdoublex); |
Ví dụ:
/* sqrt example */ #include <stdio.h> /* printf */ #include <math.h> /* sqrt */ int main () { double param, result; param = 1024.0; result = sqrt (param); printf ("sqrt(%f) = %f\n", param, result ); return 0; }123456789101112 | /* sqrt example */#include <stdio.h> /* printf */#include <math.h> /* sqrt */ intmain(){doubleparam,result;param=1024.0;result=sqrt(param);printf("sqrt(%f) = %f\n",param,result);return0;} |
Kết quả:
sqrt(1024.000000) = 32.0000001 | sqrt(1024.000000)=32.000000 |
Lưu ý: Bạn có thể truyền số nguyên vào hàm nhé.
Hàm abs() và fabs() – Tìm trị tuyệt đối
Hàm abs() thương được dùng để tìm trị tuyệt đối của số nguyên, còn hàm fabs() để tìm trị tuyệt đối của số thực.
Lưu ý: Hai hàm này có sự khác biệt một chút trong C++, tuy nhiên chúng ta đang học C nên mình không nhắc tới.
Ví dụ:
#include <stdio.h> #include <math.h> int main () { printf ("\nThe absolute value of 3.1416 is %f\n", fabs (3.1416) ); printf ("\nThe absolute value of -10.6 is %f\n", fabs (-10.6) ); printf ("\nThe absolute value of 3 is %d\n", abs (3) ); printf ("\nThe absolute value of -10 is %d\n", abs (-10) ); return 0; }123456789101112 | #include <stdio.h> #include <math.h> intmain(){printf("\nThe absolute value of 3.1416 is %f\n",fabs(3.1416));printf("\nThe absolute value of -10.6 is %f\n",fabs(-10.6));printf("\nThe absolute value of 3 is %d\n",abs(3));printf("\nThe absolute value of -10 is %d\n",abs(-10));return0;} |
Kết quả chạy:
The absolute value of 3.1416 is 3.141600 The absolute value of -10.6 is 10.600000 The absolute value of 3 is 3 The absolute value of -10 is 101234 | The absolute value of 3.1416 is 3.141600The absolute value of -10.6 is 10.600000The absolute value of 3 is 3The absolute value of -10 is 10 |
Tài liệu tham khảo
- http://www.cplusplus.com/reference/cmath/
RELATED ARTICLESMORE FROM AUTHOR
Cách tách code C++ thành file .h và .cpp
Phép toán thao tác bit trong C++ (Bitwise operation)
Cây AVL (AVL Tree) – Phần 2 (Deletion)
Subscribe Connect with Notify of new follow-up comments Label Name* Email* Website Connect with Label Name* Email* Website 2 Bình luận Inline Feedbacks View all comments Load More CommentsEDITOR PICKS
Phân loại văn bản tiếng Việt sử dụng machine learning
08/08/2020Beam search là gì? Vai trò của beam search trong NLP
02/08/2020Docker là gì? Hướng dẫn chi tiết cách sử dụng Docker
19/07/2020POPULAR POSTS
1000 bài tập lập trình C/C++ có lời giải của thầy...
25/12/2019Bài 1. Giới thiệu khóa học “Học C Bá Đạo”
21/07/2019Kiểm tra số nguyên tố sử dụng C/C++ và Java
15/07/2018POPULAR CATEGORY
- Học C/C++199
- Học Python48
- Học Java45
- Học Javascript37
- Khóa học34
- Chia sẻ27
- Học Web26
- Học C#25
- Bài tập Javascript23
Từ khóa » Thư Viên Cmath
-
Thư Viện Cmath Và Một Số Hàm Toán Học Thông Dụng Trong C++
-
C++ 42: Thư Viện Cmath - YouTube
-
Math.h Trong C | Thư Viện C Chuẩn
-
Math.h Trong C | Thư Viện C Chuẩn - Hoclaptrinh
-
Các Hàm Toán Học (math) Trong C / C++ - Freetuts
-
Cmath Với Math.h)? - Programming - Dạy Nhau Học
-
Math.h Trong C
-
Hàm Thư Viện Math H Trong C - Tài Liệu Text - 123doc
-
Thư Viện Chuẩn C++ – Wikipedia Tiếng Việt
-
Hàm GCD Trong Thư Viện Cmath C ++ Sans - HelpEx
-
Các Hàm Math Trong C++ - Học Lập Trình C++ Online - VietTuts
-
Các Mô-đun Toán Học Trong Python: Math Và Cmath - Code Tutsplus
-
Thư Viện Toán Học Từ Lớp Math Của Java