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

為什么整數(shù)除法代碼給出錯誤的答案?

Why does integer division code give the wrong answer?(為什么整數(shù)除法代碼給出錯誤的答案?)
本文介紹了為什么整數(shù)除法代碼給出錯誤的答案?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我在 Java 中有一個非常簡單的除法(它是產(chǎn)品數(shù)量/每小時產(chǎn)量),但是每當(dāng)我進(jìn)行這種除法時,我都會遇到奇怪的錯誤:

I have a very simple division in Java (it's a product quantity / production per hour), however whenever I make this division I get strange errors:

float res = quantity / standard;

我用幾個值嘗試了上述除法,但總是出錯,但是我在其他任何地方都嘗試過并且正確的一個是:

I have tried the above division with several values and I always get errors, however the one that I've tried everywhere else and gotten right was this:

世界各地:

13.6 = 6800 / 500;

Java:

13.0 = 6800 / 500;

我研究過 BigDecimal 和 BigInteger,但是我還沒有找到用它們創(chuàng)建這種除法的方法,有沒有其他方法可以在 Java 中進(jìn)行這種除法而不會出現(xiàn)精度錯誤??

I've researched BigDecimal and BigInteger, however I haven't found a way to create this division with them, is there any other way to do this division in Java without having precision errors??

任何幫助將不勝感激.

推薦答案

你正在對整數(shù)進(jìn)行除法,這意味著你正在使用整數(shù)除法.

You're dividing integers, which means that you're using integer division.

在整數(shù)除法中,結(jié)果的小數(shù)部分被丟棄.

In integer division the fractional part of the result is thrown away.

嘗試以下方法:

float res = (float) quantity / standard;
            ^^^^^^^

上面的內(nèi)容強(qiáng)制將分子視為 float 反過來促進(jìn)分母也浮動,并且執(zhí)行浮點除法而不是整數(shù)除法.

The above forces the numerator to be treated as a float which in turn promotes the denominator to float as well, and a float-division is performed instead of an int-division.

請注意,如果您正在處理文字,則可以更改

Note that if you're dealing with literals, you can change

float f = 6800 / 500;

包含 f 后綴以使分母變?yōu)楦↑c數(shù):

to include the f suffix to make the denominator a float:

float f = 6800f / 500;
              ^

這篇關(guān)于為什么整數(shù)除法代碼給出錯誤的答案?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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,如何獲取插入的自動生成密鑰?[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 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 在线中文字幕av | 成人中文字幕在线观看 | 日韩美av| 亚洲啪啪| 久久久久国产一区二区三区 | 亚洲自拍偷拍免费视频 | 精品日韩 | 欧美日韩视频在线第一区 | 国产精品视频999 | 精品一区二区电影 | av在线播放一区二区 | 精品不卡 | 一级毛片免费 | 久久综合九色综合欧美狠狠 | avhd101在线成人播放 | 尤物视频在线免费观看 | 亚洲成人福利在线观看 | 国产伦一区二区三区 | 国产亚洲日本精品 | 18gay男同69亚洲网站 | 国产美女自拍视频 | 免费国产视频 | 亚洲国产精品99久久久久久久久 | 孕妇一级毛片 | 九九99精品 | 狠狠操天天操 | 国产精品美女久久久久aⅴ国产馆 | 色综合色综合色综合 | 亚洲视频手机在线 | 亚洲三区在线观看 | 国产精品欧美一区二区 | 欧美极品在线 | 久久久久国产一区二区三区 | 在线黄 | 午夜免费电影院 | 国产一区不卡 | www.成人免费视频 | 免费福利视频一区二区三区 | 欧美视频免费在线 | 色婷婷av一区二区三区软件 | 精品一区二区三区在线视频 |