Bài 26. Thư Viện Math.h Trong C - Lập Trình Không Khó

Sign in Sign in Welcome!Log into your account your username your password Forgot your password? Password recovery Recover your password your email Search
  • Sign in / Join
Sign in Welcome! Log into your account your username your password Forgot your password? Get help Password recovery Recover your password your email A password will be e-mailed to you. Home Lập trình Học C/C++ Bài 26. Thư viện math.h trong C
  • Lập trình
  • Học C/C++
FacebookTwitterPinterestWhatsApp
Các Hàm Trong Thư Viện Math.h
Các Hàm Trong Thư Viện Math.h
This entry is part [part not set] of 69 in the series Học C Không Khó

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.704748
1 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.036691
123 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.000000
1 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 10
1234 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

  1. http://www.cplusplus.com/reference/cmath/

RELATED ARTICLESMORE FROM AUTHOR

Cách tách code C++ thành file .h và .cpp

Cách tách code C++ thành file .h và .cpp

Phép toán thao tác bit (Bitwise Operation) trong C/C++

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 guest Label Name* Email* Website Connect with guest Label Name* Email* Website 2 Bình luận Inline Feedbacks View all comments Load More Comments

EDITOR PICKS

Phân loại văn bản tiếng Việt sử dụng machine learning

Phân loại văn bản tiếng Việt sử dụng machine learning

08/08/2020 Thuật toán Beam search

Beam search là gì? Vai trò của beam search trong NLP

02/08/2020 Tìm hiểu Docker là gì, cách sử dụng Docker

Docker là gì? Hướng dẫn chi tiết cách sử dụng Docker

19/07/2020

POPULAR POSTS

bai-tap-cpp-co-loi-giai

1000 bài tập lập trình C/C++ có lời giải của thầy...

25/12/2019 Khóa học lập trình C - Học C Bá Đạo

Bài 1. Giới thiệu khóa học “Học C Bá Đạo”

21/07/2019 Thuật toán kiểm tra số nguyên tố - Nguyễn Văn Hiếu Blog

Kiểm tra số nguyên tố sử dụng C/C++ và Java

15/07/2018

POPULAR 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
ABOUT USFOLLOW US © 20Would love your thoughts, please comment.x()x| ReplyInsert

Từ khóa » Thư Viện Cmath Và Math.h