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

浮點運算不能產生精確的結果

Floating point arithmetic not producing exact results(浮點運算不能產生精確的結果)
本文介紹了浮點運算不能產生精確的結果的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要在Java中做一些浮點運算,如下代碼所示:

I need to do some floating point arithmetic in Java as shown in the code below:

public class TestMain {
    private static Map<Integer, Double> ccc = new HashMap<Integer, Double>() {
      { put(1, 0.01); put(2, 0.02); put(3, 0.05); put(4, 0.1); put(6, 0.2);
        put(10, 0.5); put(20, 1.0); put(30, 2.0); put(50, 5.0); put(100, 10.0);
      }
    };

    Double increment(Double i, boolean up) {
        Double inc = null;

        while (inc == null) {
            inc = ccc.get(i.intValue());

            if (up)
                --i;
            else
                ++i;
        }
        return inc;
    }

    public static void main(String[] args) {
        TestMain tt = new TestMain();

        for (double i = 1; i < 1000; i += tt.increment(i, true)) {
            System.out.print(i + ",");
        }
    }
}

這是為了模擬 Betfair 輸出的值范圍微調器小部件.

Java 中的浮點運算似乎引入了一些意想不到的錯誤.例如,我得到 2.180000000000001 而不是 2.18.浮點數有什么用,你不能相信對它們執行的算術結果?我該如何解決這個問題?

Floating point arithmetic in Java seems to introduce some unexpected errors. For example, I get 2.180000000000001 instead of 2.18. What use are floating point numbers is you can't trust the results of arithmetic performed on them? How can I get around this issue?

推薦答案

如果你需要精確的十進制值,你應該使用java.math.BigDecimal.然后閱讀每個計算機科學家應該了解的浮點運算知識"了解您獲得這些結果的背景.

If you need exact decimal values, you should use java.math.BigDecimal. Then read "What Every Computer Scientist Should Know About Floating-Point Arithmetic" for the background of why you're getting those results.

(我有一篇 .NET 中心文章,您可能會發現它更容易閱讀- 當然更短.Java 和 .NET 之間的差異對于理解這個問題來說幾乎是無關緊要的.)

(I have a .NET-centric article which you may find easier to read - and certainly shorter. The differences between Java and .NET are mostly irrelevant for the purposes of understanding this issue.)

這篇關于浮點運算不能產生精確的結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产成人精品网站 | 天天躁日日躁性色aⅴ电影 免费在线观看成年人视频 国产欧美精品 | 97精品国产一区二区三区 | 日韩在线小视频 | 久久精品99国产精品日本 | 国产精品www | 亚洲在线一区二区 | 天天干天天操天天看 | 日韩中文字幕在线不卡 | 99久久免费精品 | 天天影视网天天综合色在线播放 | 粉色午夜视频 | 欧美自拍另类 | 99视频免费看 | 国产在线二区 | 精品一区国产 | 五月天综合影院 | 三级av在线 | 国产精品视频一二三区 | 亚洲精品在线观看视频 | 国产三区视频在线观看 | 黄网站色大毛片 | 国产在线一区二区三区 | 精品入口麻豆88视频 | 成人美女免费网站视频 | 伊人免费网 | 久久av一区| 中文字幕免费在线 | 国产免费色 | 亚洲综合视频一区 | 国产视频第一页 | 国产精品毛片 | 日韩欧美在线精品 | 国产91色在线 | 亚洲 | 精品一区二区三区在线观看国产 | 精彩视频一区二区三区 | 黄色毛片网站在线观看 | 国产精品久久久久久婷婷天堂 | 日韩成人免费 | 久久在线精品 | 成人免费观看男女羞羞视频 |