久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么 Math.round(0.49999999999999994) 返回 1?

Why does Math.round(0.49999999999999994) return 1?(為什么 Math.round(0.49999999999999994) 返回 1?)
本文介紹了為什么 Math.round(0.49999999999999994) 返回 1?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在下面的程序中你可以看到每個略小于.5的值都被四舍五入了,除了0.5.

In the following program you can see that each value slightly less than .5 is rounded down, except for 0.5.

for (int i = 10; i >= 0; i--) {
    long l = Double.doubleToLongBits(i + 0.5);
    double x;
    do {
        x = Double.longBitsToDouble(l);
        System.out.println(x + " rounded is " + Math.round(x));
        l--;
    } while (Math.round(x) > i);
}

打印

10.5 rounded is 11
10.499999999999998 rounded is 10
9.5 rounded is 10
9.499999999999998 rounded is 9
8.5 rounded is 9
8.499999999999998 rounded is 8
7.5 rounded is 8
7.499999999999999 rounded is 7
6.5 rounded is 7
6.499999999999999 rounded is 6
5.5 rounded is 6
5.499999999999999 rounded is 5
4.5 rounded is 5
4.499999999999999 rounded is 4
3.5 rounded is 4
3.4999999999999996 rounded is 3
2.5 rounded is 3
2.4999999999999996 rounded is 2
1.5 rounded is 2
1.4999999999999998 rounded is 1
0.5 rounded is 1
0.49999999999999994 rounded is 1
0.4999999999999999 rounded is 0

我正在使用 Java 6 更新 31.

I am using Java 6 update 31.

推薦答案

總結

在 Java 6(可能更早)中,round(x) 被實現為 floor(x+0.5).1 這是一個規范錯誤,正是針對這種病態的情況.2Java 7 不再強制執行這種損壞的實現.3

In Java 6 (and presumably earlier), round(x) is implemented as floor(x+0.5).1 This is a specification bug, for precisely this one pathological case.2 Java 7 no longer mandates this broken implementation.3

問題

0.5+0.49999999999999994 在雙精度中正好是 1:

0.5+0.49999999999999994 is exactly 1 in double precision:

static void print(double d) {
    System.out.printf("%016x
", Double.doubleToLongBits(d));
}

public static void main(String args[]) {
    double a = 0.5;
    double b = 0.49999999999999994;

    print(a);      // 3fe0000000000000
    print(b);      // 3fdfffffffffffff
    print(a+b);    // 3ff0000000000000
    print(1.0);    // 3ff0000000000000
}

這是因為 0.49999999999999994 的指數小于 0.5,所以當它們相加時,它的尾數會移動,ULP 會變大.

This is because 0.49999999999999994 has a smaller exponent than 0.5, so when they're added, its mantissa is shifted, and the ULP gets bigger.

解決方案

從 Java 7 開始,OpenJDK(例如)這樣實現它:4

Since Java 7, OpenJDK (for example) implements it thus:4

public static long round(double a) {
    if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
        return (long)floor(a + 0.5d);
    else
        return 0;
}

<小時>

<子>1. http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#round%28double%29

<子>2. http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6430675(感謝@SimonNickerson 找到這個)

2. http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6430675 (credits to @SimonNickerson for finding this)

<子>3. http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round%28double%29

<子>4. http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/lang/Math.java#Math.round%28double%29

這篇關于為什么 Math.round(0.49999999999999994) 返回 1?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 成人在线一区二区 | www.色53色.com | 亚州av在线| 欧美精品黄| 亚洲男人天堂 | 亚洲精品在线视频 | www.日本在线播放 | 日韩欧美一区在线 | 午夜在线精品 | 91伊人| 91极品尤物在线播放国产 | 亚洲欧美日韩电影 | 青青久草 | 成年人国产在线观看 | 夜夜操天天艹 | 成人精品视频 | 在线免费观看黄网 | 免费看一区二区三区 | 日本在线中文 | 久久久人成影片免费观看 | 麻豆亚洲 | 久青草影院| 国产一区二区三区不卡av | 国产精品久久久久久久久久不蜜臀 | 欧美久久一级 | 天天综合久久 | 亚洲国产成人在线观看 | 日韩久久精品视频 | 一区二区三区在线观看视频 | 日日天天 | 黄网站涩免费蜜桃网站 | 日本一区二区电影 | 久久成人精品 | 人人爽人人草 | 久久免费精品视频 | 日韩aⅴ视频 | 中文字幕视频一区 | 在线观看一区 | 成人精品一区二区三区四区 | 久久亚洲综合 | 日本精品视频 |