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

為什么多次加0.1仍然無損?

Why does adding 0.1 multiple times remain lossless?(為什么多次加0.1仍然無損?)
本文介紹了為什么多次加0.1仍然無損?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我知道 0.1 十進(jìn)制數(shù)不能用有限的二進(jìn)制數(shù)精確表示(explanation),所以 double n = 0.1 會(huì)損失一些精度,不會(huì)完全是 0.1.另一方面,0.5 可以精確表示,因?yàn)樗?0.5 = 1/2 = 0.1b.

I know the 0.1 decimal number cannot be represented exactly with a finite binary number (explanation), so double n = 0.1 will lose some precision and will not be exactly 0.1. On the other hand 0.5 can be represented exactly because it is 0.5 = 1/2 = 0.1b.

話雖如此,添加 0.1 三次 不會(huì)給出完全正確的 0.3 是可以理解的,所以下面的代碼打印出 false:

Having said that it is understandable that adding 0.1 three times will not give exactly 0.3 so the following code prints false:

double sum = 0, d = 0.1;
for (int i = 0; i < 3; i++)
    sum += d;
System.out.println(sum == 0.3); // Prints false, OK

但是,如何添加 0.1 五次 會(huì)恰好得到 0.5?以下代碼打印 true:

But then how is it that adding 0.1 five times will give exactly 0.5? The following code prints true:

double sum = 0, d = 0.1;
for (int i = 0; i < 5; i++)
    sum += d;
System.out.println(sum == 0.5); // Prints true, WHY?

如果0.1不能準(zhǔn)確表示,怎么加5次就正好0.5可以準(zhǔn)確表示?

If 0.1 cannot be represented exactly, how is it that adding it 5 times gives exactly 0.5 which can be represented precisely?

推薦答案

舍入誤差不是隨機(jī)的,它的實(shí)現(xiàn)方式試圖最小化誤差.這意味著有時(shí)錯(cuò)誤是不可見的,或者沒有錯(cuò)誤.

The rounding error is not random and the way it is implemented it attempts to minimise the error. This means that sometimes the error is not visible, or there is not error.

例如 0.1 不完全是 0.1new BigDecimal("0.1") <new BigDecimal(0.1)0.5 正是 1.0/2

For example 0.1 is not exactly 0.1 i.e. new BigDecimal("0.1") < new BigDecimal(0.1) but 0.5 is exactly 1.0/2

這個(gè)程序向您展示了所涉及的真正價(jià)值.

This program shows you the true values involved.

BigDecimal _0_1 = new BigDecimal(0.1);
BigDecimal x = _0_1;
for(int i = 1; i <= 10; i ++) {
    System.out.println(i+" x 0.1 is "+x+", as double "+x.doubleValue());
    x = x.add(_0_1);
}

打印

0.1000000000000000055511151231257827021181583404541015625, as double 0.1
0.2000000000000000111022302462515654042363166809082031250, as double 0.2
0.3000000000000000166533453693773481063544750213623046875, as double 0.30000000000000004
0.4000000000000000222044604925031308084726333618164062500, as double 0.4
0.5000000000000000277555756156289135105907917022705078125, as double 0.5
0.6000000000000000333066907387546962127089500427246093750, as double 0.6000000000000001
0.7000000000000000388578058618804789148271083831787109375, as double 0.7000000000000001
0.8000000000000000444089209850062616169452667236328125000, as double 0.8
0.9000000000000000499600361081320443190634250640869140625, as double 0.9
1.0000000000000000555111512312578270211815834045410156250, as double 1.0

注意:0.3 稍微偏離了一點(diǎn),但是當(dāng)您到達(dá) 0.4 時(shí),位必須向下移動(dòng)一位以適應(yīng) 53 位限制,并且錯(cuò)誤是丟棄.同樣,0.60.7 的錯(cuò)誤再次出現(xiàn),但對于 0.81.0,錯(cuò)誤被丟棄.

Note: that 0.3 is slightly off, but when you get to 0.4 the bits have to shift down one to fit into the 53-bit limit and the error is discarded. Again, an error creeps back in for 0.6 and 0.7 but for 0.8 to 1.0 the error is discarded.

添加 5 次應(yīng)該會(huì)累積錯(cuò)誤,而不是取消它.

Adding it 5 times should cumulate the error, not cancel it.

出現(xiàn)錯(cuò)誤的原因是精度有限.即53位.這意味著隨著數(shù)字變大使用更多位,必須從末尾丟棄位.這會(huì)導(dǎo)致舍入,在這種情況下對您有利.
當(dāng)獲得較小的數(shù)字時(shí),您可以獲得相反的效果,例如0.1-0.0999 => 1.0000000000000286E-4你會(huì)看到比以前更多的錯(cuò)誤.

The reason there is an error is due to limited precision. i.e 53-bits. This means that as the number uses more bits as it get larger, bits have to be dropped off the end. This causes rounding which in this case is in your favour.
You can get the opposite effect when getting a smaller number e.g. 0.1-0.0999 => 1.0000000000000286E-4 and you see more error than before.

這方面的一個(gè)例子是為什么在 Java 6 為什么 Math.round(0.49999999999999994) return 1 在這種情況下,計(jì)算中丟失一個(gè)位會(huì)導(dǎo)致答案有很大差異.

An example of this is why in Java 6 Why does Math.round(0.49999999999999994) return 1 In this case the loss of a bit in calculation results in a big difference to the answer.

這篇關(guān)于為什么多次加0.1仍然無損?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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ù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 成年免费大片黄在线观看一级 | 国产重口老太伦 | 成人av鲁丝片一区二区小说 | 涩爱av一区二区三区 | 亚洲一区二区三区高清 | 黄色一级大片视频 | 一区二区在线 | 三级成人在线观看 | 欧美一二三| 成av在线| 九九久久精品 | 国产精品成av人在线视午夜片 | 午夜电影日韩 | 国产在线中文字幕 | 日韩精品在线视频免费观看 | 成人精品啪啪欧美成 | 免费黄色录像片 | 丝袜美腿一区二区三区 | 成人影院在线视频 | 男人的天堂avav| 男女黄网站| 国产a区| 亚洲一区欧美一区 | 黄色精品| 黄片毛片| 久久成人av电影 | 视频1区2区 | 亚洲综合大片69999 | 精品视频在线播放 | 日日夜夜天天干 | 毛片一级黄色 | 亚洲免费视频网站 | 亚洲欧美成人影院 | 9久9久 | 国产精品视频区 | 成人免费视频播放 | 精品亚洲一区二区 | 亚洲国产日韩欧美 | 亚洲国产精品久久久 | 欧美福利在线 | 成人免费在线 |