Math类

public static void main(String args[]) {
        System.out.println(Math.E);// 比任何其他值都更接近 e(即自然对数的底数)的 double 值(2.7182818284590452354)。
        System.out.println(Math.PI);// 比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值(3.14159265358979323846)。

        //三角函数运算
        System.out.println(Math.sin(30)); // 求正弦 输出0.5
        System.out.println(Math.cos(60)); // 求余弦 输出0.5
        System.out.println(Math.tan(45)); // 求正切 输出1
        System.out.println(Math.asin(0.5)); // 求反正弦 输出30°
        System.out.println(Math.acos(0.5)); // 求反余弦 输出60°
        System.out.println(Math.atan(1));   // 求反正切 输出45°
        System.out.println(Math.atan2(1.0, 1.0)); // atan2(y,x)求向量(x,y)与x轴夹角(坐标轴中指定点与极坐标夹角) 输出 45°
        System.out.println(Math.sinh(1)); // 求双曲正弦值
        System.out.println(Math.cosh(1)); // 求双曲余弦值
        System.out.println(Math.tanh(1)); // 求双曲正切值

        // 弧度角度转换
        System.out.println(Math.toRadians(180));//角度换弧度 输出 π
        System.out.println(Math.toDegrees(Math.PI));//弧度换角度 输出180

        // 取整运算
        System.out.println(Math.ceil(1.3443)); // 向上取整 输出2.0
        System.out.println(Math.floor(1.3443)); // 向下取整 输出1.0
        System.out.println(Math.round(1.8));// 四舍五入取整 输出2.0
        System.out.println(Math.round(1.2));// 四舍五入取整 输出1.0
        System.out.println(Math.rint(1.2));// 返回最接近a的整数的double值 输出1.0
        System.out.println(Math.rint(1.8));// 返回最接近a的整数的double值 输出2.0

        // 算数运算
        System.out.println(Math.addExact(2,3)); // 求和 输出5
        System.out.println(Math.subtractExact(3,2)); // 求差 输出1
        System.out.println(Math.multiplyExact(2,3)); // 求乘积 输出6
        System.out.println(Math.incrementExact(2)); // +1 输出3
        System.out.println(Math.decrementExact(2)); // -1 输出1
        System.out.println(Math.toIntExact(2L)); // long转int 输出2
        System.out.println(Math.floorDiv(4, 3)); // 计算小于或等于商的最大整数值 输出1
        System.out.println(Math.floorMod(4, 3)); // 计算两参数相除的余数 4除以3 输出1
        //按照 IEEE 754 标准的规定,对两个参数进行余数运算,余数的算术值等于 x−y×n
        //其中 n 为最接近 x/y的商的整数
        //如果两个整数同样接近,则选其中的偶数
        //如果余数为 0,那么它的符号与第一个参数的符号相同
        System.out.println(Math.IEEEremainder(105, 10)); // 5.0
        System.out.println(Math.IEEEremainder(106, 10)); // -4.0

        // 幂函数(注意可用数据类型只有double型)
        System.out.println(Math.exp(2));// 输出E^2的值
        System.out.println(Math.pow(2.0, 3.0));// pow(x,y) 返回x^y 输出8.0
        System.out.println(Math.scalb(2, 3));// 计算x乘以2的y次幂, 2 * 2^3 = 2 * 8 = 16

        // 开根号
        System.out.println(Math.sqrt(4.0));// 开平方 输出2.0
        System.out.println(Math.cbrt(8.0));// 开立方 输出2.0
        System.out.println(Math.hypot(3.0, 4.0));// hypot(x,y)求2个参数平方和的平方根,输出5.0

        // 对数 只能传double型数据
        System.out.println(Math.log(Math.E)); // 计算以 e 为底的对数 (自然对数),ln X 输出1.0
        System.out.println(Math.log10(10));// 计算以 10 为底的对数,lg X 输出1.0
        System.out.println(Math.log1p(Math.E-1.0));// 计算以 e 为底,1 与参数之和为指数的对数 ln(1+X) 输出1.0
        System.out.println(Math.expm1(1)); // 计算 e 的 x 次幂 - 1 lnX - 1

        // 大小运算
        System.out.println(Math.max(1, 2));// 求最大值 输出2
        System.out.println(Math.min(1.9, -0.2));//求最小值 输出-0.2
        System.out.println(Math.nextUp(1.2)); // 返回比a大一点点的浮点数 输出1.2000000000000002
        System.out.println(Math.nextDown(1.2));// nextDown(a) 返回比a小一点点的浮点数 输出1.1999999999999997
        System.out.println(Math.nextAfter(1.2, 2.7));// 返回(a,b)或(b,a)间与a相邻的浮点数 b可以比a小 输出1.2000000000000002
        System.out.println(Math.nextAfter(1.2, -1));//返回(a,b)或(b,a)间与a相邻的浮点数 b可以比a小 输出1.1999999999999997

        // 符号运算
        System.out.println(Math.abs(-10));// 求绝对值 输出10
        System.out.println(Math.negateExact(2)); // 求相反数 输出-2
        // 如果参数等于0.0 返回0.0,大于0.0则返回1.0,小于0.0则返回-1.0
        System.out.println(Math.signum(0.1));// 输出1.0
        System.out.println(Math.signum(-0.1));// 输出-1.0
        System.out.println(Math.signum(0.0));// 输出0.0
        System.out.println(Math.copySign(-1.0, 2.0));// 返回 用y的符号取代x的符号后新的x值 输出1.0

        // 其他
        System.out.println(Math.ulp(0.1));// 获取参数的 ulp 值,ulp 值是该浮点值与下一个数值较大的浮点值之间的正距离 1.3877787807814457E-17
        System.out.println(Math.getExponent(0.1));// 获取在表示浮点数时使用的无偏指数 1.3877787807814457E-17

        /*
         * 7.随机数
         * random()返回[0.0,1.0)之间的double值
         * 这个产生的随机数其实可以通过*x控制
         * 比如(int)(random*100)后可以得到[0,100)之间的整数
         */
        System.out.println((int)(Math.random()*100));//输出[0,100)间的随机数
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值