Java Math Class

Filed Under: Java

Java Math class is a part of the java.lang package. Basically Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root and trigonometric functions. Java Math is a final class and it extends java.lang.Object.

Java Math Class Fields

Java Math class has below static fields:

  1. public static final double E: The double value that is closer than any other to e, the base of the natural logarithms(2.718281828459045).
  2. public static final double PI: The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter(3.141592653589793).

Java Math Class

Java Math Functions

Java Math class contains static factory method for performing basic numeric operations. Let’s have a look at the below methods with examples.

  1. abs(double a): This method returns the absolute value of specified double value.
  2. abs(float a): This method returns the absolute value of specified float value.
  3. NOTE: For the above methods let’s have a look at the below cases:

    • If the specified argument is positive zero or negative zero, the result is positive zero.
    • If the specified argument is not negative, then the argument is returned.
    • If the specified argument is negative, then the negation of the argument is returned.
    • If the specified argument is infinite, the result is positive infinity.
    • If the specified argument is NaN, the result is NaN.
  4. (int a): This method returns the absolute value of specified int value. If the specified argument is equal to the value of Integer.MIN_VALUE(-231) or the most negative representable int value, then the result is that same value, which is negative.
  5. abs(long a) method returns the absolute value of specified long value. If the specified argument is equal to the value of Long.MIN_VALUE(-263) or the most negative representable long value, then the result is that same value, which is negative.
  6. NOTE: For the above methods let’s have a look at the below cases:

    • If the specified argument is not negative, then the argument is returned.
    • If the specified argument is negative, then the negation of the argument is returned.

    Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math abs() method example
     * 
     * @author pankaj
     *
     */
    public class MathAbsExample {
    
    	public static void main(String[] args) {
    		try {
    			//For integers
    			System.out.println("Absolute value of Positive integer: "+Math.abs(5));
    			System.out.println("Absolute value of Negative integer: "+Math.abs(-5));
    			System.out.println("Absolute value of Negative Zero: "+Math.abs(-0));
    			System.out.println("Absolute value of Most Negative integer: "+Math.abs(Integer.MIN_VALUE));
    			
    			//For longs
    			System.out.println("Absolute value of Positive long: "+Math.abs(10L));
    			System.out.println("Absolute value of Negative long: "+Math.abs(-10L));
    			System.out.println("Absolute value of Most Negative long: "+Math.abs(Long.MIN_VALUE));
    			
    			//For doubles
    			System.out.println("Absolute value of Positive double: "+Math.abs(5.55));
    			System.out.println("Absolute value of Negative double: "+Math.abs(-6.66));
    			
    			//For floats
    			System.out.println("Absolute value of Positive float: "+Math.abs(55.55f));
    			System.out.println("Absolute value of Negative float: "+Math.abs(-66.66f));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    Absolute value of Positive integer: 5
    Absolute value of Negative integer: 5
    Absolute value of Negative Zero: 0
    Absolute value of Most Negative integer: -2147483648
    Absolute value of Positive long: 10
    Absolute value of Negative long: 10
    Absolute value of Most Negative long: -9223372036854775808
    Absolute value of Positive double: 5.55
    Absolute value of Negative double: 6.66
    Absolute value of Positive float: 55.55
    Absolute value of Negative float: 66.66
    
  7. acos(double a): This method returns the arc cosine of the specified value of double. The value of the returned angle of cosine is in the range 0.0 through pi. If the specified argument is NaN or its absolute value is greater than 1, then the result is NaN.
  8. (double a): This method returns the arc sine of the specified value of double. The value of the returned angle of sine is in the range –pi/2 through pi/2.
    • If the specified argument is NaN or its absolute value is greater than 1, then the result is NaN.
    • If the argument is zero, then the result is a zero with the same sign as the argument.
  9. atan(double a): This method returns the arctangent of the specified value of double. The value of the returned angle of the tangent is in the range –pi/2 through pi/2.
    • If the specified argument is NaN, then the result is NaN.
    • If the argument is zero, then the result is a zero with the same sign as the argument.
  10. atan2(double y, double x): This method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). This method computes the phase theta by computing an arc tangent of y/x in the range of -pi to pi.
    • If either argument is NaN, then the result is NaN.
    • If the first argument is positive zero and the second argument is positive, or the first argument is positive and finite and the second argument is positive infinity, then the result is positive zero.
    • If the first argument is negative zero and the second argument is positive, or the first argument is negative and finite and the second argument is positive infinity, then the result is negative zero.
    • If the first argument is positive zero and the second argument is negative, or the first argument is positive and finite and the second argument is negative infinity, then the result is the double value closest to pi.
    • If the first argument is negative zero and the second argument is negative, or the first argument is negative and finite and the second argument is negative infinity, then the result is the double value closest to -pi.
    • If the first argument is positive and the second argument is positive zero or negative zero, or the first argument is positive infinity and the second argument is finite, then the result is the double value closest to pi/2.
    • If the first argument is negative and the second argument is positive zero or negative zero, or the first argument is negative infinity and the second argument is finite, then the result is the double value closest to -pi/2.
    • If both arguments are positive infinity, then the result is the double value closest to pi/4.
    • If the first argument is positive infinity and the second argument is negative infinity, then the result is the double value closest to 3*pi/4.
    • If the first argument is negative infinity and the second argument is positive infinity, then the result is the double value closest to -pi/4.
    • If both arguments are negative infinity, then the result is the double value closest to -3*pi/4.

    Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math different types of arc angle method example
     * 
     * @author pankaj
     *
     */
    public class MathArcExample {
    
    	public static void main(String[] args) {
    		try {
    			//acos method
    			System.out.println("acos Results:");
    			System.out.println(Math.acos(0.111));
    			System.out.println(Math.acos(1.11));
    			
    			//asin method
    			System.out.println("asin Results:");
    			System.out.println(Math.asin(0.0));
    			System.out.println(Math.asin(-0.0));
    			System.out.println(Math.asin(1.11));
    			System.out.println(Math.asin(0.11));
    			
    			//atan method
    			System.out.println("atan Results:");
    			System.out.println(Math.atan(0));
    			System.out.println(Math.atan(1.1));
    			
    			//atan2 method
    			System.out.println("atan2 Results:");
    			System.out.println(Math.atan2(0.0, 0.0));
    			System.out.println(Math.atan2(-0.0, 0.0));
    			System.out.println(Math.atan2(90.0, 45.0));
    			System.out.println(Math.atan2(-0.0, -5.0));
    			System.out.println(Math.atan2(-5.0, -5.0));
    			System.out.println(Math.atan2(4.0, -5.0));
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    acos Results:
    1.4595671151542782
    NaN
    asin Results:
    0.0
    -0.0
    NaN
    0.11022304998774664
    atan Results:
    0.0
    0.8329812666744317
    atan2 Results:
    0.0
    -0.0
    1.1071487177940904
    -3.141592653589793
    -2.356194490192345
    2.4668517113662407
    
  11. cbrt(double a) This method returns the cube root of the specified value of double. For positive finite x, cbrt(-x) == -cbrt(x); that is, the cube root of a negative value is the negative of the cube root of that value’s magnitude.
    • If the argument is NaN, then the result is NaN.
    • If the argument is infinite, then the result is infinity with the same sign as the argument.
    • If the argument is zero, then the result is a zero with the same sign as the argument.
  12. ceil(double a): This method returns the smallest (closest to negative infinity) double value that is greater than or equal to the specified argument and is equal to a mathematical integer.
    • If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
    • If the argument is NaN or infinity or positive zero or negative zero, then the result is the same as the argument.
    • If the argument value is less than zero but greater than -1.0, then the result is negative zero.

    Note: value of Math.ceil(x) is exactly the value of -Math.floor(-x).

    Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math cbrt and ceil methods example
     * 
     * @author pankaj
     *
     */
    public class MathMethodExample {
    
    	public static void main(String[] args) {
    		try {
    			//cbrt method
    			System.out.println("cube root of positive zero:" +Math.cbrt(0.0));
    			System.out.println("cube root of negative zero:" +Math.cbrt(-0.0));
    			System.out.println("cube root of positive number:" +Math.cbrt(27.0));
    			System.out.println("cube root of negative number:" +Math.cbrt(-64.0));
    			
    			//ceil method
    			System.out.println("ceil of positive zero:"+Math.ceil(0.0));
    			System.out.println("ceil of negative zero:" +Math.ceil(-0.0));
    			System.out.println("ceil of infine value:" +Math.ceil(1.0/0.0));
    			System.out.println("ceil of positive number:" +Math.ceil(5.4));
    			System.out.println("ceil of negative number:" +Math.ceil(-5.4));
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    cube root of positive zero:0.0
    cube root of negative zero:-0.0
    cube root of positive number:3.0
    cube root of negative number:-4.0
    ceil of positive zero:0.0
    ceil of negative zero:-0.0
    ceil of infine value:Infinity
    ceil of positive number:6.0
    ceil of negative number:-5.0
    
  13. exp(double a): This method returns the Euler’s number e raised to the power of a specified double value.
    • If the argument is NaN, the result is NaN.
    • If the argument is positive infinity, then the result is positive infinity.
    • If the argument is negative infinity, then the result is positive zero.
  14. expm1(double x): This method Returns ex -1. Note that for values of x near 0, the exact sum of expm1(x) + 1 is much closer to the true result of ex than exp(x). Specified argument x is the exponent to raise e to in the computation of ex -1.
    • If the argument is NaN, the result is NaN.
    • If the argument is positive infinity, then the result is positive infinity.
    • If the argument is negative infinity, then the result is -1.0.
    • If the argument is zero, then the result is a zero with the same sign as the argument.

    The result of expm1 for any finite input must be greater than or equal to -1.0.

  15. floor(double a): This method Returns the largest (closest to positive infinity) double value that is less than or equal to the specified argument and is equal to a mathematical integer.
    • If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
    • If the argument is NaN or infinity or positive zero or negative zero, then the result is the same as the argument.

    Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math exp, expm1 and floor methods example
     * 
     * @author pankaj
     *
     */
    public class MathMethodExample2 {
    
    	public static void main(String[] args) {
    		try {
    			//exp method
    			System.out.println("exp of positive number:"+Math.exp(5));
    			System.out.println("exp of positive infinite:"+Math.exp(Double.POSITIVE_INFINITY));
    			System.out.println("exp of negative infinite:"+Math.exp(Double.NEGATIVE_INFINITY));
    			System.out.println("exp of NaN:"+Math.exp(Double.NaN));
    			System.out.println("exp of zero:"+Math.exp(0.0));
    			
    			//expm1 method
    			System.out.println("expm1 of positive number:"+Math.expm1(5));
    			System.out.println("expm1 of positive infinite:"+Math.expm1(Double.POSITIVE_INFINITY));
    			System.out.println("expm1 of negative infinite:"+Math.expm1(Double.NEGATIVE_INFINITY));
    			System.out.println("expm1 of Nan:"+Math.expm1(Double.NaN));
    			System.out.println("expm1 of zero:"+Math.expm1(0.0));
    			
    			//floor method
    			System.out.println("floor of positive number:"+Math.floor(55.0));
    			System.out.println("floor of negative number:"+Math.floor(-66.0));
    			System.out.println("floor of positive zero:"+Math.floor(0.0));
    			System.out.println("floor of negative zero:"+Math.floor(-0.0));
    			System.out.println("floor of positive infinite:"+Math.floor(Double.POSITIVE_INFINITY));
    			System.out.println("floor of negative infinite:"+Math.floor(Double.NEGATIVE_INFINITY));
    			System.out.println("floor of NaN:"+Math.floor(Double.NaN));
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    exp of positive number:148.4131591025766
    exp of positive infinite:Infinity
    exp of negative infinite:0.0
    exp of NaN:NaN
    exp of zero:1.0
    expm1 of positive number:147.4131591025766
    expm1 of positive infinite:Infinity
    expm1 of negative infinite:-1.0
    expm1 of Nan:NaN
    expm1 of zero:0.0
    floor of positive number:55.0
    floor of negative number:-66.0
    floor of positive zero:0.0
    floor of negative zero:-0.0
    floor of positive infinite:Infinity
    floor of negative infinite:-Infinity
    floor of NaN:NaN
    
  16. log(double a): This method returns the natural logarithm (base e) of a specified double value.
    • If the argument is NaN or less than zero, then the result is NaN.
    • If the argument is positive infinity, then the result is positive infinity.
    • If the argument is positive zero or negative zero, then the result is negative infinity.
  17. log10(double a): This method returns the base 10 logarithm of a specified double value.
    • If the argument is NaN or less than zero, then the result is NaN.
    • If the argument is positive infinity, then the result is positive infinity.
    • If the argument is positive zero or negative zero, then the result is negative infinity.
    • If the argument is equal to 10n for integer n, then the result is n.
  18. log1p(double x): This method returns the natural logarithm of the sum of the argument and 1. Note that for small values x, the result of log1p(x) is much closer to the true result of ln(1 + x) than the floating-point evaluation of log(1.0+x).
    • If the argument is NaN or less than -1, then the result is NaN.
    • If the argument is positive infinity, then the result is positive infinity.
    • If the argument is negative one, then the result is negative infinity.
    • If the argument is zero, then the result is a zero with the same sign as the argument.

    Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math log, log10 and log1p methods example
     * 
     * @author pankaj
     *
     */
    public class MatLogMethodExample {
    
    	public static void main(String[] args) {
    		try {
    			//log method
    			System.out.println("log of positive number:"+Math.log(60));
    			System.out.println("log of negative number:"+Math.log(-66));
    			System.out.println("log of positive infinite:"+Math.log(Double.POSITIVE_INFINITY));
    			System.out.println("log of negative infinite:"+Math.log(Double.NEGATIVE_INFINITY));
    			System.out.println("log of NaN:"+Math.log(Double.NaN));
    			System.out.println("log of positive zero:"+Math.log(0.0));
    			System.out.println("log of negative zero:"+Math.log(-0.0));
    			
    			//log10 method
    			System.out.println("log10 of positive number:"+Math.log10(100));
    			System.out.println("log10 of negative number:"+Math.log10(-10));
    			System.out.println("log10 of positive infinite:"+Math.log10(Double.POSITIVE_INFINITY));
    			System.out.println("log10 of negative infinite:"+Math.log10(Double.NEGATIVE_INFINITY));
    			System.out.println("log10 of NaN:"+Math.log10(Double.NaN));
    			System.out.println("log10 of positive zero:"+Math.log10(0.0));
    			System.out.println("log10 of negative zero:"+Math.log10(-0.0));
    			
    			//log1p method
    			System.out.println("log1p of positive number:"+Math.log1p(100));
    			System.out.println("log1p of negative number:"+Math.log1p(-1));
    			System.out.println("log1p of positive infinite:"+Math.log1p(Double.POSITIVE_INFINITY));
    			System.out.println("log1p of negative infinite:"+Math.log1p(Double.NEGATIVE_INFINITY));
    			System.out.println("log1p of NaN:"+Math.log1p(Double.NaN));
    			System.out.println("log1p of positive zero:"+Math.log1p(0.0));
    			System.out.println("log1p of negative zero:"+Math.log1p(-0.0));
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    log of positive number:4.0943445622221
    log of negative number:NaN
    log of positive infinite:Infinity
    log of negative infinite:NaN
    log of NaN:NaN
    log of positive zero:-Infinity
    log of negative zero:-Infinity
    log10 of positive number:2.0
    log10 of negative number:NaN
    log10 of positive infinite:Infinity
    log10 of negative infinite:NaN
    log10 of NaN:NaN
    log10 of positive zero:-Infinity
    log10 of negative zero:-Infinity
    log1p of positive number:4.61512051684126
    log1p of negative number:-Infinity
    log1p of positive infinite:Infinity
    log1p of negative infinite:NaN
    log1p of NaN:NaN
    log1p of positive zero:0.0
    log1p of negative zero:-0.0
    
  19. max(double a, double b): This method returns the greater of specified two double values. The result is the argument closer to positive infinity.
  20. max(float a, float b) This method returns the greater of specified two float values. The result is the argument closer to positive infinity.
  21. NOTE: For the above methods let’s have a look at the below cases:

    • If either value of specified argument is NaN, then the result is NaN.
    • If the arguments have the same value, the result is that same value.
    • If one argument is positive zero and the other negative zero, the result is positive zero.
  22. max(long a, long b) This method returns the greater of specified two long values. The result is the argument closer to the value of Long.MAX_VALUE(263-1). If the arguments have the same value, the result is that same value.
  23. max(int a, int b) This method returns the greater of specified two int values. The result is the argument closer to the value of Integer.MAX_VALUE(231-1). If the arguments have the same value, the result is that same value.
  24. min(double a, double b) This method returns the smaller of specified two double values. The result is the argument closer to negative infinity.
  25. min(float a, float b) This method returns the smaller of specified two float values. The result is the argument closer to negative infinity.
  26. NOTE: For the above methods let’s have a look at the below cases:

    • If either value of specified argument is NaN, then the result is NaN.
    • If the arguments have the same value, the result is that same value.
    • If one argument is positive zero and the other negative zero, the result is negative zero.
  27. min(long a, long b) This method returns the smaller of specified two long values. The result is the argument closer to the value of Long.MIN_VALUE(-263). If the arguments have the same value, the result is that same value.
  28. min(int a, int b) This method returns the smaller of specified two int values. The result is the argument closer to the value of Integer.MIN_VALUE(-231). If the arguments have the same value, the result is that same value.
  29. Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math min and max methods example
     * 
     * @author pankaj
     *
     */
    public class MathMinMaxMethodsExample {
    
    	public static void main(String[] args) {
    		try {
    			//min method
    			System.out.println("min of positive doubles:"+Math.min(50.55, 50.56));
    			System.out.println("min of positive floats:"+Math.min(10.5f, 11.6f));
    			System.out.println("min of positive longs:"+Math.min(557L, 656L));
    			System.out.println("min of positive ints:"+Math.min(5, 6));
    			System.out.println("min(-67, -55)="+Math.min(-67, -55));
    			System.out.println("min(-45.50, -78.9)="+Math.min(-45.50, -78.9));
    			System.out.println("min(-0.1, -0.0)="+Math.min(-0.1, -0.0));
    			System.out.println("min(55, NaN)="+Math.min(55, Double.NaN));
    			System.out.println("min(NaN, NaN)="+Math.min(Double.NaN, Double.NaN));
    			System.out.println("min(10, 10)="+Math.min(10, 10));
    			
    			//max method
    			System.out.println("max of positive doubles:"+Math.max(50.55, 50.56));
    			System.out.println("max of positive floats:"+Math.max(10.5f, 11.6f));
    			System.out.println("max of positive longs:"+Math.max(557L, 656L));
    			System.out.println("max of positive ints:"+Math.max(5, 6));
    			System.out.println("max(-67, -55)="+Math.max(-67, -55));
    			System.out.println("max(-45.50, -78.9)="+Math.max(-45.50, -78.9));
    			System.out.println("max(-0.1, -0.0)="+Math.max(-0.1, -0.0));
    			System.out.println("max(55, NaN)="+Math.max(55, Double.NaN));
    			System.out.println("max(NaN, NaN)="+Math.max(Double.NaN, Double.NaN));
    			System.out.println("max(10, 10)="+Math.max(10, 10));
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    min of positive doubles:50.55
    min of positive floats:10.5
    min of positive longs:557
    min of positive ints:5
    min(-67, -55)=-67
    min(-45.50, -78.9)=-78.9
    min(-0.1, -0.0)=-0.1
    min(55, NaN)=NaN
    min(NaN, NaN)=NaN
    min(10, 10)=10
    max of positive doubles:50.56
    max of positive floats:11.6
    max of positive longs:656
    max of positive ints:6
    max(-67, -55)=-55
    max(-45.50, -78.9)=-45.5
    max(-0.1, -0.0)=-0.0
    max(55, NaN)=NaN
    max(NaN, NaN)=NaN
    max(10, 10)=10
    
  30. pow(double a, double b) This method returns the value of the first argument raised to the power of the second argument.
    • If the second argument is positive or negative zero, then the result is 1.0.
    • If the second argument is 1.0, then the result is the same as the first argument.
    • If the second argument is NaN, then the result is NaN.
    • If the first argument is NaN and the second argument is nonzero, then the result is NaN.
    • If the absolute value of the first argument is greater than 1 and the second argument is positive infinity, or the absolute value of the first argument is less than 1 and the second argument is negative infinity, then the result is positive infinity.
    • If the absolute value of the first argument is greater than 1 and the second argument is negative infinity, or the absolute value of the first argument is less than 1 and the second argument is positive infinity, then the result is positive zero.
    • If the absolute value of the first argument equals 1 and the second argument is infinite, then the result is NaN.
    • If the first argument is positive zero and the second argument is greater than zero, or the first argument is positive infinity and the second argument is less than zero, then the result is positive zero.
    • If the first argument is positive zero and the second argument is less than zero, or the first argument is positive infinity and the second argument is greater than zero, then the result is positive infinity.
    • If the first argument is negative zero and the second argument is greater than zero but not a finite odd integer, or the first argument is negative infinity and the second argument is less than zero but not a finite odd integer, then the result is positive zero.
    • If the first argument is negative zero and the second argument is a positive finite odd integer, or the first argument is negative infinity and the second argument is a negative finite odd integer, then the result is negative zero.
    • If the first argument is negative zero and the second argument is less than zero but not a finite odd integer, or the first argument is negative infinity and the second argument is greater than zero but not a finite odd integer, then the result is positive infinity.
    • If the first argument is negative zero and the second argument is a negative finite odd integer, or the first argument is negative infinity and the second argument is a positive finite odd integer, then the result is negative infinity.
    • If the first argument is finite and less than zero and
      – if the second argument is a finite even integer, the result is equal to the result of raising the absolute value of the first argument to the power of the second argument
      – if the second argument is a finite odd integer, the result is equal to the negative of the result of raising the absolute value of the first argument to the power of the second argument
      – if the second argument is finite and not an integer, then the result is NaN.

    Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math pow() method example
     * 
     * @author pankaj
     *
     */
    public class MathPowExample {
    
    	public static void main(String[] args) {
    		try {
    			System.out.println("pow(5, -0.0)="+Math.pow(5, -0.0));
    			System.out.println("pow(44, 0.0)="+Math.pow(44, 0.0));
    			System.out.println("pow(45, 1.0)="+Math.pow(45, 1.0));
    			System.out.println("pow(67, NaN)="+Math.pow(67, Double.NaN));
    			System.out.println("pow(NaN, 77)="+Math.pow(Double.NaN, 77));
    			System.out.println("pow(5, 1.0/0)="+Math.pow(5, 1.0/0));
    			System.out.println("pow(0, -1.0/0)="+Math.pow(0, -1.0/0));
    			System.out.println("pow(9, -1.0/0)="+Math.pow(9, -1.0/0));
    			System.out.println("pow(0, 1.0/0)="+Math.pow(0, 1.0/0));
    			System.out.println("pow(1, 1.0/0)="+Math.pow(1, 1.0/0));
    			System.out.println("pow(0, 6)="+Math.pow(0, 6));
    			System.out.println("pow(1.0/0, -1)="+Math.pow(1.0/0, -1));
    			System.out.println("pow(0.0, -10)="+Math.pow(0.0, -10));
    			System.out.println("pow(1.0/0, 67)="+Math.pow(1.0/0, 67));
    			System.out.println("pow(-0.0, 44.50)="+Math.pow(-0.0, 44.50));
    			System.out.println("pow(-1.0/0, -7.9)="+Math.pow(-1.0/0, -7.9));
    			System.out.println("pow(-0.0, 7)="+Math.pow(-0.0, 7));
    			System.out.println("pow(-1.0/0, -3)="+Math.pow(-1.0/0, -3));
    			System.out.println("pow(-0.0, -44.50)="+Math.pow(-0.0, -44.50));
    			System.out.println("pow(-1.0/0, 6.7)="+Math.pow(-1.0/0, 6.7));
    			System.out.println("pow(-0.0, -3)="+Math.pow(-0.0, -3));
    			System.out.println("pow(-1.0/0, 5)="+Math.pow(-1.0/0, 5));
    			System.out.println("pow(-1.0, 6)="+Math.pow(-1.0, 6));
    			System.out.println("pow(-2.0, 6)="+Math.pow(-2.0, 7));
    			System.out.println("pow(-5.0, 6.6)="+Math.pow(-5.0, 6.6));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    pow(5, -0.0)=1.0
    pow(44, 0.0)=1.0
    pow(45, 1.0)=45.0
    pow(67, NaN)=NaN
    pow(NaN, 77)=NaN
    pow(5, 1.0/0)=Infinity
    pow(0, -1.0/0)=Infinity
    pow(9, -1.0/0)=0.0
    pow(0, 1.0/0)=0.0
    pow(1, 1.0/0)=NaN
    pow(0, 6)=0.0
    pow(1.0/0, -1)=0.0
    pow(0.0, -10)=Infinity
    pow(1.0/0, 67)=Infinity
    pow(-0.0, 44.50)=0.0
    pow(-1.0/0, -7.9)=0.0
    pow(-0.0, 7)=-0.0
    pow(-1.0/0, -3)=-0.0
    pow(-0.0, -44.50)=Infinity
    pow(-1.0/0, 6.7)=Infinity
    pow(-0.0, -3)=-Infinity
    pow(-1.0/0, 5)=-Infinity
    pow(-1.0, 6)=1.0
    pow(-2.0, 6)=-128.0
    pow(-5.0, 6.6)=NaN
    
  31. random() This method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudo randomly with uniform distribution from that range.

    When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression: new java.util.Random(). This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.

    This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.

  32. round(double a) This method returns the closest long to the specified double argument, with ties rounding up.
    • If the argument is NaN, the result is 0.
    • If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE(-263), the result is equal to the value of Long.MIN_VALUE.
    • If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE(263-1), the result is equal to the value of Long.MAX_VALUE.
  33. round(float a) This method returns the closest int to the specified float argument, with ties rounding up.
    • If the argument is NaN, the result is 0.
    • If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE(-231), the result is equal to the value of Integer.MIN_VALUE.
    • If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE(231-1), the result is equal to the value of Integer.MAX_VALUE.

    Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math random and round methods example
     * 
     * @author pankaj
     *
     */
    public class MathMethodExample3 {
    
    	public static void main(String[] args) {
    		try {
    			//random method
    			System.out.println("random number:"+Math.random());
    			
    			//round method
    			System.out.println("round(0.0)="+Math.round(0.0));
    			System.out.println("round(-0.0)="+Math.round(-0.0));
    			System.out.println("round(NaN)="+Math.round(Double.NaN));
    			System.out.println("round(-1.0/0)="+Math.round(-1.0/0));
    			System.out.println("round(1.0/0)="+Math.round(1.0/0));
    			System.out.println("round(55.789)="+Math.round(55.789));
    			System.out.println("round(55)="+Math.round(55));
    			System.out.println("round(5.6f)="+Math.round(5.6f));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    random number:0.6910799189555229
    round(0.0)=0
    round(-0.0)=0
    round(NaN)=0
    round(-1.0/0)=-9223372036854775808
    round(1.0/0)=9223372036854775807
    round(55.789)=56
    round(55)=55
    round(5.6f)=6
    
  34. toDegrees(double angrad) This method converts an angle measured in radians to an approximately equivalent angle measured in degrees. The conversion from radians to degrees is generally inexact.
  35. toRadians(double angred): This method converts an angle measured in degrees to an approximately equivalent angle measured in radians. The conversion from degrees to radians is generally inexact.
  36. Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math toDegrees and toRadians methods example
     * 
     * @author pankaj
     *
     */
    public class MathMethodExample4 {
    
    	public static void main(String[] args) {
    		try {
    			//toDegrees method
    			System.out.println("1.5 radians into degrees="+Math.toDegrees(1.5));
    			
    			//toRadians method
    			System.out.println("45 degrees into radians="+Math.toRadians(45));
    			System.out.println("90 degrees into radians="+Math.toRadians(90));
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    1.5 radians into degrees=85.94366926962348
    45 degrees into radians=0.7853981633974483
    90 degrees into radians=1.5707963267948966
    

New Methods in Math Class in Java 8

Let’s have a look at the below newly added methods in Math class in java 8 with examples.

  1. addExact(long x, long y): This method returns the sum of its specified long arguments, it throws ArithmeticException – if the result overflows a long.
  2. addExact(int x, int y) This method returns the sum of its specified int arguments, it throws ArithmeticException – if the result overflows a int.
  3. Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math addExact method example
     * 
     * @author pankaj
     *
     */
    public class MathaddExactMethodExample {
    
    	public static void main(String[] args) {
    		try {
    			System.out.println("sume of 5 and 8="+Math.addExact(5, 8));
    			System.out.println(Math.addExact(Integer.MAX_VALUE, 1));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    sume of 5 and 8=13
    java.lang.ArithmeticException: integer overflow
    	at java.lang.Math.addExact(Unknown Source)
    	at com.journaldev.examples.MathaddExactMethodExample.main(MathaddExactMethodExample.java:14)
    
  4. subtractExact(long x, long y) This method returns the difference of the specified long arguments, it throws ArithmeticException – if the result overflows a long.
  5. subtractExact(int x, int y) This method returns the difference of the specified int arguments, it throws ArithmeticException – if the result overflows an int.
  6. Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math subtractExact method example
     * 
     * @author pankaj
     *
     */
    public class MathsubtractExactMethodExample {
    
    	public static void main(String[] args) {
    		try {
    			System.out.println("difference of 5 and 8="+Math.subtractExact(5, 8));
    			System.out.println(Math.subtractExact(Long.MIN_VALUE, 1));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    difference of 5 and 8=-3
    java.lang.ArithmeticException: long overflow
    	at java.lang.Math.subtractExact(Unknown Source)
    	at com.journaldev.examples.MathsubtractExactMethodExample.main(MathsubtractExactMethodExample.java:14)
    
  7. incrementExact(int a) This method returns the specified argument incremented by one, it throws ArithmeticException – if the result overflows an int.
  8. incrementExact(long a) This method returns the specified argument incremented by one, it throws ArithmeticException – if the result overflows an long.
  9. Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math incrementExact methods example
     * 
     * @author pankaj
     *
     */
    public class MathIncremenExactExample {
    
    	public static void main(String[] args) {
    		try {
    			//incrementExact method
    			System.out.println("increment of 100 ="+Math.incrementExact(100));
    			System.out.println(Math.incrementExact(Integer.MAX_VALUE));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    increment of 100 =101
    java.lang.ArithmeticException: integer overflow
    	at java.lang.Math.incrementExact(Unknown Source)
    	at com.journaldev.examples.MathIncrementDecrementExactExample.main(MathIncrementDecrementExactExample.java:15)
    
  10. decrementExact(int a) This method returns the specified argument decremented by one, it throws ArithmeticException – if the result overflows an int.
  11. decrementExact(long a) This method returns the specified argument decremented by one, it throws ArithmeticException – if the result overflows an long.
  12. Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math decrementExact methods example
     * 
     * @author pankaj
     *
     */
    public class MathDecremenExactExample {
    
    	public static void main(String[] args) {
    		try {
    			//decrementExact method
    			System.out.println("decrement of 100 ="+Math.decrementExact(100));
    			System.out.println(Math.decrementExact(Integer.MIN_VALUE));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    decrement of 100 =99
    java.lang.ArithmeticException: integer overflow
    	at java.lang.Math.decrementExact(Unknown Source)
    	at com.journaldev.examples.MathDecremenExactExample.main(MathDecremenExactExample.java:15)
    
  13. multiplyExact(int x, int y) This method returns the multiplication of the specified arguments, it throws ArithmeticException – if the result overflows an int.
  14. multiplyExact(long x, long y) This method returns the multiplication of the specified arguments, it throws ArithmeticException – if the result overflows an long.
  15. Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math multiplyExact methods example
     * 
     * @author pankaj
     *
     */
    public class MathMultiplyExactExample {
    
    	public static void main(String[] args) {
    		try {
    			//multiplyExact method
    			System.out.println("multiplication of 100 and 5 ="+Math.multiplyExact(100, 5));
    			System.out.println(Math.multiplyExact(Integer.MAX_VALUE, 5));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    multiplication of 100 and 5 =500
    java.lang.ArithmeticException: integer overflow
    	at java.lang.Math.multiplyExact(Unknown Source)
    	at com.journaldev.examples.MathMultiplyExactExample.main(MathMultiplyExactExample.java:15)
    
  16. negateExact(int a) This method returns the negation of the specified argument, it throws ArithmeticException – if the result overflows an int.
  17. negateExact(long a) This method returns the negation of the specified argument, it throws ArithmeticException – if the result overflows an long.
  18. Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math negateExact methods example
     * 
     * @author pankaj
     *
     */
    public class MathNegateExactExample {
    
    	public static void main(String[] args) {
    		try {
    			//negateExact method
    			System.out.println("negation of 100 ="+Math.negateExact(100));
    			System.out.println(Math.negateExact(Integer.MIN_VALUE));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    negation of 100 =-100
    java.lang.ArithmeticException: integer overflow
    	at java.lang.Math.negateExact(Unknown Source)
    	at com.journaldev.examples.MathNegateExactExample.main(MathNegateExactExample.java:15)
    
  19. floorDiv(int x, int y) This method divides the first argument by second and call the floor() method upon returned result.
  20. floorMod(int x, int y) This method returns the floor value of modulus of the specified arguments.
  21. Let’s have look at the below example program.

    
    package com.journaldev.examples;
    
    /**
     * Java Math floorDiv and floorMod methods example
     * 
     * @author pankaj
     *
     */
    public class MathMethodExample5 {
    
    	public static void main(String[] args) {
    		try {
    			//floorDiv method
    			System.out.println("floorDiv(7, 2)="+Math.floorDiv(7, 2));
    			
    			//floorMod method
    			System.out.println("floorMod(7, 2)="+Math.floorMod(7, 2));
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    Output of the above program is below:

    
    floorDiv(7, 2)=3
    floorMod(7, 2)=1
    

That’s all for Java Math class, I hope nothing important got missed here.

References: Java 7 API Doc and Java 8 API Doc.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages