Java的Math类提供了多种数学计算方法,例如min()、max()、avg()、sin()、cos()、tan()、round()、ceil()、floor()、abs()等。

与某些StrictMath类的数值方法不同,Math类中等效函数的所有实现不能被定义为返回完全相同的逐位结果。

对于int或long类型的值,如果计算结果超出值的范围,那么addExact()、subtractExact()、multiplyExact()和toIntExact()方法会抛出ArithmeticException异常。

对于其他算术运算,例如递增、递减、除法、绝对值和取反,在特定的最小值或最大值情况下可能会发生溢出。

示例 1

public class JavaMathExample1    
{    
    public static void main(String[] args)     
    {    
        double x = 28;    
        double y = 4;    
          
        // 返回两个数中的最大值  
        System.out.println("Maximum number of x and y is: " +Math.max(x, y));   
          
        // 返回 y 的平方根   
        System.out.println("Square root of y is: " + Math.sqrt(y));   
          
        //返回4的28次方即28*28*28*28     
        System.out.println("Power of x and y is: " + Math.pow(x, y));      
  
        // 返回给定值的对数          
        System.out.println("Logarithm of x is: " + Math.log(x));   
        System.out.println("Logarithm of y is: " + Math.log(y));  
          
        // 当基数为 10 时,返回给定值的对数   
        System.out.println("log10 of x is: " + Math.log10(x));   
        System.out.println("log10 of y is: " + Math.log10(y));    
          
        // 返回 x + 1 的对数 
        System.out.println("log1p of x is: " +Math.log1p(x));    
  
        //  返回 2 的幂     
        System.out.println("exp of a is: " +Math.exp(x));    
          
        // 返回(2 的幂)-1 
        System.out.println("expm1 of a is: " +Math.expm1(x));  
    }    
}    

输出:

Maximum number of x and y is: 28.0
Square root of y is: 2.0
Power of x and y is: 614656.0
Logarithm of x is: 3.332204510175204
Logarithm of y is: 1.3862943611198906
log10 of x is: 1.4471580313422192
log10 of y is: 0.6020599913279624
log1p of x is: 3.367295829986474
exp of a is: 1.446257064291475E12
expm1 of a is: 1.446257064290475E12

示例 2

public class JavaMathExample2    
{    
    public static void main(String[] args)     
    {    
        double a = 30;    
          
        // 将值转换为弧度     
        double b = Math.toRadians(a);   
          
        // 返回a的三角正弦值     
        System.out.println("Sine value of a is: " +Math.sin(a));    
          
        // 返回a的三角余弦值 
        System.out.println("Cosine value of a is: " +Math.cos(a));  
          
        // 返回a的三角正切值  
        System.out.println("Tangent value of a is: " +Math.tan(a));  
          
        // 返回 a 的三角反正弦值      
        System.out.println("Sine value of a is: " +Math.asin(a));    
          
        // 返回a的三角反余弦值  
        System.out.println("Cosine value of a is: " +Math.acos(a));  
          
        //  返回a的三角反正切值    
        System.out.println("Tangent value of a is: " +Math.atan(a));  
  
        //  返回 a 的双曲正弦值        
        System.out.println("Sine value of a is: " +Math.sinh(a));    
          
        //  返回 a 的双曲余弦值     
        System.out.println("Cosine value of a is: " +Math.cosh(a));  
          
        // 返回 a 的双曲正切值   
        System.out.println("Tangent value of a is: " +Math.tanh(a));  
    }    
}    

输出:

Sine value of a is: -0.9880316240928618
Cosine value of a is: 0.15425144988758405
Tangent value of a is: -6.405331196646276
Sine value of a is: NaN
Cosine value of a is: NaN
Tangent value of a is: 1.5374753309166493
Sine value of a is: 5.343237290762231E12
Cosine value of a is: 5.343237290762231E12
Tangent value of a is: 1.0

java.lang.Math

java.lang.Math类包含各种执行基本数值运算的方法,例如数、立方根和三角函数等。各种 java 数学方法如下:

基本数学方法

方法描述
Math.abs()它将返回给定值的绝对值。
Math.max()它返回两个值中的最大值。
Math.min()它用于返回两个值中的最小值。
Math.round()它用于将十进制数字四舍五入到最接近的值。
Math.sqrt()它用于返回数字的平方根。
Math.cbrt()它用于返回数字的立方根。
Math.pow()它将第一个参数的值返回到第二个参数的幂。
Math.signum()它用于查找给定值的符号。
Math.ceil()它用于查找大于或等于参数或数学整数的最小整数值。
Math.copySign()它用于查找第一个参数的绝对值以及第二个参数中指定的符号。
Math.nextAfter()它用于返回在第二个参数的方向上与第一个参数相邻的浮点数。
Math.nextUp()它返回正无穷大方向上与 d 相邻的浮点值。
Math.nextDown()它返回在负无穷大方向上与 d 相邻的浮点值。
Math.floor()它用于查找小于或等于参数且等于双精度值的数学整数的最大整数值。
Math.floorDiv()它用于查找小于或等于代数商的最大整数值。
Math.random()它返回一个带正号的双精度值,大于或等于 0.0 且小于 1.0。
Math.rint()它返回最接近给定参数且等于数学整数的双精度值。
Math.hypot()它返回 sqrt(x 2 +y 2 ),中间没有溢出或下溢。
Math.ulp()它返回参数的 ulp 的大小。
Math.getExponent()它用于返回用于表示值的无偏指数。
Math.IEEEremainder()它用于计算 IEEE 754 标准规定的两个参数的余数运算并返回值。
Math.addExact()它用于返回其参数的总和,如果结果溢出 int 或 long,则抛出异常。
Math.subtractExact()它返回参数的差异,如果结果溢出 int 则抛出异常。
Math.multiplyExact()它用于返回参数的乘积,如果结果溢出 int 或 long,则抛出异常。
Math.incrementExact()它返回递增 1 的参数,如果结果溢出 int 则抛出异常。
Math.decrementExact()它用于返回减一的参数,如果结果溢出 int 或 long 则抛出异常。
Math.negateExact()它用于返回参数的否定,如果结果溢出 int 或 long 则抛出异常。
Math.toIntExact()它返回 long 参数的值,如果值溢出 int 则抛出异常。

对数数学方法

方法描述
Math.log()它返回双精度值的自然对数。
Math.log10()它用于返回双精度值的以 10 为底的对数。
Math.log1p()它返回参数与 1 之和的自然对数。
Math.exp()它返回 E 的双精度值次方,其中 E 是欧拉数,大约等于 2.71828。
Math.expm1()它用于计算 E 的幂并从中减去 1。

三角数学方法

方法描述
Math.sin()它用于返回给定双精度值的三角正弦值。
Math.cos()它用于返回给定双精度值的三角余弦值。
Math.tan()它用于返回给定双精度值的三角正切值。
Math.asin()它用于返回给定双精度值的三角反正弦值
Math.acos()它用于返回给定双精度值的三角反余弦值。
Math.atan()它用于返回给定双精度值的三角反正切值。

双曲数学方法

方法描述
Math.sinh()它用于返回给定双精度值的三角双曲余弦值。
Math.cosh()它用于返回给定双精度值的三角双曲正弦值。
Math.tanh()它用于返回给定双精度值的三角双曲正切值。

角度数学方法

方法描述
Math.toDegrees它用于将指定的弧度角转换为以度为单位测量的等效角。
Math.toRadians它用于将指定的角度转换为以弧度测量的等效角度。

标签: java, Java面试题, Java下载, java教程, java技术, Java学习, Java学习教程, Java语言, Java开发, Java入门教程, Java进阶教程, Java高级教程, Java笔试题, Java编程思想