0% found this document useful (0 votes)
5 views

Mathematical Method_Functions in Java

The document discusses common mathematical functions in Java, including methods from the Math class such as min(), max(), sqrt(), abs(), and random(). It provides examples of how to use these functions in programming, demonstrating their applications for calculations. Key methods highlighted include those for exponentiation, rounding, and generating random numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Mathematical Method_Functions in Java

The document discusses common mathematical functions in Java, including methods from the Math class such as min(), max(), sqrt(), abs(), and random(). It provides examples of how to use these functions in programming, demonstrating their applications for calculations. Key methods highlighted include those for exponentiation, rounding, and generating random numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Mathematical Functions

in
Java
Objectives
• Discuss some of the common mathematical functions used in Java
such as minimum, maximum, square root, absolute value and random
number.
• Demonstrate how to use these functions in programming.
Introduction
• Java provides many useful methods in the Math class for performing
common mathematical functions.
• Java Math class provides several methods to work on math
calculations like min(), max(), avg(), sin(), cos(), tan(), round(), ceil(),
floor(), abs() etc.
● Class methods:
– Exponent Methods
– Rounding Methods
– min, max, abs, and random Methods
Math.sqrt(x)
• The Math.sqrt(x) method returns the square root
of x:
• Example:
Math.sqrt(64);
System.out.println(Math.sqrt(64)); //returns 8.0
Math.abs(x)
• The Math.abs(x) method returns the absolute (positive)
value of x.
• Example:
Math.abs(-4.7); //returns 4.7
Math.min(x,y)
• It is used to return the Smallest of two values.
• Examples:
• Math.min(a,b);
int minNum = (int) Math.min(a,b);
Math.max(x,y)
• It returns the Largest of two values.
• Examples:
• Math.max(5,2);
• int maxNum = (int) Math.max(5,2);
Math.round()

• It is used to round of the decimal numbers to the nearest value.


• Examples:
• Math.round(3.54);
• double numRound = Math.round(3.54); //returns 4.0
Math.random()
• 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, for


example, if 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
Exponent Methods
Examples:
● pow(double a, Math.pow(2, 3) returns 8.0
double b) Math.pow(3, 2) returns 9.0
Returns a raised to the power of b. Math.pow(3.5, 2.5) returns
22.91765
● sqrt(double a) Math.sqrt(4) returns 2.0
Returns the square root of a. Math.sqrt(10.5) returns 3.24

10
min, max, and abs
● max(a, b)and Examples:
min(a, b)
Returns the maximum or Math.max(2, 3) returns 3
minimum of two parameters. Math.max(2.5, 3) returns
3.0
● abs(a)
Math.min(2.5, 3.6)
Returns the absolute value of the
returns 2.5
parameter.
Math.abs(-2) returns 2
● random() Math.abs(-2.1) returns
Returns a random double value 2.1
in the range [0.0, 1.0).

11
The random Method
Generates a random double value greater than or equal to 0.0 and less
than 1.0 (0 <= Math.random() < 1.0).

Examples:

In general,

12
Try this code:

You might also like