Lesson 6 Mathematical Library Methods
Lesson 6 Mathematical Library Methods
Library Methods
Topics to be covered
1. Recapitulation
2. Math Library
3. Functions used in Math Library
Package
• A package is a collection of classes. Every
Java package has a name and it can be
included within the program by using the
keyword import. These are used to perform
various tasks. java.lang is a method to
perform the calculations on String and
mathematical variables.
Mathematical Functions
Function Name Purpose Return data type Example
Math.sqrt(a) Returns the square double Math.sqrt(64) = 8.0
root of a positive
number.
Math.min(a,b) Returns the smaller Depends upon the Math.min(34,89) =
number between a input value 34
& b. Math.min(-34,-89)
= -89
Math.min(0.34,0.8
9) = 0.34
Math.max(a,b) Returns the greater Depends upon the Math.max(34,89) =
number between a input value 89
& b. Math.max(-34,-89)
= -34
Math.max(0.34,0.8
9) = 0.89
Math.pow(a,b) Returns the value a double Math.pow(5,3) =
b 125.0
Math.log(a) Returns the value double
of natural
logarithm.
Mathematical Functions
Function Name Purpose Return data type Example
Math.abs(a) Returns the Depends upon the Math.abs(3.77) =
absolute value of input value 3.77
any numeral. Math.abs(-3.77) =
3.77
Math.round(a) Returns the int Math.round(2.75) =
rounded value up 3
to the nearest Math.round(2.25) =
integer. 2
Math.floor(a) Returns the double Math.floor(2.75) =
rounded value 2.0
down to the Math.floor(-2.25) =
nearest integer. -2.0
Math.ceil(a) Returns the whole double Math.ceil (2.75) =
number greater 3.0
than or equal to Math.round(2.25) =
the number. 3.0
Let us think!
• Find the output.
1. System.out.println(Math.pow(5.0,-2.0));
2. System.out.println(Math.sqrt(Math.abs(-56.25)
);
3. System.out.println(Math.round(8.5));
4. System.out.println(Math.round(-8.50));?
5. System.out.println(Math.round(-8.51));
6. System.out.println(Math.ceil(-0.95));
7. System.out.println(Math.min(-25.5,-12.5));
8. System.out.println(Math.floor(-0.88));
Write the answers in the notebook.
1. Distinguish between Math.ceil() and
Math.floor()
2. Write syntax to find round off a number d,
square root of a number d and exponent of a
number e.
3. What is Math.cbrt(a) function used for? Give an
example.
4. What is the syntax to find smaller between two
numbers a and b?
YO U!
T H A N K