Computer Notes(ICSE)
Definitions of Math Class functions
Java provides a library class called Math with predefined functions to perform some useful mathematical operations
easily. (Math class is available in java.lang package.)
SOME IMPORTANT JAVA MATHEMATICAL FUNCTIONS :
1) Math.sqrt( )- This function is used to find the square root of a positive number. It returns double value.
e.g. double r=Math.sqrt(25) Ans : 5.0
double r=Math.sqrt(‘d’) Ans : 10.0
double r=Math.sqrt(-25) Ans : NaN (NaN -Not a Number)
2) Math.cbrt( )- This function is used to find the cube root of a number. It returns double value.
The cube root of a negative value is the negative of the cube root of that value's magnitude.
double r=Math.cbrt(125) Ans : 5.0
double r=Math.cbrt(-125) Ans : -5.0
double r=Math.cbrt(10)=2.154434690031884
3) Math.pow( ) - This function is used to find the power raised to a specified base(number). It returns double
value.
e.g., double r=Math.pow(5,3) Ans: 125.0
double r=Math.pow(5,0) Ans: 1.0
double r=Math.pow('d',0) Ans: 1.0
double r=Math.pow(0,0) Ans: 1.0
4) Math.min ( ) – This function returns minimum of two numbers/arguments.
e.g. int r=Math.min(5,3) Ans : 3
int r=Math.min(‘A’, ‘B’) Ans : 65
int r=Math.min(100,’D’) Ans : 68
5) Math.max( ) – This function returns maximum of two numbers/arguments.
e.g. int r=Math.max(5,3) Ans : 5
int r=Math.max(‘A’, ‘B’) Ans : 66
double r=Math.max(100,’D’) Ans: 100.0
*** Find the smallest & greatest number among 3 input numbers
int a=90,b=45,c=65;
int st=Math.min(a,Math.min(b,c));
int gt=Math.max(a,Math.max(b,c));
System.out.print(st+”\t”+gt);
1
6) Math.abs( )- This function is used to return absolute value.(i.e. only magnitude of the number)
e.g. int r=Math.abs(-35) Ans: 35
double r=Math.abs(25.67) Ans : 25.67
e.g. | (A-B) | =Math.abs(a-b)
7) Math.floor( ) – This function is used to return a number down to the nearest integer.It returns a double value.
Math.floor(35.5) Ans: 35.0
Math.floor(35.1) Ans: 35.0
Math.floor(-35.5) Ans: -36.0
Math.floor(30) Ans: 30.0
8) Math.ceil( ) – This function is used to return the whole number greater than or equal to the given number.It
returns a double value.
Math.ceil(35.5) Ans: 36.0
Math.ceil(-35.1) Ans: -35.0
Math.ceil(30); Ans: 30.0
9) Math.round( )- This function returns the value in rounded form. It always returns an integer data type value.
Math.round(35.62) Ans :36
Math.round(35.25) Ans : 35
Math.round(-35.5) Ans : -35
Math.round(-35.14) Ans : -35
Math.round(-35.6) Ans : -36
Math.round(30.5) Ans : 31
Math.round(31.5) Ans :32
10) Math.random( )- This function is used to generate a random number between 0 and 1 or in the specified
range. It returns a double value.
Math.random() – It will generate a double value between 0 and 1.
Math.random( ) *n – It will generate a double value between 0 and n ( where n is a natural number)
e.g. Math.random()*5 - (now we will get a number between 0 and 5)
Math.random()*(UB-LB)+LB -It will generate a double value between LB and UB.
e.g. Math.random()*(10-5)+5 (now we will get a number between 5 and 10)
11) Math.PI – This constant of Math class is used to get π value(i.e. 3.141)