Chapter 6 - Mathematical Library Methods
Chapter 6 - Mathematical Library Methods
CLASS - IX
Computer Application
Introduction:-
A method is a named block of code within a class. It executes a defined set of instructions.
Some methods are defined by the user itself. These types of methods are user-defined
methods.
Methods which are defined by the Java System developers are known as Library Methods or
built-in methods.
Methods of Math class:-
All mathematical functions are included in a class called “Math” that is part of the java.lang
package. The java.lang package is imported by default by the Java compiler.
Syntax: Math.MethodName(arguments);
Some of the basic methods of the Math class are
Math.pow()
• This function is used to find the power raised to a given base value. It accepts two
double values as arguments. The first argument is the base and the second argument
work like the index to indicate the power value.
• It will return a double type value.
Syntax:
<Return type)<variable name>= <Function name(argument1, argument2)>
For eg.,
double a =Math.pow(4,2)
42 = 16.0
Returns a double type value for ‘a’ as 16.0
Math.sqrt()
• This function is used to find the square root of a positive number. It will return a
double type value.
Syntax:
<Return type)<variable name>= <Function name(Positive number)>
Ctd…..
-2-
For eg.,
double n=Math.sqrt(9.0);
Returns a double type value for n as 3.0
double n=Math.sqrt(1.44);
Returns a double type value for n as 1.2
Square root of a negative number is imaginary number. Hence if
you use a function as
double n=Math.sqrt(-6.29); We get a run time error
Math. max()
• This function returns the greatest value of two given arguments. It accepts two
• The return value depends upon the values used as the arguments of the function.
Syntax:
For example,
int n=Math.max(4, 8);
Returns the maximum value for n as 8 in integer data type.
double n= Math.max(12.7,8.4);
Returns the maximum value for n as 12.7 in double data type
double x=Math.max(-67.66, -97 .45);
Returns the maximum value for n as -67.66 in double data type.
Math.min()
• This function returns the minimum of two numbers. It accepts two
int/long/float/double values and compares them to find the smaller one.
• The return value depends upon the values used as the arguments of the function.
Syntax:
<Return type)<variable name>= <Function name(argument1, argument2)>
Ctd…..
-3-
For eg.,
int n=Math.min(5, 8);
Returns an integer type value for n as 5.
double n=Math .min(5.6, 2.4);
Returns the minimum value for n as 2.4 in a double data type.
double x=Math.min(-4.5, -8.5);
Returns the minimum value for x as -8.5 in a double data type.
Math.cbrt()
• This function is used to find the cube root of a positive number or a negative
number.
• It will return a double type value.
Syntax:
<Return type)<variable name>= <Function name(number)>
For example,
double n=Math.cbrt(64.0);
Returns a double type value for n as 4.0
double n=Math.cbrt(-8.0);
Returns a double type value for n as -2.0
Math.abs()
• This function is used to return the absolute value of a given argument.
• It will return an int/long/double type value depending on the argument supplied.
• If the argument is positive, it is returned as it is. If it is negative, then the
positive number is returned.
Syntax:
<Return type)<variable name>= <Function name(argument)>
For example,
int x=Math.abs(5);
Returns an integer value for x as 5
int x=Math.abs(-10);
Returns an integer value for x as 10.
double x=Math.abs(8.5);
Returns a double value for x as 8.5.
double x=Math.abs(-2.32);
Returns a double value for x as 2.32.
-4-
Math.round()
• This function is used to return the value of a number rounded to its nearest integer
or long.
Syntax:
<Return type)<variable name>= <Function name(number)>
If the fractional part is 0.5 or more, then the function Math.round() will return the next
higher integer.
For example,
• System.out.pritnln(Math.round(8.5)); output= 9
• System.out.pritnln(Math.round(8.99)); output= 9
For example,
System.out.pritnln(Math.round(-7.5)); output= -7
System.out.pritnln(Math.round(-6.43)); output= -6
• If the fractional part is more than 0.5, then the function Math.round will return the
next lower integer.
For example,
System.out.pritnln(Math.round(-7.51)); output= -8
System.out.pritnln(Math.round(-6.9)); output= -7 Ctd…
-5-
Math.ceil():-
• This function is used to return the closest integer that is greater than or equal to the
number.
• It always returns the value in double data type whether the argument given is int,
float or double.
• Syntax :
• <Return data type><variable> =Function name(number);
For example:
System.out.pritnln(Math. ceil(8)); output=8.0
Ctd..…
-7-
Math.floor()
• This function is used to return the closest integer that is less than or equal to the
number.
• It always returns the value in double data type whether the argument given is int,
float or double.
• Syntax :
• <Return data type><variable> =Function name(number);
Math.random() :
• This function is used to return a random number between 0 and 1.
• It does not accept any argument.
• Whenever this function is called, it returns a real value between 0 and 1, such as
0.8, 0.3 .023 etc
Syntax :
<Return data type><variable> =Function name( );
double d= Math.random( );
Returns any double value for d between 0 and 1.
***********