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

159 Views Asked by At

If modulo(%) is same as "remainder(r)" for positive numbers then why does "0.86%1" gives "0.86" as result and not "0" since "0.86/1 = 0.86" with remainder=0.

I have seen other questions but none of them address the moduling with 1 condition. The only way I have been able to make sense of this is by thinking that 0.86 is smaller than 1 and so cannot be divided by one thus returning 0.86 as remainder.

2

There are 2 best solutions below

1
On BEST ANSWER

You say

"0.86/1 = 0.86" with remainder=0.

Well, only in the sense that 5/2 = 2.5 with remainder 0, and it's clear that something's wrong with that, right?

When we talk about remainders, the quotient has to be an integer. It can't be 2.5 or 0.86. If you take away as many multiples of the divisor from the dividend as you can, what's left is the remainder. For 5/2, we have

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

For 0.86/1, we have

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

I guess you are not understanding Math properly.

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

This is because 1 is big enough and 1 goes 0 times in 0.86, leaving 0.86 as a remainder.

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

So what you are looking at, is right. If you want to see what's the whole number of result, then you need to do parseInt():

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

Better Explanation

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))