Math类的数学计算功能

//Math类的数学计算功能
public class MathTest {
    public static void main(String[] args) {
        /*----------下面是三角运算----------*/
        //将弧度转换成角度
        System.out.println("Math.toDegrees(1.57):" + Math.toDegrees(1.57));
        //将角度转换为弧度
        System.out.println("Math.toRadians(90):" + Math.toRadians(90));
        //计算反余弦,返回的角度范围在0.0到pi之间
        System.out.println("Math.acos(1.2):" + Math.acos(1.2));
        /*----------下面是整数运算----------*/
        //取整,返回小于目标数的最大整数(向下取整)
        System.out.println("Math.floor(-1.2):" + Math.floor(-1.2));
        //取整,返回大于目标数的最小整数(向上取整)
        System.out.println("Math.ceil(1.2)" + Math.ceil(1.2));
        //四舍五入
        System.out.println("Math.round(2.3):" + Math.round(2.3));
        /*----------下面是乘方、开方、指数运算----------*/
        //计算平方根
        System.out.println("Math.sqrt(2.3):" + Math.sqrt(2.3));
        //计算立方根
        System.out.println("Math.cbrt(9):" + Math.cbrt(9));
        //返回欧拉数e的n次幂
        System.out.println("Math.exp(2):" + Math.exp(2));
        //返回sqrt(x²+y²),中间没有溢出或下溢
        System.out.println("Math.hypot(4, 4):" + Math.hypot(4, 4));
        //计算乘方
        System.out.println("Math.pow(3, 2):" + Math.pow(3, 2));
        //计算自然对数
        System.out.println("Math.log(12):" + Math.log(12));
        //计算底数为10的对数
        System.out.println("Math.log10(9):" + Math.log10(9));
        //返回参数与1之和的自然对数
        System.out.println("Math.log1p(9):" + Math.log1p(9));
        /*----------下面是符号相关的运算----------*/
        //计算绝对值
        System.out.println("Math.abs(-4.5):" + Math.abs(-4.5));
        //符号赋值,返回带有第二个浮点数符号的第一个浮点参数
        System.out.println("Math.copySign(1.2, -1.0):" + Math.copySign(1.2, -1.0));
        //符号函数,如果参数为0,则返回0;如果参数大于0,则返回1.0;如果参数小于0,则返回-1.0
        System.out.println("Math.signum(2.3):" + Math.signum(2.3));
        /*----------下面是大小相关的运算----------*/
        //找出最大值
        System.out.println("Math.max(2.3, 4.5):" + Math.max(2.3, 4.5));
        //计算最小值
        System.out.println("Math.min(2.3, 4.5):" + Math.min(2.3, 4.5));
        //返回第一个参数和第二个参数之间与第一个参数相邻的浮点数
        System.out.println("Math.nextAfter(1.2, 1.0):" + Math.nextAfter(1.2, 1.0));
        //返回比目标数略大的浮点数
        System.out.println("Math.nextUp(1.2):" + Math.nextUp(1.2));
        //返回一个伪随机数,该值大于等于0且小于1.0
        System.out.println("Math.random():" + Math.random());
    }
}

 

posted @ 2018-07-25 14:13  王甘林  阅读(679)  评论(0编辑  收藏  举报