为什么 0.86%1 不是零而是 0.86,因为 0.86/1 给出余数 0?

Why is 0.86%1 not zero but 0.86 since 0.86/1 gives remainder 0?

如果正数的模 (%) 与 "remainder(r)" 相同,那么为什么“0.86%1”给出“0.86”而不是“0”,因为“0.86/1 = 0.86”和余数=0。

我看到了其他问题,但其中 none 解决了 1 条件下的模块化问题。我能够理解这一点的唯一方法是认为 0.86 小于 1,因此不能除以 1,因此返回 0.86 作为余数。

我猜你没有正确理解数学。

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

这是因为1足够大,1在0.86中走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.86/1 = 0.86" with remainder=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