Các phép tính toán học trong C++ được thực hiện bằng các hàm toán học nằm trong thư viện math hoặc cmath. Các hàm này được định nghĩa để thực hiện các phép tính phức tạp. Hãy tìm hiểu từng hàm:
sin (Sine)
Hàm sin tính sin của góc (độ) truyền vào. Chấp nhận một double, trả về double là giá trị sin(x°).
double sin(double)
Cú pháp gọi
double x = sin(23.4);
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double x = 45.3;
cout << "sin ( " << x << " ) = " << sin(x) << endl;
} Kết quả
sin( 45.3 ) = 0.968142
cos (Cosine)
Hàm cos tính cos của góc (độ) truyền vào. Chấp nhận một double, trả về double là giá trị cos(x°).
double cos(double)
Cú pháp gọi
double x = cos(23.4);
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double x = 45.3;
cout << "cos ( " << x << " ) = " << cos(x) << endl;
} Kết quả
cos( 45.3 ) = 0.2504
tan (Tangent)
Hàm tan tính tan của góc (độ) truyền vào. tan(x°) = sin(x°)/cos(x°).
double tan(double)
Cú pháp gọi
double x = tan(23.4);
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double x = 45.3;
cout << "tan ( " << x << " ) = " << tan(x) << endl;
} Kết quả
tan( 45.3 ) = 3.86638
asin (Arc Sine)
Hàm asin tìm arc sine của đối số. Trả về giá trị asin cho đầu vào double trong khoảng [-1, 1], ngoài ra báo lỗi.
double asin(double)
Cú pháp gọi
double x = asin(0.3232);
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double x = 0.3232;
cout << "asin ( " << x << " ) = " << asin(x) << endl;
} Kết quả
asin( 0.3232 ) = 0.3291
acos (Arc Cosine)
Hàm acos tìm arc cosine của đối số. Trả về giá trị acos cho đầu vào double trong khoảng [-1, 1].
double acos(double)
Cú pháp gọi
double x = acos(0.3232);
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double x = 0.3232;
cout << "acos ( " << x << " ) = " << acos(x) << endl;
} Kết quả
acos( 0.3232 ) = 1.24169
atan (Arc Tangent)
Hàm atan tính arc tangent của đối số. Trả về double cho double đầu vào.
double atan(double)
Cú pháp gọi
double x = atan(0.3232);
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double x = 0.3232;
cout << "atan ( " << x << " ) = " << atan(x) << endl;
} Kết quả
atan( 0.3232 ) = 0.312603
cosh (Hyperbolic Cosine)
Hàm cosh tính hyperbolic cosine. Trả về double cho double đầu vào.
double cosh(double)
Cú pháp gọi
double x = cosh(0.342);
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double x = 0.342;
cout << "cosh ( " << x << " ) = " << cosh(x) << endl;
} Kết quả
cosh( 0.342 ) = 1.05905
sinh (Hyperbolic Sine)
Hàm sinh tính hyperbolic sine.
double sinh(double)
Cú pháp gọi
double x = sinh(0.342);
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double x = 0.342;
cout << "sinh ( " << x << " ) = " << sinh(x) << endl;
} Kết quả
sinh( 0.342 ) = 0.348706
tanh (Hyperbolic Tangent)
Hàm tanh tính hyperbolic tangent.
double tanh(double)
Cú pháp gọi
double x = tanh(0.342);
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double x = 0.342;
cout << "tanh ( " << x << " ) = " << tanh(x) << endl;
} Kết quả
tanh( 0.342 ) = 0.329262
pow (Lũy thừa)
Hàm pow tính cơ số mũ số mũ. Chấp nhận hai double (cơ số, số mũ), trả về double.
double pow(double, double)
Cú pháp gọi
double x = pow(2, 4)
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double base = 2, power = 4;
cout << "pow( " << base << " , " << power << " ) = " << pow(base, power) << endl;
} Kết quả
pow( 2 , 4 ) = 16
sqrt (Căn bậc hai)
Hàm sqrt trả về căn bậc hai của double đầu vào.
double sqrt(double)
Cú pháp gọi
double x = sqrt(25.00)
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double a = 25;
cout << "sqrt( " << a << " ) = " << sqrt(a) << endl;
} Kết quả
sqrt( 25 ) = 5.00
log (Logarit tự nhiên)
Hàm log tìm logarit tự nhiên của số cho trước.
double log(double)
Cú pháp gọi
double x = log(1.35)
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double a = 1.35;
cout << "log( " << a << " ) = " << log(a) << endl;
} Kết quả
log( 1.35 ) = 0.300105
floor (Làm tròn xuống)
Hàm floor trả về giá trị làm tròn xuống (floor value) của double.
double floor(double)
Cú pháp gọi
double x = floor(5.24)
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double a = 6.24;
cout << "floor( " << a << " ) = " << floor(a) << endl;
} Kết quả
floor( 6.24 ) = 6
ceil (Làm tròn lên)
Hàm ceil trả về giá trị làm tròn lên (ceil value) của double.
double ceil(double)
Cú pháp gọi
double x = ceil(5.24)
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double a = 6.64;
cout << "ceil( " << a << " ) = " << ceil(a) << endl;
} Kết quả
ceil( 6.64 ) = 7
abs (Giá trị tuyệt đối)
Hàm abs trả về giá trị tuyệt đối của số nguyên.
int abs(int)
Cú pháp gọi
int x = abs(-512)
Ví dụ
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int a = -345;
cout << "abs( " << a << " ) = " << abs(a) << endl;
} Kết quả
abs( -345 ) = 345