JAVA - Math Functions
The [Link] contains a set of basic math functions. Return type of the function is
given in red.
[Link]() depends on the type of parameter passed
It returns the absolute value of the parameter passed to it.
Examples:
int a = [Link](10); // a = 10 ....since the parameter is already positive no changes occur
double b= [Link](-20.3); // b = 20.3
The [Link]() method is overloaded in 4 versions:
[Link](int)
[Link](long)
[Link](float)
[Link](double)
The return type of these depend on the type of the parameter passed it.
[Link]() double
This function rounds a floating point value up to the nearest integer value. The rounded
value is returned as a double type.
Example:
double c= [Link](7.343); // c = 8.0
double d=[Link](-7.343) // d=-7.0
[Link]() double
This function rounds a floating point value down to the nearest integer value. The rounded
value is returned as a double type .
Example:
double f = [Link](7.343); // f = 7.0
double f=[Link](-7.343); // f = -8.0
[Link]() double
This method calculates the square root of the parameter given to it.
SUJATHA SRIDHAR
Example:
double sq = [Link](4); // sq=2.0
double sq=[Link](-4): //NaN, since sqrt cannot operate on negative value.
[Link]() double
This method calculates the cuberoot of the parameter given to it.
Example:
double cq = [Link](64); // cq=4.0
double cq=[Link](-64): // cq=-4.0 (-4.0*-4.0*-4.0=-64.0)
[Link]() depends on the type of parameter passed
This method returns the smallest of two values passed to it as parameter.
Example:
int m = [Link](10, 20); // m=10
double m=[Link](0.0,-0.0) // m=-0.0
int m=[Link](-3,3) //m = -3
[Link]() depends on the type of parameter passed
This method returns the largest of two values passed to it as parameter.
Example:
int m = [Link](10, 20); // m= 20
double m=[Link](0.0,-0.0) // m= 0.0
int m=[Link](-3,3) // m = 3
[Link]() int
This method rounds a float or double to the nearest integer using normal math round rules
(either up or down). The return type is an integer.
Example:
int roundDown = [Link](23.445); // roundDown = 23
int roundUp = [Link](23.545); // roundUp = 24
SUJATHA SRIDHAR
[Link]() double
This method returns a random floating point number between 0 and 1.
Example:
double r = [Link]() // returns a random number between 0 and 1 example
0.89878....
If you need an integer value, use the round() along with [Link]()
(int)([Link]() * ((max - min) + 1)) + min
To get a random numbers between 1 and 10
int x=(int)([Link]() * ((10 - 1) + 1)) + 1;
[Link](x);
To get a random number between 1 and 100
int x=(int)([Link]() * ((100 - 1) + 1)) + 1;
[Link](x);
To get a random number between 20 and 30
int x=(int)([Link]() * ((30 - 20) + 1)) + 20;
[Link](x);
[Link]() double
This function takes two parameters. The method returns the value of the first parameter
raised to the power of the second parameter.
Example:
double p1 = [Link](2,2); p1=4.0
double p2 = [Link](2,0); // p2=1.0
double p3= [Link](9,0.5) // p3=3.0 // any number raised to power of 0.5 will return its
squareroot.
SUJATHA SRIDHAR
MATH METHOD -EXERCISES
SET 1
SET 2 Predict the output:
1. [Link]([Link](9.0))
2. [Link]([Link](9,3),8)
3. [Link]([Link](-64))
4. [Link]([Link](-67.8))
5. [Link]([Link](8.7))
6. [Link]([Link](0.0,-0.0),-0.0)
7. [Link]([Link](9.9)+6)
8. [Link]([Link](-64))
9. [Link]([Link](81))
[Link]([Link](12,[Link](-12)),2)
[Link]([Link](64,0.5),2)
[Link]([Link](9,0.5),[Link](9,0))
13.(int)([Link]()*10)
14. [Link]([Link](20.3),[Link](20.6))
15. [Link](-9.4)+[Link](10.5)
SUJATHA SRIDHAR
SET 3
SUJATHA SRIDHAR