JAVA中Math类

Math

基本介绍:Math类包含用于执行基本数学运算的方法,如初等指数,对数,平方根和三角函数,并且Math所有的方法都是静态的。

Math类常见方法
abs    		绝对值
pow    		求幂
ceil   		向上取整
floor		向下取整
round		四舍五入
sqrt		求开方
max			求两个数的最大值
min			求两个数的最小值
random		求随机数
请写出获取 a - b之间的一个随机整数 , a,b均为整数
int num = (int)(a + Math.random()*(b - a + 1));
Math类常见方法源码
绝对值
   public static int abs(int a) {
        return (a < 0) ? -a : a;
    }
    
求幂
    public static double pow(double a, double b) {
        return StrictMath.pow(a, b); // default impl. delegates to StrictMath
    }
    
向上取整
    public static double ceil(double a) {
        return StrictMath.ceil(a); // default impl. delegates to StrictMath
    }
    
    
向下取整
    public static double floor(double a) {
        return StrictMath.floor(a); // default impl. delegates to StrictMath
    }
    
四舍五入
    public static long round(double a) {
        long longBits = Double.doubleToRawLongBits(a);
        long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
                >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
        long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
                + DoubleConsts.EXP_BIAS) - biasedExp;
        if ((shift & -64) == 0) { // shift >= 0 && shift < 64
            // a is a finite number such that pow(2,-64) <= ulp(a) < 1
            long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
                    | (DoubleConsts.SIGNIF_BIT_MASK + 1));
            if (longBits < 0) {
                r = -r;
            }
            // In the comments below each Java expression evaluates to the value
            // the corresponding mathematical expression:
            // (r) evaluates to a / ulp(a)
            // (r >> shift) evaluates to floor(a * 2)
            // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
            // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
            return ((r >> shift) + 1) >> 1;
        } else {
            // a is either
            // - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
            // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
            // - an infinity or NaN
            return (long) a;
        }
    }


求开方
    public static double sqrt(double a) {
        return StrictMath.sqrt(a); // default impl. delegates to StrictMath
                                   // Note that hardware sqrt instructions
                                   // frequently can be directly used by JITs
                                   // and should be much faster than doing
                                   // Math.sqrt in software.
    }
    

最大值
    public static int max(int a, int b) {
        return (a >= b) ? a : b;
    }

最小值
    public static int min(int a, int b) {
        return (a <= b) ? a : b;
    }


随机数
    public static double random() {
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
    }

案例代码

package wrapper_.math;

/**
 * @author: 海康
 * @version: 1.0
 */
public class Math01 {
    public static void main(String[] args) {
        //绝对值
        int abs = Math.abs(-9);
        System.out.println(abs);

        //求幂
        double pow = Math.pow(2, 3);
        System.out.println(pow);

        //向上取整 ceil[天花板]
        // ceil 向上取整 返回 >= 该参数的最大整数
        double ceil = Math.ceil(5.3);
        System.out.println(ceil);

        //向下取整 floor[地板]
        // floor 向下取整 返回 <= 该参数的最小整数
        double floor = Math.floor(8.6);
        System.out.println(floor);

        //round 四舍五入
        long round = Math.round(7.6);
        System.out.println(round);

        //sqrt 求开方
        double sqrt = Math.sqrt(9.0);
        System.out.println(sqrt);

        //max 最大值
        int max = Math.max(168, 88168);
        System.out.println(max);

        //min 最小值
        double min = Math.min(9.6, 8.8);
        System.out.println(min);

        /**
         * random :求随机数
         * random 返回的是 0 <= x <1 之间的一个随机小数
         * 思考:请写出获取 a - b 之间的一个随机整数 a b 均为整数,比如: a = 2 b = 7
         */
        for (int i = 0; i < 100; i++) {
//            double random = Math.floor(Math.random() * 100) + 1;//取 1 - 100 之间的整数 【包含100】
            int num = (int) (2 + Math.random() * (7 - 2) + 1); // 取 2 - 7 之间的整数 【包含 2 和 7】
            System.out.println(num);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值