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

為什么在 Java 中比較浮點數不一致?

Why is comparing floats inconsistent in Java?(為什么在 Java 中比較浮點數不一致?)
本文介紹了為什么在 Java 中比較浮點數不一致?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

class Test{  
    public static void main(String[] args){  
        float f1=3.2f;  
        float f2=6.5f;  

        if(f1==3.2){
            System.out.println("same");  
        }else{
            System.out.println("different");  
        }
        if(f2==6.5){
            System.out.println("same");  
        }else{  
            System.out.println("different");  
        }
    }  
}  

輸出:

different
same

為什么是這樣的輸出?我希望 same 與第一種情況的結果相同.

Why is the output like that? I expected same as the result in first case.

推薦答案

不同的是,6.5 可以用 float 和 double 精確表示,而 3.2 不能用任何一種類型精確表示.并且兩個最接近的近似值是不同的.

The difference is that 6.5 can be represented exactly in both float and double, whereas 3.2 can't be represented exactly in either type. and the two closest approximations are different.

float 和 double 之間的相等比較首先將 float 轉換為 double,然后比較兩者.所以數據丟失.

An equality comparison between float and double first converts the float to a double and then compares the two. So the data loss.

您永遠不應該比較浮點數或雙精度數是否相等;因為您不能真正保證分配給 float 或 double 的數字是準確的.

You shouldn't ever compare floats or doubles for equality; because you can't really guarantee that the number you assign to the float or double is exact.

這種舍入誤差是浮點計算的一個特征.

將無限多個實數壓縮為有限位數需要一個近似的表示.雖然有無限許多整數,在大多數程序中整數計算的結果可以以 32 位存儲.

Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits.

相反,給定任意固定位數,大多數實數計算將產生的數量不能用那么多位精確表示.因此浮點計算的結果通常必須按順序四舍五入以適應其有限的表示.這個舍入誤差是浮點計算的特征.

In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.

查看每個計算機科學家都應該了解的浮點運算知識更多!

這篇關于為什么在 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(使用線程逐塊處理文件)
主站蜘蛛池模板: 欧美综合在线观看 | 久久久久黄 | 妞干网av | 午夜日韩视频 | 天天草草草 | 国产一区二区三区在线观看免费 | 精产嫩模国品一二三区 | 日韩av在线一区二区三区 | 日韩a在线| 中文字幕第九页 | 国产精品成人在线 | 中文字幕视频一区 | 在线观看视频91 | 成人深夜福利 | 一区二区三区视频在线免费观看 | 日韩精品视频在线播放 | 国产精品久久久久久婷婷天堂 | 国产精品精品3d动漫 | 国产精品一区二 | 一级毛片视频 | 国产成人精品亚洲日本在线观看 | 天天综合国产 | 欧美亚洲视频在线观看 | 日本精品一区二区三区在线观看视频 | 福利视频大全 | 欧洲性生活视频 | 在线成人精品视频 | 正在播放一区二区 | 欧美成人精品 | 特级a欧美做爰片毛片 | 一区二区精品在线 | 精品麻豆剧传媒av国产九九九 | 亚洲免费在线观看 | 久久日韩精品一区二区三区 | 瑟瑟激情| 天堂在线91 | 欧美久久一级特黄毛片 | 欧美乱人伦视频 | 久久久久av | 日韩欧美手机在线 | 亚洲天堂精品久久 |