Math Class Methods in Java with Examples

Java Math Class provides several methods to perform mathematical calculations such as avg(),  round(), floor(), max(), tan(), abs(), and so on.

Java Math methods

The java.lang.Math method contains several methods that are used to perform basic numeric operations such as square root, cube root, logarithm, exponential, and trigonometric operations. The various Java methods are described below:

Basic Math methods

  • abs()

This method returns the absolute value of any type of argument passed.

public class AbsMethod {

	public static void main(String[] args) {

		int first = -1;
		float second = .5f;

		System.out.println("Before int : " + first);
		System.out.println("Before float : " + second);

		// Now use of .bas() method to get Absolute Value

		int third = Math.abs(first);
		float fourth = Math.abs(second);

		System.out.println("Before int :" + third);
		System.out.println("Before float  : " + fourth);
	}
}
  • max()

This method returns the greater value out of the two arguments passed values.

public class MaxMethod {

	public static void main(String[] args) {

		int first = 1000;
		int second = 2000;
		System.out.println("Print the large number between first and second = " + Math.max(first, second));
	}
}
  • min()

This method returns the smallest value out of the two arguments passed values.

public class MinMethod {

	public static void main(String[] args) {

		int first = 1000;
		int second = 2020;
		System.out.println("Print the large number between first and second = " + Math.min(first, second));
	}
}
  • round()

This method rounds off the passed argument up to closest decimal places.

public class RoundMethod {

	public static void main(String[] args) {

		double first = 4000.40;
		double second = 3000.99;
		float third = 2000.25f;
		float fourth = 1000.76f;

		System.out.println(Math.round(first));
		System.out.println(Math.round(second));
		System.out.println(Math.round(third));
		System.out.println(Math.round(fourth));
	}
}
  • sqrt()

This method returns the square root value of the argument passed.

public class SqrtMethod {

	public static void main(String[] args) {

		double number = 30;

		System.out.println("Value of E is : "+Math.E);
		System.out.println("Now value of sqrt is :  "+ Math.sqrt(number));
	}
}
  • cbrt()

This method returns the cube root value of the argument passed.

public class CbrtMethod {

	public static void main(String[] args) {
		
	   	double first = 30.0;
		double second = -(40.0/0);
		double third = 50.0 / 0;
		double fourth = 0.0;

		System.out.println(Math.cbrt(first));
		System.out.println(Math.cbrt(second));
		System.out.println(Math.cbrt(third));
		System.out.println(Math.cbrt(fourth));
	}
}
  • pow()

This method returns the value as base raised to the power of the exponent.

public class PowMethod {

	public static void main(String[] args) {

		double first = 50;
		double second = 100;

		System.out.println(Math.pow(first, second));
	}
}
  • signum()

This method returns the signum value of the passed argument.

public class SignumMethod {

	public static void main(String[] args) {

		double first = 50;

		System.out.println(Math.signum(first));
	}
}
  • ceil()

This method is used to find the smallest possible value which is either greater or equal to the argument passed.

public class SignumMethod {

	public static void main(String[] args) {

		double first = 50;
		float second = 70;

		System.out.println(Math.ceil(first));
		System.out.println(Math.ceil(second));
	}
}
  • copySign()

This method returns the second argument with the sign of the first floating-point argument.

public class CopysignMethod {

	public static void main(String[] args) {

		double first = 50.11;
		double second = -0.50;

		System.out.println(Math.copySign(first, second));

		System.out.println(Math.copySign(second, first));
	}
}
  • nextAfter()

This method returns the floating-point number adjacent to the first argument toward the second argument direction.

public class NextAfterMethod {

	public static void main(String[] args) {

		double first = 0.0 / 0;
		double second = 10.9;

		System.out.println(Math.nextAfter(first, second));
	}
}
  • nextUp()

This method returns the floating-point value adjacent to d in the positive infinity direction.

public class NextupMethod {

	public static void main(String[] args) {

		double first = 99.99;
		float second = 10.9f;
		double third = 1.0 / 0;

		System.out.println(Math.nextUp(first));
		System.out.println(Math.nextUp(second));
		System.out.println(Math.nextUp(third));
	}
}
  • nextDown()

This method returns the floating-point value adjacent to d in the negative infinity direction.

public class NextdownMethod {

	public static void main(String[] args) {

		double first = 99.99;
		float second = 10.9f;
		double third = 1.0 / 0;

		System.out.println(Math.nextDown(first));
		System.out.println(Math.nextDown(second));
		System.out.println(Math.nextDown(third));
	}
}
  • floor()

This method returns the closest possible value that is either less than or equal to the passed argument.

public class FloorMethod {

	public static void main(String[] args) {

		double first = 99.99;
		float second = -10.9f;

		System.out.println(Math.floor(first));
		System.out.println(Math.floor(second));
	}
}
  • floorDiv()

This method returns the largest integer value that is less than or equal to the algebraic quotient.

public class FloordivMethod {

	public static void main(String[] args) {

		int first = 75;
		int second = 25;
		System.out.println(Math.floorDiv(first, second));
	}
}
  • random()

This method returns the double value with a positive sign that is greater than or equal to 0.0 and less than 1.0.

public class RandomMethod {

	public static void main(String[] args) {

		double rand = Math.random();

		System.out.println("Random Number: " + rand);

	}
}
  • rint()

This method returns the double value that is closest to the argument passed and similar to the mathematical integer.

public class JavaMathMethod {

	public static void main(String[] args) {

		double first = 15.5;
		double second = 12.12;

		double firstKey = Math.rint(first);
		double secondKey = Math.rint(second);

		System.out.println(firstKey);
		System.out.println(secondKey);
	}
}
  • hypot()

This method returns the hypotenuse of the right-angled triangle without intermediate overflow or underflow.

public class TestClass {

	public static void main(String[] args)   
    {  
        double first = 10;  
        double second = 15;  
        System.out.println(Math.hypot(first, second));
    }
}
  • ulp()

This method returns the least distance between two floating point numbers.

public class TestClass {

	public static void main(String[] args) {
		int first = 10; 
		System.out.println(Math.ulp(first));
	}
}
  • getExponent()

This method returns the unbiased exponent used in the representation of a value.

public class TestClass {

	public static void main(String[] args)   
    {  
        double num = 10.10;  
        System.out.println(Math.getExponent(num));  
    }  
}
  • IEEEremainder()

This method returns the remainder when a dividend is divided by a divisor.

public class TestClass {

	public static void main(String[] args) {
		double num1 = 100.99;
		double num2 = 10.99;
		System.out.println(Math.IEEEremainder(num1, num2));
	}
}
  • subtractExact()

This method returns the difference of the arguments passed.

public class TestClass {
	public static void main(String[] args) {

		long first = 999;
		long second = 99;
		System.out.println(Math.subtractExact(first, second));
	}
}
  • multiplyExact()

This method returns the product of the arguments passed.

public class TestClass {
	public static void main(String[] args) {
		int first = 555;
		int second = 50;
		System.out.println(Math.multiplyExact(first, second));
	}
}
  • incrementExact()

This method returns the incremented value of the argument.

public class TestClass {
	public static void main(String[] args) {
		int first = 999;
		System.out.println(Math.incrementExact(first));
	}
}
  • decrementExact()

This method returns the argument decremented by one.

public class TestClass {
	public static void main(String[] args) {
		int first = -990;
		System.out.println(Math.decrementExact(first));
	}
}
  • toIntExact()

This method returns the value of the long argument.

public class TestClass {
	public static void main(String[] args) {
		long num = 999;
		System.out.println(Math.toIntExact(num));
	}
}

Logarithmic Math Methods

  • log()

This method returns the logarithm value of the passed argument.

public class TestClass {

	public static void main(String[] args) {
		double num = 99.9;
		System.out.println(Math.log(num));
	}
}
  • log10()

This method returns the base 10 logarithm value of the argument passed.

public class TestClass {

	public static void main(String[] args) {
		double num = 9900000;
		System.out.println(Math.log10(num));
	}
}
  • log1p()

This method returns the natural logarithm of the sum of the argument and 1.

public class TestClass {

	public static void main(String[] args) {
		double num = 2500;
		System.out.println(Math.log1p(num));
	}
}
  • exp()

This method returns the Euler’s number rose to the power of the passed argument.

public class TestClass {

	public static void main(String[] args) {
		double num = 100.99;
		System.out.println(Math.exp(num));
	}
}
  • expm1()
 public class TestClass {

	public static void main(String[] args) {
		double num = 100.99;
		System.out.println(Math.expm1(num));
	}
}

Trigonometric Math Methods

  • sin()

This method returns the trigonometric sine value of the passed argument.

public class TestClass {

	public static void main(String[] args) {
		double num1 = 100;
		double num2 = Math.toRadians(num1);
		System.out.println(Math.sin(num2));
	}
}
  • cos()

This argument returns the trigonometric cosine value of the argument passed.

public class TestClass {

	public static void main(String[] args) {
		double first = 100;
		double second = StrictMath.toRadians(first);
		System.out.println(StrictMath.cos(second));
	}
}
  • tan()

This method returns the trigonometric tangent value of the argument passed.

	public static void main(String[] args) {
		double first = 100;
		double second = StrictMath.toRadians(first);
		System.out.println(StrictMath.tan(second));
	}
}
  • asin()

This method returns the arc sine value of the argument passed.

public class TestClass {

	public static void main(String[] args) {
		double num = 9.0;
		System.out.println(Math.asin(num));
	}
}
  • acos()

This method returns the arc cosine value of the argument passed.

public class TestClass {

	public static void main(String[] args) {
		double num = 9.0;
		System.out.println(Math.acos(num));
	}
}
  • atan()

This method returns the arctangent value of the passed argument.

public class TestClass {

	public static void main(String[] args) {
		double num = 100.123;
		System.out.println(Math.atan(num));
	}
}

Hyperbolic Math Methods

  • sinh()

This method returns the hyperbolic sine of the argument arg.

public class TestClass {

	public static void main(String[] args) {
		double num = 100.123;
		System.out.println(Math.sinh(num));
	}
}
  • cosh()

This method returns the hyperbolic cosine of the argument arg.

public class TestClass {

	public static void main(String[] args) {
		double num = 100.123;
		System.out.println(Math.cosh(num));
	}
}
  • tanh()

This method returns the hyperbolic tangent of the argument arg.

public class TestClass {

	public static void main(String[] args) {
		double num = 100.123;
		System.out.println(Math.tanh(num));
	}
}

Angular Math Methods

  • toDegree

This method converts the specified radians angle to equivalent angle measured in degree.

public class TestClass {
	public static void main(String[] args) {
		double num = Math.PI;
		System.out.println(Math.toDegrees(num));
	}
}
  • toRadians

This method returns the radians equivalent of the degree-argument passed.

public class TestClass {
	public static void main(String[] args) {
		double num = Math.PI;
		System.out.println(Math.toRadians(num));
	}
}

Leave a Reply

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