java 数学_Java数学课

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类是java.lang包的一部分。 基本上,Math类包含用于执行基本数值运算的方法,例如基本指数,对数,平方根和三角函数。 Java Math是最后一个类,它扩展了java.lang.Object

Java数学类字段 (Java Math Class Fields)

Java Math class has below static fields:

Java Math类具有以下静态字段:

  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).

    public static final double E :比自然对数e (2.718281828459045)的底数更接近e的双精度值。
  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).

    public static final double PI :一个比pi更近的double值,即圆周的周长与其直径之比(3.141592653589793)。

Java数学函数 (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.

Java Math类包含用于执行基本数字运算的静态工厂方法。 让我们用示例来看看下面的方法。

  1. abs(double a): This method returns the absolute value of specified double value.

    abs(double a) :此方法返回指定double值的绝对值。
  2. abs(float a): This method returns the absolute value of specified float value.

    abs(float a) :此方法返回指定浮点值的绝对值。
  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.

    如果指定的参数为NaN,则结果为NaN。
  • (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.

    (int a) :此方法返回指定的int值的绝对值。 如果指定的参数等于Integer.MIN_VALUE(-231)的值或最负的可表示int值,则结果为相同的值,为负。
  • 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.

    abs(long a)方法返回指定的long值的绝对值。 如果指定的参数等于Long.MIN_VALUE(-263)的值或最负的可表示长值,则结果为相同的值,即为负。
  • 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
  • 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.

    acos(double a) :此方法返回指定double值的反余弦值。 余弦返回角的值在0.0到pi之间。 如果指定的自变量为NaN或其绝对值大于1,则结果为NaN。
  • (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.

    (double a) :此方法返回指定双精度值的反正弦值。 返回的正弦角值在–pi / 2至pi / 2的范围内。
    • If the specified argument is NaN or its absolute value is greater than 1, then the result is NaN.

      如果指定的自变量为NaN或其绝对值大于1,则结果为NaN。
    • If the argument is zero, then the result is a zero with the same sign as the argument.

      如果自变量为零,则结果为零,其符号与自变量相同。
  • 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.

    atan(double a) :此方法返回指定double值的反正切。 切线返回角的值在–pi / 2至pi / 2的范围内。
    • If the specified argument is NaN, then the result is NaN.

      如果指定的参数为NaN,则结果为NaN。
    • If the argument is zero, then the result is a zero with the same sign as the argument.

      如果自变量为零,则结果为零,其符号与自变量相同。
  • 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.

    atan2(double y, double x) :此方法返回从直角坐标(x,y)转换为极坐标(r,theta)的角度theta。 此方法通过计算-pi到pi范围内的y / x的反正切值来计算相位theta。
    • If either argument is NaN, then the result is NaN.

      如果任一自变量为NaN,则结果为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.

      如果第一个参数为正零,第二个参数为负,或者第一个参数为正且有限,第二个参数为负无穷大,则结果是最接近pi的double值。
    • 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.

      如果第一个参数为负零,第二个参数为负,或者第一个参数为负且有限,第二个参数为负无穷大,则结果为最接近-pi的double值。
    • 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.

      如果第一个参数为正,第二个参数为正零或负零,或者第一个参数为正无穷大,而第二个参数为有限,那么结果是最接近pi / 2的double值。
    • 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.

      如果第一个参数为负,第二个参数为正零或负零,或者第一个参数为负无穷大,而第二个参数为有限,那么结果是最接近-pi / 2的double值。
    • If both arguments are positive infinity, then the result is the double value closest to pi/4.

      如果两个参数均为正无穷大,则结果是最接近pi / 4的double值。
    • 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.

      如果第一个参数为正无穷大,第二个参数为负无穷大,则结果是最接近3 * pi / 4的double值。
    • 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.

      如果第一个参数为负无穷大,第二个参数为正无穷大,则结果是最接近-pi / 4的double值。
    • If both arguments are negative infinity, then the result is the double value closest to -3*pi/4.

      如果两个参数均为负无穷大,则结果是最接近-3 * pi / 4的double值。

    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
  • 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.

    cbrt(double a)此方法返回指定double值的多维数据集根。 对于正有限x, cbrt(-x) == -cbrt(x); 也就是说,负值的立方根就是该值大小的立方根的负数。
    • If the argument is NaN, then the result is NaN.

      如果参数为NaN,则结果为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.

      如果自变量为零,则结果为零,其符号与自变量相同。
  • 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.

    ceil(double a) :此方法返回大于(等于指定参数)且等于数学整数的最小(最接近负无穷大)double值。
    • 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.

      如果自变量是NaN或无穷大或正零或负零,则结果与自变量相同。
    • If the argument value is less than zero but greater than -1.0, then the result is negative zero.

      如果参数值小于零但大于-1.0,则结果为负零。

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

    注意:Math.ceil(x)的值恰好是-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
  • exp(double a): This method returns the Euler’s number e raised to the power of a specified double value.

    exp(double a) :此方法返回欧拉数e升至指定double值的幂。
    • If the argument is NaN, the result is NaN.

      如果参数为NaN,则结果为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.

      如果参数为负无穷大,则结果为正零。
  • 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.

    expm1(double x):此方法返回ex -1。 请注意,对于x接近0的值,expm1(x)+1的确切总和比exp(x)更接近ex的真实结果。 指定的参数x是在ex -1的计算中将e提高到的指数。
    • If the argument is NaN, the result is NaN.

      如果参数为NaN,则结果为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.

      如果参数为负无穷大,则结果为-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.

    任何有限输入的expm1结果必须大于或等于-1.0。

  • 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.

    floor(double a) :此方法返回小于或等于指定参数且等于数学整数的最大(最接近正无穷大)double值。
    • 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.

      如果自变量是NaN或无穷大或正零或负零,则结果与自变量相同。

    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
  • log(double a): This method returns the natural logarithm (base e) of a specified double value.

    log(double a) :此方法返回指定double值的自然对数(以e为底)。
    • If the argument is NaN or less than zero, then the result is NaN.

      如果参数为NaN或小于零,则结果为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.

      如果参数为正零或负零,则结果为负无穷大。
  • log10(double a): This method returns the base 10 logarithm of a specified double value.

    log10(double a) :此方法返回指定double值的以10为底的对数。
    • If the argument is NaN or less than zero, then the result is NaN.

      如果参数为NaN或小于零,则结果为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.

      如果参数对于整数n等于10n,则结果为n。
  • 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).

    log1p(double x) :此方法返回参数和1的和的自然对数。请注意,对于较小的x值,log1p(x)的结果比ln(1 + x)的真实结果更接近log(1.0 + x)的浮点计算。
    • If the argument is NaN or less than -1, then the result is NaN.

      如果参数为NaN或小于-1,则结果为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
  • max(double a, double b): This method returns the greater of specified two double values. The result is the argument closer to positive infinity.

    max(double a, double b) :此方法返回指定的两个double值中的较大者。 结果是参数更接近于正无穷大。
  • max(float a, float b) This method returns the greater of specified two float values. The result is the argument closer to positive infinity.

    max(float a, float b)此方法返回指定的两个float值中的较大者。 结果是参数更接近于正无穷大。
  • 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.

      如果指定参数的任意一个值为NaN,则结果为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.

      如果一个自变量为正零,另一个自变量为负零,则结果为正零。
  • 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.

    max(long a, long b)此方法返回指定的两个long值中的较大者。 结果是参数更接近Long.MAX_VALUE(263-1)的值。 如果参数具有相同的值,则结果为相同的值。
  • 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.

    max(int a, int b)此方法返回指定的两个int值中的较大者。 结果是参数更接近Integer.MAX_VALUE(231-1)的值。 如果参数具有相同的值,则结果为相同的值。
  • min(double a, double b) This method returns the smaller of specified two double values. The result is the argument closer to negative infinity.

    min(double a, double b)此方法返回指定的两个double值中的较小者。 结果是参数接近负无穷大。
  • min(float a, float b) This method returns the smaller of specified two float values. The result is the argument closer to negative infinity.

    min(float a, float b)此方法返回指定的两个float值中的较小者。 结果是参数接近负无穷大。
  • 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.

      如果指定参数的任意一个值为NaN,则结果为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.

      如果一个自变量为正零,另一个自变量为负零,则结果为负零。
  • 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.

    min(long a, long b)此方法返回指定的两个long值中的较小者。 结果是参数更接近Long.MIN_VALUE(-263)的值。 如果参数具有相同的值,则结果为相同的值。
  • 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.

    min(int a, int b)此方法返回指定的两个int值中较小的一个。 结果是参数更接近Integer.MIN_VALUE(-231)的值。 如果参数具有相同的值,则结果为相同的值。
  • 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
  • pow(double a, double b) This method returns the value of the first argument raised to the power of the second argument.

    pow(double a, double b)此方法将第一个自变量的值返回第二个自变量的幂。
    • If the second argument is positive or negative zero, then the result is 1.0.

      如果第二个参数为正零或负零,则结果为1.0。
    • If the second argument is 1.0, then the result is the same as the first argument.

      如果第二个参数为1.0,则结果与第一个参数相同。
    • If the second argument is NaN, then the result is NaN.

      如果第二个参数为NaN,则结果为NaN。
    • If the first argument is NaN and the second argument is nonzero, then the result is NaN.

      如果第一个参数为NaN,第二个参数为非零,则结果为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.

      如果第一个自变量的绝对值大于1,第二个自变量的绝对值小于1,或者第二个自变量的绝对值小于1,则结果为正无穷大。
    • 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.

      如果第一个自变量的绝对值大于1并且第二个自变量的绝对值为负无穷大,或者第一个自变量的绝对值小于1并且第二个自变量为正无穷大,则结果为正零。
    • If the absolute value of the first argument equals 1 and the second argument is infinite, then the result is NaN.

      如果第一个参数的绝对值等于1,第二个参数为无穷大,则结果为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.

      如果第一个参数是有限的且小于零,则
      –如果第二个参数是有限偶数整数,则结果等于将第一个参数的绝对值提高到第二个参数的幂的结果
      –如果第二个参数是一个有限的奇数整数,则结果等于将第一个参数的绝对值提高到第二个参数的幂的结果的负数
      –如果第二个参数是有限的而不是整数,则结果为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
  • 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.

    random()此方法返回一个带正号的双精度值,大于或等于0.0且小于1.0。 返回值是伪随机选择的,具有从该范围均匀分布的值。

    首次调用此方法时,它会创建一个新的伪随机数生成器,就像通过以下表达式一样:new java.util.Random()。 此新的伪随机数生成器此后将用于此方法的所有调用,并且在其他任何地方都不会使用。

    此方法已正确同步,以允许多个线程正确使用。 但是,如果许多线程需要以很高的速率生成伪随机数,则可以减少每个线程拥有自己的伪随机数生成器的竞争。

  • round(double a) This method returns the closest long to the specified double argument, with ties rounding up.

    round(double a)此方法返回最接近指定double参数的long,并舍入为四舍五入。
    • If the argument is NaN, the result is 0.

      如果参数为NaN,则结果为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.

      如果参数为负无穷大或任何小于或等于Long.MIN_VALUE(-263)的值,则结果等于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.

      如果参数为正无穷大或任何大于或等于Long.MAX_VALUE(263-1)的值,则结果等于Long.MAX_VALUE的值。
  • round(float a) This method returns the closest int to the specified float argument, with ties rounding up.

    round(float a)此方法返回与指定的float参数最接近的int,并四舍五入。
    • If the argument is NaN, the result is 0.

      如果参数为NaN,则结果为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.

      如果参数为负无穷大或任何小于或等于Integer.MIN_VALUE(-231)的值,则结果等于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.

      如果参数为正无穷大或大于或等于Integer.MAX_VALUE(231-1)的值,则结果等于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
  • 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.

    toDegrees(double angrad)此方法将以弧度为单位的角度转换为以度为单位的近似等效角度。 从弧度到度的转换通常是不精确的。
  • 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.

    toRadians(double angred) :此方法将以度为单位的角度转换为以弧度为单位的近似等效角度。 从度到弧度的转换通常是不精确的。
  • 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

    Java 8中的数学类中的新方法 (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.

    让我们来看一下Java 8中Math类中以下新增的方法和示例。

    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.

      addExact(long x, long y) :此方法返回其指定的long参数的总和,并引发ArithmeticException-如果结果溢出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.

      addExact(int x, int y)此方法返回其指定的int参数的总和,如果结果溢出一个int,则抛出ArithmeticException。
    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.

      subtractExact(long x, long y)此方法返回指定的long参数的差,并抛出ArithmeticException-如果结果溢出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.

      subtractExact(int x, int y)此方法返回指定的int参数的差,如果结果溢出一个int,则抛出ArithmeticException。
    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.

      incrementExact(int a)此方法返回指定参数加1的结果,并抛出ArithmeticException-如果结果溢出一个int。
    8. incrementExact(long a) This method returns the specified argument incremented by one, it throws ArithmeticException – if the result overflows an long.

      incrementExact(long a)此方法返回指定的参数加1,然后抛出ArithmeticException-如果结果溢出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.

      decrementExact(int a)此方法返回指定的参数减一,并抛出ArithmeticException-如果结果溢出一个int。
    11. decrementExact(long a) This method returns the specified argument decremented by one, it throws ArithmeticException – if the result overflows an long.

      decrementExact(long a)此方法返回指定的参数减一,然后抛出ArithmeticException-如果结果长时间溢出。
    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.

      multiplyExact(int x, int y)此方法返回指定参数的乘法,并抛出ArithmeticException-如果结果溢出一个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.

      multiplyExact(long x, long y)此方法返回指定参数的乘法,如果结果溢出long,则抛出ArithmeticException。
    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.

      negateExact(int a)此方法返回指定参数的取反,如果结果溢出一个int,则抛出ArithmeticException。
    17. negateExact(long a) This method returns the negation of the specified argument, it throws ArithmeticException – if the result overflows an long.

      negateExact(long a)此方法返回指定参数的取反,如果结果长时间溢出,则抛出ArithmeticException。
    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.

      floorDiv(int x, int y)此方法将第一个参数除以第二个,并在返回结果时调用floor()方法。
    20. floorMod(int x, int y) This method returns the floor value of modulus of the specified arguments.

      floorMod(int x, int y)此方法返回指定参数的模数的下限值。
    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.

    这就是Java Math类的全部内容,我希望这里不会错过任何重要的事情。

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

    参考: Java 7 API DocJava 8 API Doc

    翻译自: https://www.journaldev.com/21201/java-math-class

    java 数学

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值