[Go to site: main page, start]

0% found this document useful (0 votes)
7 views3 pages

Java Math Class Methods Explained

The Java Math class provides various methods for mathematical operations, including finding maximum and minimum values, calculating square roots, absolute values, and powers. It also includes rounding methods such as round, ceil, and floor, as well as a method for generating random numbers. These methods facilitate a wide range of mathematical tasks in Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Java Math Class Methods Explained

The Java Math class provides various methods for mathematical operations, including finding maximum and minimum values, calculating square roots, absolute values, and powers. It also includes rounding methods such as round, ceil, and floor, as well as a method for generating random numbers. These methods facilitate a wide range of mathematical tasks in Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

You might also like