2017-01-07 43 views
0

如果模數(%)與正數「餘數(r)」相同,那麼爲什麼「0.86%1」的結果爲「0.86」而不是「0」,因爲「0.86/1 = 0.86」的餘數爲0 。爲什麼0.86%1不是零而是0.86,因爲0.86/1會給出餘數0?

我已經看到其他問題,但沒有一個解決1條件的模塊。我能夠理解這一點的唯一方法是認爲0.86小於1,因此不能被一除以0.86作爲餘數。

+0

如果你找到答案回答你的問題之一,請上打鉤按鈕點擊接受的答案。 –

回答

5

你說

「0.86/1 = 0.86」w餘數= 0。

那麼,只有在5/2 = 2.5和餘數爲0的意義上,很明顯有什麼不對的地方,對吧?

當我們談論餘數時,商必須是一個整數。它不能是2.5或0.86。如果您儘可能多地從股息中除數倍數,剩下的就剩下了。對於5/2,我們有

5-2 = 3 
3-2 = 1 
2>1, so we can't subtract any more, and the remainder is 1 

爲0.86/1,我們有

1>0.86, so we can't subtract any copies of 1 from 0.86, and the remainder is 0.86 
+0

**「當我們談論餘數時,商必須是一個整數。」**這是我推理過程中的錯誤所在。謝謝。 –

5

我想你對數學不是很瞭解。

0.86 
----- = 0 Quotient & 0.86 Remainder 
    1 

這是因爲1在0.86中變得足夠大,1變爲0,餘下0.86。

0.86 % 1 // This gives remainder, not the quotient. 

所以你在看什麼是對的。如果你想看看有什麼結果的整數,那麼你需要做的parseInt()

parseInt(0.86/1, 10); // This gives you 0! 

更好的解釋

function divide(up, down) { 
 
    if (down == 0) 
 
    return false; 
 
    var res = up/down; 
 
    var rem = up % down; 
 
    console.log("Result (Quotient): " + parseInt(res, 10)); 
 
    console.log("Remainder:   " + rem); 
 
} 
 

 
console.log(divide(0.86, 1)); 
 
console.log(divide(7, 2))

+0

你不需要'console.log(divide(0.86,1));'。只需調用函數('divide(0.86,1)')即可。這樣,你可以避免控制檯中的'undefined'。 –

+1

@GerardoFurtado我注意到了。讓它成爲一個分隔符。大聲笑。 ':D'什麼說? –

+1

是的,這是'undefined'的新功能! –