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

Java中浮點數的精度錯誤

Precision error with floats in Java(Java中浮點數的精度錯誤)
本文介紹了Java中浮點數的精度錯誤的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想知道在 Java 中修復精度錯誤的最佳方法是什么.正如您在以下示例中看到的那樣,存在精度錯誤:

I'm wondering what the best way to fix precision errors is in Java. As you can see in the following example, there are precision errors:

class FloatTest
{
  public static void main(String[] args)
  {
    Float number1 = 1.89f;
    
    for(int i = 11; i < 800; i*=2)
    {
      System.out.println("loop value: " + i);
      System.out.println(i*number1);
      System.out.println("");
    }
  }
}

顯示的結果是:

循環值:11

20.789999

循環值:22

41.579998

循環值:44

83.159996

循環值:88

166.31999

循環值:176

332.63998

循環值:352

665.27997

循環值:704

1330.5599

另外,如果有人能解釋為什么它只從 11 開始,并且每次都將價值翻倍.我認為所有其他值(或至少其中許多值)都顯示了正確的結果.

Also, if someone can explain why it only does it starting at 11 and doubling the value every time. I think all other values (or many of them at least) displayed the correct result.

這樣的問題過去讓我很頭疼,我通常使用數字格式化程序或將它們放入字符串中.

Problems like this have caused me headache in the past and I usually use number formatters or put them into a String.

正如人們所提到的,我可以使用雙精度,但在嘗試之后,似乎 1.89 作為雙倍乘以 792 仍然會輸出錯誤(輸出為 1496.8799999999999).

As people have mentioned, I could use a double, but after trying it, it seems that 1.89 as a double times 792 still outputs an error (the output is 1496.8799999999999).

我想我會嘗試其他解決方案,例如 BigDecimal

I guess I'll try the other solutions such as BigDecimal

推薦答案

如果你真的很在意精度,你應該使用 BigDecimal

If you really care about precision, you should use BigDecimal

https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/math/BigDecimal.html

這篇關于Java中浮點數的精度錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产91在线视频 | 久久丝袜视频 | 亚洲精品一区二三区不卡 | 国产精品视频在线观看 | 欧美韩一区二区 | 一区二区三区成人 | 九九色综合 | 久久一区二区av | 国产日韩精品视频 | 狠狠干综合视频 | 国产视频福利在线观看 | 福利片在线观看 | 亚洲精品在线看 | 国产精品99久久久久久www | 亚洲精品2| 久在线观看 | 视频一区二区国产 | 亚洲一区二区不卡在线观看 | 欧美日韩久 | 日韩中文字幕一区二区 | 国产高清在线 | 国产精品国产a | 日韩在线视频观看 | 国产成人精品一区二区三 | 91精品中文字幕一区二区三区 | 亚洲毛片网站 | 欧美国产91| 成人在线一区二区三区 | 欧美精品一区二区三 | 欧美三级在线 | 成人日b视频 | 一区二区三区中文字幕 | 精品欧美二区 | 亚洲a视频| 不卡一区二区三区四区 | 欧美久久一区二区三区 | 亚洲欧美日韩系列 | 国产亚洲一区二区三区 | 亚洲国产aⅴ精品 | 最新伦理片 | 一区二区福利视频 |