久久久久久久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.欧美.com | 欧美日本免费 | 国产黄色在线观看 | 亚洲国产日韩一区 | 视频一区二区在线观看 | 亚洲91精品| 成人精品一区亚洲午夜久久久 | 日韩快播电影 | 亚洲国产一区二区在线 | 日韩免费在线 | 国产91 在线播放 | 久久久人 | 国产久 | 国产小视频自拍 | 男人天堂国产 | 日本久久一区 | 国产一区免费 | 黄色大片在线播放 | 国产欧美精品在线 | 国产精品久久国产精品 | 国内久久 | 国产一区二区小视频 | 日本精品久久 | 看一级毛片| 日韩欧美在线一区二区 | 国产成人精品福利 | 中文字幕亚洲精品 | 国产精品视频偷伦精品视频 | www.久久.com | 国产特一级黄色片 | 天堂一区在线观看 | 国产精品麻 | 久久精品日产第一区二区三区 | 亚洲一区 中文字幕 | 中文字幕一区在线观看视频 | 国产一区二区三区 | 一级大片网站 | 久久99精品国产麻豆婷婷 | 午夜精品久久久久久久 |