java 中的 Math.round(-1.5) 等于多少?

导读:本篇文章讲解 java 中的 Math.round(-1.5) 等于多少?,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

这道题目的答案是 -1.

其实这道题目考察的主要知识点就是:Math.round方法的概念以及返回值类型。

注意:Math的round方法是四舍五入,如果参数是负数,则往大的数如,Math.round(-1.5)=-1,如果是Math.round(1.5)则结果为2

 

从源码及注释上可以看出:

1.如果参数大于 Long.MAX_VALUE 则返回Long.MAX_VALUE

2.如果参数小于Long.MIN_VALUE 则返回Long.MIN_VALUE

3.如果参数为NaN则返回0

4.其余值则返回接近于当前参数的最大整数值。所以Math.round(-1.5)返回的是最接近的最大整数-1。

下面是源码:

   /**
     * Returns the closest {@code long} to the argument, with ties
     * rounding to positive infinity.
     *
     * <p>Special cases:
     * <ul><li>If the argument is NaN, the result is 0.
     * <li>If the argument is negative infinity or any value less than or
     * equal to the value of {@code Long.MIN_VALUE}, the result is
     * equal to the value of {@code Long.MIN_VALUE}.
     * <li>If the argument is positive infinity or any value greater than or
     * equal to the value of {@code Long.MAX_VALUE}, the result is
     * equal to the value of {@code Long.MAX_VALUE}.</ul>
     *
     * @param   a   a floating-point value to be rounded to a
     *          {@code long}.
     * @return  the value of the argument rounded to the nearest
     *          {@code long} value.
     * @see     java.lang.Long#MAX_VALUE
     * @see     java.lang.Long#MIN_VALUE
     */
    public static long round(double a) {
        //方法返回根据IEEE754浮点“双精度格式”位布局,不是非数字(NaN)值,返回指定浮点值的表示。
        long longBits = Double.doubleToRawLongBits(a);
        //a=-1.6d 时 -4613487458278336102


        long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
                >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
        //biasedExp =1023


        long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2+ DoubleConsts.EXP_BIAS) - biasedExp;
        //shift=51

        // shift >= 0 && shift < 64
        if ((shift & -64) == 0) { 
            // 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;
        }
    }

 

下面是代码执行的结果:

java 中的 Math.round(-1.5) 等于多少?

java 中的 Math.round(-1.5) 等于多少?

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/97074.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!