Suane Mane wrote:In mathematical logic it should not. 1 divided by 2 is 0.5.
Liutauras Vilda wrote:
But you are not dividing, you use modulus operation in actual question.
What would be 3 % 2 according to you?
Dave Tolls wrote:0.5 is not a whole number, so 1 divided by 2 is 0 remainder 1.
So the modulus is 1.
Suane Mane wrote:In order to find the remainder you need to divide. So you are dividing even if the result required isn't the result of the division. 3%2 would have a remainder 1, yes.
Liutauras Vilda wrote:I think Dave gave an info, that the division is based on how many whole numbers fit. 3 % 2 as in this case, how many 2's fit to 3, so you divide 3 / 2, you get 1 and remainder 1, so you are correct on that one. And now if you get back to your initial example 1 % 2, how many 2's fit to 1? 0. You don't take fractions.
there are two different conventions:-The two use different rounding conventions for integer division. If you can't work out from trial and error which convention Java® uses, as PS suggested, all the gory details are explained in the Java® Language Specification. Note the operator is usually called “remainder” in English and “moduus” in other European languages.
Piet Souris wrote:. . . For negative values, things are a little different . . .
I hae had the same argument from my former supervisor, who said that floating‑point numbers don't have a remainder.
fred rosenberger wrote:. . . but by your logic, modulus would never return anything but zero. . . .
fred rosenberger wrote:Not to beat a dead horse, but by your logic, modulus would never return anything but zero. 12 divided by five would be 2.4, so 0 is the remainder.
15 divided by 8 is 1.875, so zero is the remainder...
Modus, by definition, is the leftover after integer division. so 2 goes into 1 exactly zero times, with one left over. just like 5 goes into 12 two times, with two left over...
You need to check that; as I said earlier it differs from language to language, but in the case of Java®, the language specification strictly defines the result.
Suane Mane wrote:. . . any int division with smaller dividend will yield a remainder equal to the value of the dividend itself.