iT Portal

# Important Methods of Math Class of Java

Methods of the java.lang.Math Class

The methods of the Math class are static and are accessed like any static

abs()

The abs() method returns the absolute value of the argument.

Eg:
x = Math.abs(99); // output is 99
x = Math.abs(-99) // output is 99

ceil()

The ceil() method rounded up to the largest integer equivalent.

Eg:
Math.ceil(9.0) // result is 9.0
Math.ceil(8.8) // rises to 9.0
Math.ceil(8.02) // still rises to 9.0

Math.ceil(-9.0) // result is –9.0
Math.ceil(-9.4) // rises to –9.0
Math.ceil(-9.8) // still rises to –9.0

floor()

This method is the antithesis of the ceil() method.
Eg:
Math.floor(9.0) // result is 9.0
Math.floor(9.4) // drops to 9.0
Math.floor(9.8) // still drops to 9.0

Math.floor(-9.0) // result is –9.0
Math.floor(-8.8) // drops to –9.0
Math.floor(-8.1) // still drops to –9.0

max()

The max() method takes two numeric arguments and returns the greater of the
Two.

Eg:
x = Math.max(1024, -5000); // output is 1024.

min()

The min() method is the antithesis of the max() method.

random()

The random() method returns a random double that is greater than or equal to
0.0 and less than 1.0. The random() method does not take any parameters.

Eg:
double randomNumber = Math.random(); //generate a random number:
System.out.println(randomNumber); // output: 0.46041540179337437

round()

The round() method returns the integer closest to the argument.

Eg:
Math.round(-10.5); // result is –10

sin(), cos() and tan()

These methods return trigonometric angles.(Math.sin(), Math.cos() and Math.tan())

acos(), asin and atan()

These methods return the trigonometric arc of angles.( Math.asin(), Math.acos() and Math.atan())

sqrt()

This method returns the square root of a double.

Eg:
Math.sqrt(9.0) // returns 3.0

toDegrees()

The toDegrees() method takes an argument representing an angle in radians
and returns the equivalent angle in degrees.

Eg:

Math.toDegrees(Math.PI * 2.0) // returns 360.0

This method takes an argument representing an angle in degrees
and returns the equivalent angle in radians.

Eg:
Math.toRadians(360.0); // returns 6.283185, which is 2 * Math.PI

exp()

Returns Euler's number e raised to the power of a double value.

Eg:
Math.exp(1.0);

Pow()

Returns the value of the first argument raised to the power of the second argument.

Eg:
Math.pow(2.0,2.0);

Example Covering imp math methods:

public class MathLibraryExample {

public static void main(String[] args) {

int i = 7;
int j = -9;
double x = 72.3;
double y = 0.34;

System.out.println(“i is ” + i);
System.out.println(“j is ” + j);
System.out.println(“x is ” + x);
System.out.println(“y is ” + y);

// The absolute value of a number is equal to
// the number if the number is positive or
// zero and equal to the negative of the number
// if the number is negative.

System.out.println(“|” + i + “| is ” + Math.abs(i));
System.out.println(“|” + j + “| is ” + Math.abs(j));
System.out.println(“|” + x + “| is ” + Math.abs(x));
System.out.println(“|” + y + “| is ” + Math.abs(y));

// Truncating and Rounding functions

// You can round off a floating point number
// to the nearest integer with round()
System.out.println(x + ” is approximately ” + Math.round(x));
System.out.println(y + ” is approximately ” + Math.round(y));

// The “ceiling” of a number is the
// smallest integer greater than or equal to
// the number. Every integer is its own
// ceiling.
System.out.println(“The ceiling of ” + i + ” is ” + Math.ceil(i));
System.out.println(“The ceiling of ” + j + ” is ” + Math.ceil(j));
System.out.println(“The ceiling of ” + x + ” is ” + Math.ceil(x));
System.out.println(“The ceiling of ” + y + ” is ” + Math.ceil(y));

// The “floor” of a number is the largest
// integer less than or equal to the number.
// Every integer is its own floor.
System.out.println(“The floor of ” + i + ” is ” + Math.floor(i));
System.out.println(“The floor of ” + j + ” is ” + Math.floor(j));
System.out.println(“The floor of ” + x + ” is ” + Math.floor(x));
System.out.println(“The floor of ” + y + ” is ” + Math.floor(y));

// Comparison operators

// min() returns the smaller of the two arguments you pass it
System.out.println(“min(” + i + “,” + j + “) is ” + Math.min(i,j));
System.out.println(“min(” + x + “,” + y + “) is ” + Math.min(x,y));
System.out.println(“min(” + i + “,” + x + “) is ” + Math.min(i,x));
System.out.println(“min(” + y + “,” + j + “) is ” + Math.min(y,j));

// There’s a corresponding max() method
// that returns the larger of two numbers

System.out.println(“max(” + i + “,” + j + “) is ” + Math.max(i,j));
System.out.println(“max(” + x + “,” + y + “) is ” + Math.max(x,y));
System.out.println(“max(” + i + “,” + x + “) is ” + Math.max(i,x));
System.out.println(“max(” + y + “,” + j + “) is ” + Math.max(y,j));

// The Math library defines a couple
// of useful constants

System.out.println(“Pi is ” + Math.PI);
System.out.println(“e is ” + Math.E);

// Trigonometric methods
// All arguments are given in radians

// Convert a 45 degree angle to radians
double angle = 45.0 * 2.0 * Math.PI/360.0;
System.out.println(“cos(” + angle + “) is ” + Math.cos(angle));
System.out.println(“sin(” + angle + “) is ” + Math.sin(angle));

// Inverse Trigonometric methods
// All values are returned as radians

double value = 0.707;

System.out.println(“acos(” + value + “) is ” + Math.acos(value));
System.out.println(“asin(” + value + “) is ” + Math.asin(value));
System.out.println(“atan(” + value + “) is ” + Math.atan(value));

// Exponential and Logarithmic Methods

// exp(a) returns e (2.71828…) raised
// to the power of a.
System.out.println(“exp(1.0) is ” + Math.exp(1.0));
System.out.println(“exp(10.0) is ” + Math.exp(10.0));
System.out.println(“exp(0.0) is ” + Math.exp(0.0));

// log(a) returns the natural
// logarithm (base e) of a.
System.out.println(“log(1.0) is ” + Math.log(1.0));
System.out.println(“log(10.0) is ” + Math.log(10.0));
System.out.println(“log(Math.E) is ” + Math.log(Math.E));

// pow(x, y) returns the x raised
// to the yth power.

System.out.println(“pow(2.0, 2.0) is ” + Math.pow(2.0,2.0));
System.out.println(“pow(10.0, 3.5) is ” + Math.pow(10.0,3.5));
System.out.println(“pow(8, -1) is ” + Math.pow(8,-1));

// sqrt(x) returns the square root of x.

for (i=0; i < 10; i++) {
System.out.println(
"The square root of " + i + " is " + Math.sqrt(i));
}

// Finally there's one Random method
// that returns a pseudo-random number
// between 0.0 and 1.0;

System.out.println("Here's one random number: " + Math.random());
System.out.println("Here's another random number: " + Math.random());

}

}

Output:

i is 7
j is -9
x is 72.3
y is 0.34
|7| is 7
|-9| is 9
|72.3| is 72.3
|0.34| is 0.34
72.3 is approximately 72
0.34 is approximately 0
The ceiling of 7 is 7.0
The ceiling of -9 is -9.0
The ceiling of 72.3 is 73.0
The ceiling of 0.34 is 1.0
The floor of 7 is 7.0
The floor of -9 is -9.0
The floor of 72.3 is 72.0
The floor of 0.34 is 0.0
min(7,-9) is -9
min(72.3,0.34) is 0.34
min(7,72.3) is 7.0
min(0.34,-9) is -9.0
max(7,-9) is 7
max(72.3,0.34) is 72.3
max(7,72.3) is 72.3
max(0.34,-9) is 0.34
Pi is 3.141592653589793
e is 2.718281828459045
cos(0.7853981633974483) is 0.7071067811865476
sin(0.7853981633974483) is 0.7071067811865475
acos(0.707) is 0.7855491633997437
asin(0.707) is 0.785247163395153
atan(0.707) is 0.6154085176292563
exp(1.0) is 2.7182818284590455
exp(10.0) is 22026.465794806718
exp(0.0) is 1.0
log(1.0) is 0.0
log(10.0) is 2.302585092994046
log(Math.E) is 1.0
pow(2.0, 2.0) is 4.0
pow(10.0, 3.5) is 3162.2776601683795
pow(8, -1) is 0.125
The square root of 0 is 0.0
The square root of 1 is 1.0
The square root of 2 is 1.4142135623730951
The square root of 3 is 1.7320508075688772
The square root of 4 is 2.0
The square root of 5 is 2.23606797749979
The square root of 6 is 2.449489742783178
The square root of 7 is 2.6457513110645907
The square root of 8 is 2.8284271247461903
The square root of 9 is 3.0
Here’s one random number: 0.27783449993212095
Here’s another random number: 0.3521085788967532