久久久久久久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(使用線程逐塊處理文件)
主站蜘蛛池模板: 99精品国自产在线观看 | 久久久999国产精品 中文字幕在线精品 | 国产精品国产三级国产播12软件 | 久久99国产精品 | 免费一区二区三区 | 国产一区精品 | 欧美一区二区三区大片 | caoporn国产精品免费公开 | 国产精品久久久久aaaa樱花 | 91免费版在线观看 | 97精品久久 | 久久精品99国产精品 | 综合久久99 | 天天干天天草 | 久久首页| 亚洲资源站| av一二三四 | 99国产精品99久久久久久粉嫩 | 婷婷在线免费 | 国产一区在线视频 | 亚洲黄色一区二区三区 | 久久久久国产一区二区三区 | 久久午夜国产精品www忘忧草 | 日本精品一区 | 在线国产一区二区 | 国产人免费人成免费视频 | 91在线看网站 | 成人免费视频7777777 | 欧美国产日韩在线 | 久久天堂 | 国产一级黄色网 | 亚洲bt 欧美bt 日本bt | 国产黄视频在线播放 | 欧美一区二区在线观看视频 | 精品日韩 | 日本精品一区二区三区在线观看视频 | 欧美一级久久久猛烈a大片 日韩av免费在线观看 | 精品在线播放 | 欧美精品电影一区 | 久久久久久久久一区 | 久久久久亚洲国产|