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

Java:使用雙精度不準(zhǔn)確

Java: Inaccuracy using double(Java:使用雙精度不準(zhǔn)確)
本文介紹了Java:使用雙精度不準(zhǔn)確的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

可能重復(fù):
在java中使用雙精度保持精度
Java 程序中奇怪的浮點(diǎn)行為

我正在制作一個(gè)直方圖類,但遇到了一個(gè)奇怪的問(wèn)題.

I'm making a histogram class, and I'm encountering a weird issue.

這是該類的基礎(chǔ)知識(shí),還有更多方法,但與問(wèn)題無(wú)關(guān).

Here are the basics of the class, there are more methods but they aren't relevant to the issue.

private int[] counters;
private int numCounters;
private double min, max, width;

public Histogram(double botRange, double topRange, int numCounters) {
    counters = new int[numCounters];
    this.numCounters = numCounters;
    min = botRange;
    max = topRange;
    width = (max - min) / (double) numCounters;
}

public void plotFrequency() {
    for (int i = 0; i < counters.length; i++) {
        writeLimit(i * width, (i + 1) * width);
        System.out.println(counters[i]);
    }
}

private void writeLimit(double start, double end) {
    System.out.print(start + " <= x < " + end + "		");
}

當(dāng)我繪制頻率時(shí)會(huì)出現(xiàn)問(wèn)題.我創(chuàng)建了 2 個(gè)實(shí)例.新直方圖(0, 1, 10);新直方圖(0, 10, 10);

the problem happens when I plot the frequencies. I've created 2 instances. new Histogram(0, 1, 10); new Histogram(0, 10, 10);

這是他們輸出的內(nèi)容.

Frequecy
0.0 <= x < 0.1      989
0.1 <= x < 0.2      1008
0.2 <= x < 0.30000000000000004      1007
0.30000000000000004 <= x < 0.4      1044
0.4 <= x < 0.5      981
0.5 <= x < 0.6000000000000001       997
0.6000000000000001 <= x < 0.7000000000000001        1005
0.7000000000000001 <= x < 0.8       988
0.8 <= x < 0.9      1003
0.9 <= x < 1.0      978

Frequecy
0.0 <= x < 1.0      990
1.0 <= x < 2.0      967
2.0 <= x < 3.0      1076
3.0 <= x < 4.0      1048
4.0 <= x < 5.0      971
5.0 <= x < 6.0      973
6.0 <= x < 7.0      1002
7.0 <= x < 8.0      988
8.0 <= x < 9.0      1003
9.0 <= x < 10.0     982    

所以我的問(wèn)題是,為什么我在第一個(gè)示例中得到了非常長(zhǎng)的小數(shù)限制,而在第二個(gè)示例中沒(méi)有?

So my question is, why am I getting the really long decimal limits in the first example, but not the second one?

推薦答案

有些小數(shù)不能用雙精度值精確表示.0.3 是這些值之一.

Some decimals cannot be exactly represented by double values. 0.3 is one of those values.

所有小于某個(gè)數(shù)字(我忘記了)的整數(shù)值恰好有一個(gè)雙精度值的精確表示,所以你看不到近似值.

All integer values less than a certain number (I forget which) happen to have an exact representation by a double value, so you don't see the approximation.

考慮我們?nèi)绾慰创龜?shù)字:數(shù)字 123 表示為 (1 * 100) + (2 * 10) + (3 * 1).我們使用 10 作為我們的基礎(chǔ).二進(jìn)制數(shù)使用兩個(gè).因此,當(dāng)您查看數(shù)字的分?jǐn)?shù)時(shí),如何通過(guò)添加 2 的單個(gè)冪來(lái)表示 0.3?你不能.你能想到的最好的值大約是 0.30000000000000004(我必須查看確切的二進(jìn)制數(shù)字才能知道它是如何達(dá)到的).

Consider how we think of numbers: the number 123 is represented as (1 * 100) + (2 * 10) + (3 * 1). We use 10 as our base. Binary numbers use two. So when you look at fractions of a number, how could you represent 0.3 by adding individual powers of 2? You can't. The best you can come up with is about 0.30000000000000004 (I'd have to see the exact binary digits to see how it reaches that).

這篇關(guān)于Java:使用雙精度不準(zhǔn)確的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 久久无毛 | 日韩精品免费播放 | 久在线 | 伊人伊人伊人 | av永久免费 | 99视频| 国产激情偷乱视频一区二区三区 | 毛片国产 | 精品国产一区二区三区免费 | 国产精品国产精品国产专区不卡 | 日韩精品在线一区 | 神马久久久久久久久久 | 求毛片 | 美女爽到呻吟久久久久 | 成人网av | 国产成人精品久久久 | 亚洲精品自在在线观看 | 亚洲精品电影网在线观看 | 欧洲毛片 | 国产精品99久久久精品免费观看 | 亚洲最大的成人网 | 精品国产乱码久久久久久蜜退臀 | 日韩中文一区二区三区 | 99精品一区二区 | 久久亚洲一区二区三区四区 | 三级av网址 | 亚洲国产成人av好男人在线观看 | 国产精品夜夜夜一区二区三区尤 | 日韩天堂av | 亚洲一区二区三区视频 | 九九热这里只有精品6 | 久久人操| 日本天天操 | 亚洲精品一区中文字幕乱码 | www.国产一区 | 精品久久久久久久 | 九九99久久| 国产精品成人在线播放 | 亚洲国产精品一区在线观看 | 久草网在线视频 | 久久国产精99精产国高潮 |