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

檢查雙重平等是否安全?

Is checking a double for equality ever safe?(檢查雙重平等是否安全?)
本文介紹了檢查雙重平等是否安全?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有以下代碼:

double x = 0;

{ ...do stuff ...}

if(x == 0){

}

我總是被教導(dǎo)不要檢查浮點(diǎn)數(shù)是否相等.檢查它是否等于零有什么不同嗎?

I was always taught that you shouldn't check floats for equality. Is checking to see if it is equal to zero any different?

推薦答案

你不應(yīng)該檢查浮點(diǎn)數(shù)是否相等的原因是浮點(diǎn)數(shù)不是完全精確的——存儲(chǔ)中有一些數(shù)字不準(zhǔn)確,比如那些這延伸到尾數(shù)和重復(fù)小數(shù)太遠(yuǎn)(請(qǐng)注意,我正在談?wù)撘?為底的重復(fù)小數(shù)).您可以將這種不精確性視為四舍五入".超出浮點(diǎn)數(shù)精度的數(shù)字將被截?cái)?,?shí)際上是向下舍入.

The reason you shouldn't check floats for equality is that floating point numbers are not perfectly precise -- there's some inaccuracy in storage with some numbers, such as those that extended too far into the mantissa and repeating decimals (note that I'm talking about repeating decimals in base 2). You can think of this imprecision as "rounding down". The digits that extend beyond the precision of the floating-point number are truncated, effectively rounding down.

如果它沒有改變,它將保持這種平等.但是,如果你稍微改變它,你可能不應(yīng)該使用等式,而是使用像 (x < 0.0001 && x > -.0001) 這樣的范圍.

If it has not changed, it will keep that equality. However, if you change it even slightly, you probably should not use equalities, but instead a range like (x < 0.0001 && x > -.0001).

簡(jiǎn)而言之:只要你不是在很小的級(jí)別上玩 x 就可以了.

In short: as long as you're not playing with x at a very small level, it's OK.

這篇關(guān)于檢查雙重平等是否安全?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(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ù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 欧美电影免费网站 | 亚洲成人免费视频在线 | 精品国产区 | 久久噜噜噜精品国产亚洲综合 | 国产精品久久国产愉拍 | 精品久久国产 | 国产视频二区在线观看 | 日本精品一区二区 | 99pao成人国产永久免费视频 | 在线中文字幕亚洲 | 另类a v| 日韩精品一区二区在线观看 | 国产成人久久av免费高清密臂 | 一区二区免费看 | 成人欧美一区二区三区 | a天堂在线| a级在线免费观看 | 欧美欧美欧美 | 一区二区三区精品视频 | 成人精品一区二区 | 狠狠干网站| 麻豆精品一区二区三区在线观看 | 午夜av在线| 福利一区二区 | 龙珠z国语版在线观看 | 1000部精品久久久久久久久 | 一级特黄视频 | 欧美一区二区久久 | 国产精品久久久久久久免费大片 | 性色在线 | 农夫在线精品视频免费观看 | 成人精品一区二区 | 久久伊人一区二区 | 国产一区二区精 | 免费观看www7722午夜电影 | 精品一区二区三区不卡 | 中文字幕日韩在线 | 亚洲精品乱码久久久久久按摩观 | 亚洲一区二区 | 国产精品视频一二三区 | 午夜视频免费在线观看 |