Java Basics #4
Java Math
Math.max(x,y)
The Math.max(x,y)
method can be used to find the highest value of x and y.
Example:
Math.max(5, 10);
Math.min(x,y)
The Math.min(x,y)
method can be used to find the lowest value of x and y.
Example:
Math.min(5, 10);
Math.sqrt(x)
The Math.sqrt(x)
method returns the square root of x.
Example:
Math.sqrt(64);
Math.abs(x)
The Math.abs(x)
method returns the absolute (positive) value of x.
Example:
Math.abs(-4.7);
Random Numbers
Math.random()
returns a random number between 0.0 (inclusive), and 1.0 (exclusive).
Example:
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.
Example:
int randomNum = (int)(Math.random() * 101); // 0 to 100
More Math Functions as Follows:
asin(x) Returns the arcsine of x, in radians
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
cbrt(x) Returns the cube root of x
ceil(x) Returns the value of x rounded up to its nearest integer
sin(x) Returns the sine of x (x is in radians)
toDegrees(x) Converts an angle measured in radians to an approx. equivalent angle measured in degrees.
Comments
Post a Comment