个性化阅读
专注于IT技术分析

Java Math类方法

点击下载

本文概要

Java的数学类提供了一些方法来工作的数学计算像MIN(),MAX(),AVG(),sin(),COS(),floor(),ABS( )等。

不像一些StrictMath类数字方法,数学类的同等功能的所有实现不能定义返回位对位相同的结果。这个松弛允许与在不要求严格可重复性更好的性能实现。

如果尺寸是int或长,结果溢出的值的范围内,该方法addExact(),subtractExact(),multiplyExact(),和toIntExact()抛出ArithmeticException。

对于像加,减,除,绝对值和否定溢出等算术运算与特定的最低或最高值才会发生。它应与最大和最小值适当进行检查。

实例1

public class JavaMathExample1  
{  
    public static void main(String[] args)   
    {  
        double x = 28;  
        double y = 4;  
        
        // return the maximum of two numbers
        System.out.println("Maximum number of x and y is: " +Math.max(x,y)); 
        
        // return the square root of y 
        System.out.println("Square root of y is: " + Math.sqrt(y)); 
        
        //returns 28 power of 4 i.e. 28*28*28*28  
        System.out.println("Power of x and y is: " + Math.pow(x,y));    

        // return the logarithm of given value     
        System.out.println("Logarithm of x is: " + Math.log(x)); 
        System.out.println("Logarithm of y is: " + Math.log(y));
        
        // return the logarithm of given value when base is 10    
        System.out.println("log10 of x is: " + Math.log10(x)); 
        System.out.println("log10 of y is: " + Math.log10(y));  
        
        // return the log of x + 1
        System.out.println("log1p of x is: " +Math.log1p(x));  

        // return a power of 2  
        System.out.println("exp of a is: " +Math.exp(x));  
        
        // return (a power of 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;  
        
        // converting values to radian  
        double b = Math.toRadians(a); 
        
        // return the trigonometric sine of a    
        System.out.println("Sine value of a is: " +Math.sin(a));  
        
        // return the trigonometric cosine value of a
        System.out.println("Cosine value of a is: " +Math.cos(a));
        
        // return the trigonometric tangent value of a
        System.out.println("Tangent value of a is: " +Math.tan(a));
        
        // return the trigonometric arc sine of a    
        System.out.println("Sine value of a is: " +Math.asin(a));  
        
        // return the trigonometric arc cosine value of a
        System.out.println("Cosine value of a is: " +Math.acos(a));
        
        // return the trigonometric arc tangent value of a
        System.out.println("Tangent value of a is: " +Math.atan(a));

        // return the hyperbolic sine of a    
        System.out.println("Sine value of a is: " +Math.sinh(a));  
        
        // return the hyperbolic cosine value of a
        System.out.println("Cosine value of a is: " +Math.cosh(a));
        
        // return the hyperbolic tangent value of 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的数学方法

该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),没有中间溢出或下溢。
Math.ulp()它返回参数的ulp的大小。
Math.getExponent()它用于返回用于表示值的无偏指数。
Math.IEEEremainder()它被用来计算上规定由IEEE 754标准,并返回值的两个参数的余数操作。
Math.addExact()它用于返回其参数的和,如果结果溢出int或long,则抛出异常。
Math.subtractExact()它返回参数的差异,如果结果溢出一个int,则抛出异常。
Math.multiplyExact()它是用来返回的参数的产品,抛出一个异常,如果结果溢出int或长。
Math.incrementExact()它返回递增1的参数,如果结果溢出一个int,则抛出异常。
Math.decrementExact()它用于返回递减1的参数,如果结果溢出int或long,则抛出异常。
Math.negateExact()它是用来返回参数的否定,抛出一个异常,如果结果溢出int或长。
Math.toIntExact()它返回长参数的值,如果该值溢出一个int,则抛出异常。

对数数学方法

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

三角函数数学方法

方法描述
Math.sin()它用于返回给定双值的三角正弦值。
Math.cos()It is used to return the trigonometric Cosine value of a Given double value.
Math.tan()它是用来返回给定double值的三角正切值。
Math.asin()它用于返回给定双值的三角反正弦值
Math.acos()它用于返回给定双值的三角反余弦值。
Math.atan()它是用来返回给定double值的三角反正切值。

双曲数学方法

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

角数学方法

方法描述
Math.toDegrees它用于将指定的弧度角转换为以度数表示的等效角度。
Math.toRadians它用于将指定的角度转换为以弧度度量的等效角度。
赞(0)
未经允许不得转载:srcmini » Java Math类方法

评论 抢沙发

评论前必须登录!