Mathematical Library
----------------------
The math library is present under the package java.lang. Not under the package java.math .
1) Math.sqrt() -> return type is double.
It returns the square root of a number.
(a) double x = Math.sqrt(4); => 2.0
(b) double y = Math.sqrt(-4); => NaN (Not a number)
(for any negative value the answer is always NaN)
2) Math.cbrt() -> return type is double.
It returns the cube root of a number
(a) double x = Math.cbrt(8); => 2.0
(b) double y = Math.cbrt(-125); => -5.0
3) Math.pow() -> return type is double
Syntax: Math.pow(base,exponent)
5 to the power of 3 (here 5 is base and 3 is exponent)
(a) double x = Math.pow(5,3); => 125.0
(b) double y = Math.pow(16,1/2); => Math.pow(16,0); => 1.0
here 1/2 is 0 (L<R,/,0 when both are integer)
(c) double z = Math.pow(16,1.0/2); => Math.pow(16,0.5) => 4.0
(d) double a = Math.pow(-16,1.0/2); => NaN
(e) double b = Math.pow(64,1.0/3); => 4.0
4) Math.ceil() -> return type is double
***
If a positive number in its decimal place, the value is in between 0.1 and 0.9, then the number is increased by 1 and the decimal part is removed.
Example:
double x = Math.ceil(9.4); => 10.0
double y = Math.ceil(0.2); => 1.0
double z = Math.ceil(8.0); => 8.0
(if in the decimal place there is 0, then no change)
****
If a negative number in it's decimal place, the value is in between 0.1 and 0.9, then the number stays same and the decimal part becomes 0.
Example:
double x= Math.ceil(-2.5); => -2.0
double y = Math.ceil(-9.0); => -9.0
(if in the decimal place there is 0, then no change)
5) Math.floor() -> return type is double
****
If a positive number in its decimal place, the value is in between 0.1 and 0.9, then no change only the decimal part becomes 0
Example:
double x = Math.floor(9.8); => 9.0
double x = Math.floor(0.8); => 0.0;
double y = Math.floor(1.2); => 1.0
double a = Math.floor(1.0); => 1.0
(no change if the decimal number is zero)
****
If a negative number in its decimal place, the value is in between 0.1 and 0.9, the number is increased by 1,keeping the negative sign as it is and the decimal part is made 0.
Example:
double y = Math.floor(-1.8); -2.0
double x = Math.floor(-0.1); -1.0
double z = Math.floor(-1.0); -1.0
(no change if the decimal number is zero)
6) Math.random() -> return type is double
It returns any value between 0.0(inclusive) and 1.0(exclusive) 0.0<=x<1.0 .
To display random numbers between 1 and 6
int g = (int)(Math.random()*(6-1))+1;
int r = (int)(Math.random()*(max-min))+min;
For 1 to 100
int g = (int)(Math.random()*100)+1;
7) Math.round() -> return type is int/long
If the number is positive and in its decimal place the number is in between 0.1 and 0.4, then the number stays same and the decimal part is removed.
int k = Math.round(2.4) -> 2
int m = Math.round(0.4) -> 0
int n = Math.round(2.499) -> 2
if the number is positive and in its decimal place, the number is in between 0.5 and 0.9,
then the number is increased by 1 and the decimal part is removed.
int k = Math.round(2.5) -> 3
int k = Math.round(2.5999) -> 3
If the number is negative and in its decimal place