0% found this document useful (0 votes)
3K views6 pages

Chapter 6 - Mathematical Library Methods

The document discusses various mathematical methods in Java's Math class. It describes methods like pow(), sqrt(), max(), min(), cbrt(), abs(), round(), ceil(), floor(), and random(). These methods perform basic math functions like finding powers, square roots, maximum/minimum values, absolute values, rounding, ceiling, flooring, and generating random numbers. The document provides the syntax and examples of using each method to calculate numeric results.

Uploaded by

6a03aditisingh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3K views6 pages

Chapter 6 - Mathematical Library Methods

The document discusses various mathematical methods in Java's Math class. It describes methods like pow(), sqrt(), max(), min(), cbrt(), abs(), round(), ceil(), floor(), and random(). These methods perform basic math functions like finding powers, square roots, maximum/minimum values, absolute values, rounding, ceiling, flooring, and generating random numbers. The document provides the syntax and examples of using each method to calculate numeric results.

Uploaded by

6a03aditisingh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

The J. B.

Vachha High School For Parsi Girls

CLASS - IX
Computer Application

Chapter 6 - Mathematical Library Methods

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

int/long/float/double values and compares them to find the greater one.

• The return value depends upon the values used as the arguments of the function.

Syntax:

<Return type)<variable name>= <Function name(argument1, argument2)>

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)>

Math.round() for positive number:


• If fractional part is less than 0.5, then the function will return the integer part of the
number.
For example,
System.out.pritnln(Math.round(8.0)); output= 8
System.out.pritnln(Math.round(8.49)); output= 8

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

Math.round() for negative number:


• If fractional part is 0.5 or less than 0.5 then the function will return the integer part of
the number.

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);

Math.ceil()for positive number:

For example:
System.out.pritnln(Math. ceil(8)); output=8.0

System.out.pritnln(Math. ceil(45.6)); output=46.0


The floating number 45.6 is between integer 45 and 46. The output is 46.0
because the closest integer greater than or equal to 45.6 is 46

System.out.pritnln(Math.ceil(0.6)); output= 1.0


The floating number 0.6 is between integer 0 and 1. The output is 1.0 because
the closest integer greater than or equal to 0.6 is 1

Math.ceil() for negative number:


For example:

System.out.pritnln(Math. ceil(-45.6)); output=-45.0


The floating number -45.6 is between integer -45 and -46. The output is -45.0
because the closest integer greater than or equal to -45.6 is -45

System.out.pritnln(Math.ceil(-0.6)); output= -0.0


The floating number -0.6 is between integer 0 and -1. The output is 0.0
because the closest integer greater than or equal to -0.6 is -0.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.floor()for positive number:


System.out.pritnln(Math. floor(45.6)); output=45.0
The floating number 45.6 is between integer 45 and 46. The output is 45.0
because the closest integer less than or equal to 45.6 is 45

System.out.pritnln(Math. floor(0.6)); output= 0.0


The floating number 0.6 is between integer 0 and 1. The output is 1.0
because the closest integer less than or equal to 0.6 is 0

Math.floor() for negative number:


System.out.pritnln(Math.floor(-45.6)); output=-46.0
The floating number -45.6 is between integer -45 and -46.
The output is -46.0 because the closest integer less than or equal to 45.6 is -46

System.out.pritnln(Math.floor(-0.6)); output= -1.0


The floating number -0.6 is between integer 0 and -1.
The output is -1.0 because the closest integer less than or equal to -0.6 is -1.
-8-

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.
***********

You might also like