久久久久久久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(使用線程逐塊處理文件)
主站蜘蛛池模板: 天天操天天拍 | 亚洲精品久久久久久久不卡四虎 | 日本人麻豆 | 国家aaa的一级看片 h片在线看 | 福利片在线 | 涩色视频在线观看 | 国产成人精品免费 | 日韩一区三区 | 亚洲欧洲成人av每日更新 | 中文字幕第49页 | 国产综合精品一区二区三区 | 精品国产一区二区三区免费 | 噜噜噜噜狠狠狠7777视频 | 欧美性高潮 | 91婷婷韩国欧美一区二区 | 国产一区二区在线观看视频 | 一区二区电影 | 欧美aaaaaaaaaa| 亚洲国产精品一区二区三区 | av手机在线免费观看 | 黑人巨大精品 | 久久精品色欧美aⅴ一区二区 | 日本成人区 | 欧美 日韩 国产 成人 | 福利精品| 亚洲高清av | 国产99精品 | 亚洲精品在线播放 | 国产精品不卡 | 人操人免费视频 | 欧美国产精品 | 亚洲国产成人精品久久 | 91性高湖久久久久久久久_久久99 | 91久久 | 久久国产精品久久国产精品 | 国产一区二区三区视频在线观看 | 日韩在线| 亚洲最大的黄色网址 | 国产999精品久久久 午夜天堂精品久久久久 | 国产高清久久 | 国产一区二区三区日韩 |