JAVA Math

JAVA INSTALL

W3School

Lớp Toán trong Java có nhiều phương thức cho phép bạn thực hiện các nhiệm vụ toán học với các con số.

Math.max(x,y)

Math.max(x,y) có thể tìm được số lớn nhất giữa x và y. Với ví dụ bên dưới kết quả trả về sẽ là 10.

Math.max(5, 10); // Output: 10

Math.min(x,y)

The Math.min(x,y) có thể tìm được số nhỏ nhất giữa x và y. Với ví dụ bên dưới kết quả trả về sẽ là 5.

Math.min(5, 10);

Math.sqrt(x)

The Math.sqrt(x) method returns the square root of x:

Math.sqrt(64);

Math.abs(x)

The Math.abs(x) method returns the absolute (positive) value of x:

Math.abs(-4.7);

Random Numbers

Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive):

Math.random();

To get more control over the random number, e.g. you only want a random number between 0 and 100, you can use the following formula:

int randomNum = (int)(Math.random() * 101);  // 0 to 100

A list of all Math methods can be found in the table below:

MethodDescriptionReturn Type
abs(x)Returns the absolute value of xdouble|float|int|long
acos(x)Returns the arccosine of x, in radiansdouble
asin(x)Returns the arcsine of x, in radiansdouble
atan(x)Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radiansdouble
atan2(y,x)Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).double
cbrt(x)Returns the cube root of xdouble
ceil(x)Returns the value of x rounded up to its nearest integerdouble
copySign(x, y)Returns the first floating point x with the sign of the second floating point ydouble
cos(x)Returns the cosine of x (x is in radians)double
cosh(x)Returns the hyperbolic cosine of a double valuedouble
exp(x)Returns the value of Exdouble
expm1(x)Returns ex -1double
floor(x)Returns the value of x rounded down to its nearest integerdouble
getExponent(x)Returns the unbiased exponent used in xint
hypot(x, y)Returns sqrt(x2 +y2) without intermediate overflow or underflowdouble
IEEEremainder(x, y)Computes the remainder operation on x and y as prescribed by the IEEE 754 standarddouble
log(x)Returns the natural logarithm (base E) of xdouble
log10(x)Returns the base 10 logarithm of xdouble
log1p(x)Returns the natural logarithm (base E) of the sum of x and 1double
max(x, y)Returns the number with the highest valuedouble|float|int|long
min(x, y)Returns the number with the lowest valuedouble|float|int|long
nextAfter(x, y)Returns the floating point number adjacent to x in the direction of ydouble|float
nextUp(x)Returns the floating point value adjacent to x in the direction of positive infinitydouble|float
pow(x, y)Returns the value of x to the power of ydouble
random()Returns a random number between 0 and 1double
round(x)Returns the value of x rounded to its nearest integerint
rint(x)Returns the double value that is closest to x and equal to a mathematical integerdouble
signum(x)Returns the sign of xdouble
sin(x)Returns the sine of x (x is in radians)double
sinh(x)Returns the hyperbolic sine of a double valuedouble
sqrt(x)Returns the square root of xdouble
tan(x)Returns the tangent of an angledouble
tanh(x)Returns the hyperbolic tangent of a double valuedouble
toDegrees(x)Converts an angle measured in radians to an approx. equivalent angle measured in degreesdouble
toRadians(x)Converts an angle measured in degrees to an approx. angle measured in radiansdouble
ulp(x)Returns the size of the unit of least precision (ulp) of xdouble|float

Note: All Math methods are static.

W3School

Leave a Reply

Your email address will not be published. Required fields are marked *