java Math类的常用方法介绍

java Math类的常用方法介绍

代码块

public class MainTest {
    public static void main(String[] args) {
        //求sin值
        double sin = Math.sin(3.14);
        System.out.println("sin3.14=" + sin);
        //求cos值
        double cos = Math.cos(0);
        System.out.println("cos0=" + cos);
        //求tan值
        double tan = Math.tan(0.785);
        System.out.println("tan0.785=" + tan);
        //求arcsin
        double arcsin = Math.asin(1);
        System.out.println("arcsin1 = " + arcsin);
        //求arccos
        double arccos = Math.acos(1);
        System.out.println("arccos = " + arccos);
        //求arctan
        double arctan = Math.atan(30);
        System.out.println("arctan30 = " + arctan);
        //求弧度
        double radians = Math.toRadians(180);
        System.out.println("180度角的弧度为" + radians);
        //求角度
        double angle = Math.toDegrees(3.141592653589793);
        System.out.println("π的角度数为" + angle);
        //求以e为底的指数
        double exp = Math.exp(1);
        System.out.println("以e为底指数为1的数为" + exp);
        //求以e为底e的平方的对数
        double log = Math.log(Math.E * Math.E);
        System.out.println("以e为底e的平方的对数" + log);
        //求以10为底100的对数
        double log10 = Math.log10(100);
        System.out.println("以10为底100的对数" + log10);
        //求100的平方根
        double sqrt = Math.sqrt(100);
        System.out.println("100的平方根是" + sqrt);
        //求27的立方根
        double cbrt = Math.cbrt(27);
        System.out.println("27的立方根是" + cbrt);
        //求10除以3的余数
        double rest = Math.IEEEremainder(10, 3);
        System.out.println("10除以3的余数为" + rest);
        //求0.9向上取整
        double ceil = Math.ceil(0.9);
        System.out.println("0.9向上取整" + ceil);
        //求2.49向下取整
        double floor = Math.floor(2.49);
        System.out.println("2.49向下取整" + floor);
        //求最接近参数的整数值(若有两个满足条件的数据则取为偶数的数据)
        double rint = Math.rint(3.5);
        System.out.println("最接近参数的整数值" + rint);
        //获得(1,1)坐标与x轴夹角度数
        double atan2 = Math.atan2(1, 1);
        System.out.println("坐标(1,1)的极坐标为" + atan2);
        //求3的5次方
        double pow = Math.pow(3, 5);
        System.out.println("3的5次方" + pow);
        //4舍5入
        double round = Math.round(3.5);
        System.out.println("3.5四舍五入为" + round);
        //计算2<<5+3<<4(会抛出结果溢出异常)------- 2<<5:2乘以2的5次方 3<<4:3乘以2的4次方
        int sum = Math.addExact(2 << 5, 3 << 4);
        System.out.println("2<<5+3<<4=" + sum);
        //计算2<<5-3<<4(会抛出结果溢出异常)
        int subTract = Math.subtractExact(2 << 5, 3 << 4);
        System.out.println("2<<5-3<<4=" + subTract);
        //计算2<<5*3<<4
        int multiply = Math.multiplyExact(2 << 5, 3 << 4);
        System.out.println("2<<5*3<<4=" + multiply);
        //计算2<<5加1(会跑出溢出异常)
        int increment = Math.incrementExact(2 << 5);
        System.out.println("2<<5+1 = " + increment);
        //计算2<<5加1(会跑出溢出异常)
        int decrementExact = Math.decrementExact(2 << 5);
        System.out.println("2<<5-1 = " + decrementExact);
        //计算2<<5的相反数
        int negate = Math.negateExact(2 << 5);
        System.out.println("2<<5的相反数是" + negate);
        //long转int 2<<15
        int intNum = Math.toIntExact(2 << 15L);
        System.out.println(intNum);

        //2&3 2在2进制中为10 3为11 & 表示相同位都为1则为1,其他为0 10&11 2进制第二位都为1 所以 2&3=10=2

        //multiplyHigh意义未知
        //计算2<<5除以2<<4(取整)
        int floorDiv = Math.floorDiv(2<<5,2<<4);
        System.out.println("2<<5/2<<4="+floorDiv);
        //取模运算(算法与取余相同,但负数运算是计算方式不同)
        int floorMod = Math.floorMod(10,3);
        System.out.println("10/3的模="+floorMod);
        //取-52的绝对值
        int abs = Math.abs(-52);
        System.out.println("-52的绝对值="+abs);
        //比较2<<5和3<<4的大小
        int max = Math.max(2<<5,3<<4);
        System.out.println("2<<5与3<<4相比,较大的数为"+max);
        //比较2<<5和3<<4的大小
        int min = Math.min(2<<5,3<<4);
        System.out.println("2<<5与3<<4相比,较小的数为"+min);
        //计算3乘5加4
        double fma = Math.fma(3,5,4);
        System.out.println("3乘5加4="+fma);
    }
}

执行结果

sin3.14=0.0015926529164868282
cos0=1.0
tan0.785=0.9992039901050427
arcsin1 = 1.5707963267948966
arccos = 0.0
arctan30 = 1.5374753309166493
180度角的弧度为3.141592653589793
π的角度数为180.0
以e为底指数为1的数为2.718281828459045
以e为底e的平方的对数2.010为底100的对数2.0
100的平方根是10.0
27的立方根是3.0
10除以3的余数为1.0
0.9向上取整1.0
2.49向下取整2.0
最接近参数的整数值4.0
坐标(1,1)的极坐标为0.7853981633974483
35次方243.0
3.5四舍五入为4.0
2<<5+3<<4=112
2<<5-3<<4=16
2<<5*3<<4=3072
2<<5+1 = 65
2<<5-1 = 63
2<<5的相反数是-64
65536
2<<5/2<<4=2
10/3的模=1
-52的绝对值=52
2<<53<<4相比,较大的数为64
2<<53<<4相比,较小的数为48
354=19.0
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值