Most commonly used Java Math methods with Examples





Hey guys in this post, we will discuss the most commonly used Java Math methods with examples.

Math Methods


Following are the most commonly used java math methods that can be used in almost all real word java project.

  • abs()
  • random()
  • round()
  • pow()
  • min()
  • max()
  • sqrt()
  • cbrt()
  • ceil()
  • floor()
  • sin()
  • cos()
  • log()
  • exp()

Let’s look at the exmples one by one

abs(x)


It returns the absolute value of X. Below is the example for abs()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.abs(10)); //10
		System.out.println(Math.abs(-10)); //10
		System.out.println(Math.abs(-10.8)); //10.8
	}
}

random()


It returns the random number between 0 and 1. Below is the example for random()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.random()); //0.9840067831230108
		System.out.println(Math.random()); //0.13061950123529276
		System.out.println(Math.random()); //0.6652359817062997
	}
}

round(x)


It returns the value of X rounded to its nearest integer. Below is the example for round()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.round(6.3)); //6
		System.out.println(Math.round(-6.5)); //-6
		System.out.println(Math.round(6.6)); //7
	}
}

pow(x,y)


It returns the value of X to the power of Y. Below is the example for pow()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.pow(6, 2)); //36.0
		System.out.println(Math.pow(2, 1)); //2.0
		System.out.println(Math.pow(2.1, 3.2)); //10.74241047739471
	}
}

min(x,y)


It returns the number with the lowest value. Below is the example for min()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.min(10, 2)); //2
		System.out.println(Math.min(-6, -6.6)); //-6.6
		System.out.println(Math.min(-10, 2.3)); //-10.0
	}
}

max(x,y)


It returns the number with the highest value. Below is the example for max()



public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.max(10, 2)); //10
		System.out.println(Math.max(-6, -6.6)); //-6.0
		System.out.println(Math.max(-10, 2.3)); //2.3
	}
}

sqrt(x)


It returns the square root of X. Below is the example for sqrt()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.sqrt(2)); //1.4142135623730951
		System.out.println(Math.sqrt(-8)); //NaN
		System.out.println(Math.sqrt(2.5)); //1.5811388300841898
	}
}

cbrt(x)


It returns the cube root of X. Below is the example for cbrt()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.cbrt(8)); //2.0
		System.out.println(Math.cbrt(-8)); //-2.0
		System.out.println(Math.cbrt(-125)); //-5.0
	}
}

ceil(x)


It returns the value of X rounded up to its nearest integer. Below is the example for ceil()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.ceil(12.8)); //13.0
		System.out.println(Math.ceil(-12.8)); //-12.0
		System.out.println(Math.ceil(25)); //25.0
	}
}

floor(x)


It returns the value of X rounded down to its nearest integer. Below is the example for floor()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.floor(12.8)); //12.0
		System.out.println(Math.floor(-12.8)); //-13.0
		System.out.println(Math.floor(25)); //25.0
	}
}

sin(x)


It returns the sine of X (X is in radians). Below is the example for sin()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.sin(8.125 / 3)); //0.4198311934217366
		System.out.println(Math.sin(-8.125 / 3)); //-0.4198311934217366
		System.out.println(Math.sin(0)); //0.0
	}
}

cos(x)


It returns the cosine of X (X is in radians). Below is the example for cos()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.cos(8.125 / 3)); //-0.9076022085859423
		System.out.println(Math.cos(-8.125 / 3)); //-0.9076022085859423
		System.out.println(Math.cos(0)); //1.0
	}
}

log(x)


It returns the natural logarithm (base E) of X. Below is the example for log()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.log(0)); //-Infinity
		System.out.println(Math.log(1)); //0.0
		System.out.println(Math.log(Math.PI)); //1.1447298858494002
	}
}

exp(x)


It returns to the value of Ex. Below is the example for exp()

public class Test {
	
	public static void main(String[] args) {
		System.out.println(Math.exp(0)); //1.0
		System.out.println(Math.exp(1)); //2.718281828459045
		System.out.println(Math.exp(Math.PI)); //23.140692632779267
	}
}




About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *