Bài 12: Thư viện math.h và các hàm toán học căn bậc hai, hàm mũ, hàm làm tròn trong lập trình C



Giới thiệu các hàm toán học trong lập trình C

Thư viện “math.h”


No. Phương thức Mô tả


1. ceil(number)

làm tròn lên một số đã cho. Nó trả về giá trị số nguyên lớn hơn hoặc bằng với số đã cho.


2. floor(number)

làm tròn xuống một số đã cho. Nó trả về giá trị số nguyên nhỏ hơn hoặc bằng số đã cho.


3. sqrt(number) trả về căn bậc hai của số đã cho.


4. pow(base, exponent) trả về số mũ của số đã cho.


5. abs(number) trả về giá trị tuyệt đối của số đã cho số nguyên.

fabs(number) trả về giá trị tuyệt đối của số đã cho số thực.


------------------------------------------------------------


abs(x) Returns the absolute value of x

acos(x) Returns the arccosine of x, in radians

asin(x) Returns the arcsine of x, in radians

atan(x) Returns the arctangent of x, in radians

cbrt(x) Returns the cube root of x

ceil(x) Returns the value of x rounded up to its nearest integer

cos(x) Returns the cosine of x, in radians

cosh(x) Returns the hyperbolic cosine of x, in radians

exp(x) Returns the value of Ex

expm1(x) Returns ex -1

fabs(x) Returns the absolute value of a floating x

fdim(x, y) Returns the positive difference between x and y

floor(x) Returns the value of x rounded down to its nearest integer

hypot(x, y) Returns sqrt(x2 +y2) without intermediate overflow or underflow

fma(x, y, z) Returns x*y+z without losing precision

fmax(x, y) Returns the highest value of a floating x and y

fmin(x, y) Returns the lowest value of a floating x and y

fmod(x, y) Returns the floating point remainder of x/y

pow(x, y) Returns the value of x to the power of y

sin(x) Returns the sine of x (x is in radians)

sinh(x) Returns the hyperbolic sine of a double value

tan(x) Returns the tangent of an angle

tanh(x) Returns the hyperbolic tangent of a double value

Video giải thích chi tiết về các hàm toán học trong lập trình C



Code ví dụ


//12
#include "stdio.h"
#include "math.h"

int main(){
	int a = 5;
	int b = 2;
	
	float kq = (float)a/b;
	
	printf("\n floor(a/b)= %f", floor(kq));
	printf("\n ceil(a/b)=%f", ceil(kq));
	printf("\n sqrt(9)=%f", sqrt(9));
	printf("\n 5^2 = %f", pow(5,2));
	printf("\n |5|= %d", abs(5));
	printf("\n |5.2|= %f", fabs(5.2));

}

    

Bạn có thể thích những bài đăng này:

Không có nhận xét nào:

Đăng nhận xét