問題描述
我有這樣的價(jià)值觀:
long millis = 11400000;int 常數(shù) = 86400000;雙分辨率 = 毫/常數(shù);
問題是:為什么 res
等于 0.0
(而不是 ca. 0.131944
)?它存儲(chǔ)在 double
中,所以應(yīng)該沒有四舍五入吧?
當(dāng)你使用二元運(yùn)算符時(shí),兩個(gè)參數(shù)的類型應(yīng)該相同,結(jié)果也應(yīng)該是它們的類型.當(dāng)你想劃分 (int)/(long)
它變成 (long)/(long)
結(jié)果是 (long)
.您應(yīng)該將其設(shè)為 (double)/(long)
或 (int)/(double)
以獲得雙重結(jié)果.由于 double 大于 int 和 long,所以 int 和 long 在 (double)/(long)
和 (int)/(double)
I have values like this:
long millis = 11400000;
int consta = 86400000;
double res = millis/consta;
The question is: why res
equals 0.0
(instead of ca. 0.131944
)? It's stored in double
so there should be no rounding right?
When you are using a binary operator, both arguments should be of a same type and the result will be in their type too. When you want to divide (int)/(long)
it turns into (long)/(long)
and the result is (long)
. you shouldmake it (double)/(long)
or (int)/(double)
to get a double result. Since double is greater that int and long, int and long will be turned into double in (double)/(long)
and (int)/(double)
這篇關(guān)于意外結(jié)果導(dǎo)致長/整數(shù)除法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!