Java Math
The Java Math class has many methods that allows you to
perform mathematical tasks on numbers.
[Link] [Link](x,y) method can be used to find the highest
value of x and y:
Example
[Link](5, 10);
[Link] [Link](x,y) method can be used to find the lowest
value of x and y:
Example
[Link](5, 10);
3. The [Link](x) method returns the square root of x:
Example
Math. sqrt(64);
[Link] [Link](x) method returns the absolute (positive) value
of x:
Example
[Link](-4.7);
5. The [Link](x, y) method returns the value of x raised to
the power of y:
Example
Math. pow(2, 8); // 256.0
Note: [Link](2, 8) means 2 multiplied by itself 8 times:
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 = 256
Note: The [Link]() method always returns a double, even if
the result is a whole number. For example, [Link](2,
8) returns 256.0 (not 256).
Rounding Methods
Java has several methods for rounding numbers:
[Link](x) - rounds to the nearest integer
[Link](x) - rounds up (returns the smallest integer
greater than or equal to x)
[Link](x) - rounds down (returns the largest integer
less than or equal to x)
Example
[Link](4.6); // 5
Math. ceil(4.1); // 5.0
[Link](4.9); // 4.0
Random Numbers
[Link]() returns a random number between 0.0
(inclusive), and 1.0 (exclusive):
Example
[Link]();
To get more control over the random number, for example, if
you only want a random number between 0 and 100, you can
use the following formula:
Example
int randomNum = (int)([Link]() * 101); // 0 to 100