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

如何避免 Java 中的浮點(diǎn)數(shù)或雙精度數(shù)的浮點(diǎn)精度

How to avoid floating point precision errors with floats or doubles in Java?(如何避免 Java 中的浮點(diǎn)數(shù)或雙精度數(shù)的浮點(diǎn)精度錯(cuò)誤?)
本文介紹了如何避免 Java 中的浮點(diǎn)數(shù)或雙精度數(shù)的浮點(diǎn)精度錯(cuò)誤?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)非常煩人的問(wèn)題,即 Java 中的浮點(diǎn)數(shù)或雙精度數(shù)很長(zhǎng).基本上這個(gè)想法是,如果我執(zhí)行:

I have a very annoying problem with long sums of floats or doubles in Java. Basically the idea is that if I execute:

for ( float value = 0.0f; value < 1.0f; value += 0.1f )
    System.out.println( value );

我得到的是:

0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.70000005
0.8000001
0.9000001

我知道有浮動(dòng)精度誤差的累積,但是,如何擺脫這個(gè)?我嘗試使用 doubles 將錯(cuò)誤減半,但結(jié)果還是一樣.

I understand that there is an accumulation of the floating precision error, however, how to get rid of this? I tried using doubles to half the error, but the result is still the same.

有什么想法嗎?

推薦答案

沒(méi)有將 0.1 精確表示為 floatdouble.由于這種表示錯(cuò)誤,結(jié)果與您的預(yù)期略有不同.

There is a no exact representation of 0.1 as a float or double. Because of this representation error the results are slightly different from what you expected.

您可以使用的幾種方法:

A couple of approaches you can use:

  • 當(dāng)使用 double 類(lèi)型時(shí),只顯示你需要的數(shù)字.在檢查相等性時(shí),無(wú)論哪種方式都允許有一個(gè)小的容差.
  • 或者使用允許您存儲(chǔ)要精確表示的數(shù)字的類(lèi)型,例如 BigDecimal 可以精確表示 0.1.
  • When using the double type, only display as many digits as you need. When checking for equality allow for a small tolerance either way.
  • Alternatively use a type that allows you to store the numbers you are trying to represent exactly, for example BigDecimal can represent 0.1 exactly.

BigDecimal 的示例代碼:

BigDecimal step = new BigDecimal("0.1");
for (BigDecimal value = BigDecimal.ZERO;
     value.compareTo(BigDecimal.ONE) < 0;
     value = value.add(step)) {
    System.out.println(value);
}

在線查看:ideone

這篇關(guān)于如何避免 Java 中的浮點(diǎn)數(shù)或雙精度數(shù)的浮點(diǎn)精度錯(cuò)誤?的文章就介紹到這了,希望我們推薦的答案對(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 中的組件周?chē)h(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电影 | 在线亚洲一区二区 | 日本三级网址 | 精品96久久久久久中文字幕无 | 蜜桃av一区二区三区 | 午夜男人的天堂 | 亚洲男人天堂2024 | 毛片网在线观看 | 午夜二区| 成人日韩av | 日韩精品四区 | 中文一区 | 久久精品久久久久久 | 综合精品在线 | 99精品欧美一区二区三区综合在线 | 一区二区亚洲 | 一级黄色片在线免费观看 | 色综合美女 | 亚洲 成人 在线 | 日本久久综合网 | 一区二区在线 | 北条麻妃99精品青青久久主播 | 国产一区三区视频 | 在线亚洲电影 | 成人国产精品久久久 | 国产亚洲一级 | 亚洲一区二区免费 | av在线免费网站 | 中国美女撒尿txxxxx视频 | 91av导航 | 精品免费观看 | 亚洲人成在线观看 | 一级片成人| 亚洲电影一区 | 久久高清国产 |