Stephen-Austin Murphy wrote:The following code is yielding non-zero results. I thought that 10% any number less than 10 and >=0 should be zero.
Following is output:
Stephen-Austin Murphy wrote:
But just now I actually did the long division:
1 divided by 10 is .1 with .9 as a remainder. So Java rounds to 1.
Similarly, 2 divided by 10 is .2 with 1.8 as a remainder. So Java rounds to 2.
Is the above correct?
Matthew Brown wrote:
I think you may still be getting mixed up between integer division and floating-point division.
With real numbers (represented by floating point) there is no such thing as a remainder. We simply have exact division. So 1.0/10.0 = 0.1, and 2.0/10.0 = 0.2.
Remainders only come into play when you're dealing with integers only. In integer arithmetic 1 divided by 10 is zero, with a remainder of 1. There's no rounding going on.