Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Why is modulo operator producing these results

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
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:

Thanks

author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:

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:

Can you explain to us why you think that 1 mod 10 should be zero?

Henry

Ranch Hand
Posts: 172
• 1
• Number of slices to send:
Optional 'thank-you' note:
I do agree with Henry here,
How do you think a modulus operator function as?
Once you are able to answer this, I'm certain you would be able to answer the question that you have posted too.
Cheers.

Bartender
Posts: 4568
9
• 1
• Number of slices to send:
Optional 'thank-you' note:
Are you confusing it with division? Any number less than 10 divided by 10 is zero when you're using integers. But that isn't what the modulus operator means.

Marshal
Posts: 79019
375
• 1
• Number of slices to send:
Optional 'thank-you' note:
It is actually called the remainder operator; even the name might help to explain its purpose.
When I did maths, which was a long time ago, we used modulus (|x|) to mean the absolute value of a number, so |-1| = 1. In most non‑English‑speaking countries it is called modulo and means remainder. A lot of people use modulo to mean remainder in Britain, too.
The remainder operation is very closely related to the division operator; in fact on many computers the two use the same part of the chip and the circuit produces the quotient on one output and the remainder on another output. It is classified by Java as a multiplicative operation and you can read about it in the Java Language Specification, but it may be difficult to understand. You might do well to read only those parts relating to integer arithmetic.

Ranch Hand
Posts: 178
2
• 1
• Number of slices to send:
Optional 'thank-you' note:
% get the remainder. Remind the long division done in small classes. It is done in here...

Stephen-Austin Murphy
Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Thanks everyone who replied.
<red-face>OMG, I can't believe I had forgotten some basic math.</red-face>

My understanding of the % operator is that it returns the remainder after division.

Here was my reasoning that led to my post:
---------------------------------------------------------------------------------------------------------------
So 1 mod 10 is the remainder of 1 divided by 10.
1 divided by 10 is .1 with no remainder.
---------------------------------------------------------------------------------------------------------------
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?

Thanks.

Matthew Brown
Bartender
Posts: 4568
9
• 1
• Number of slices to send:
Optional 'thank-you' note:

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?

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.

Bartender
Posts: 2856
10
• 1
• Number of slices to send:
Optional 'thank-you' note:
To make the understanding simple, here is how I think of it.
Here 1 is the dividend, 10 is the divisor so in the world of integers 1/10 = 0 and so is 2/10...
Now dividend = divisor x quotient + remainder
So
1 = 10 x 0 + 1
2 = 10 x 0 + 2
and so on.

If you have noticed, the % operator is showing the remainder here, which is the number after the + sign.
Now if the dividend is fully divisible, we will get zero.
For example
100 = 10 x 10 + 0
6 = 3 x 2 + 0
So 100%10 and 6%3 are both 0.

Henry Wong
author
Posts: 23949
142
• 1
• Number of slices to send:
Optional 'thank-you' note:

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.

Since everyone is helping the OP get clearer, I get to throw a monkey wrench into the works...

The % operator actually works with floating points... for example ...

4.4 % 1.4 = 0.2

Henry

Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
• 1
• Number of slices to send:
Optional 'thank-you' note:
In floating point division, convert both divisor and dividend into integer values. (By multiplying 10^x where x is and integer. ^ means power.)
Then do the long division.
Convert the remainder into a float by dividing it by 10^x..!

Don't do long division too much long. Get only the Integer answer. Don't divide remainder again..!!

Campbell Ritchie
Marshal
Posts: 79019
375
• 1
• Number of slices to send:
Optional 'thank-you' note:
That is correct, but confusingly written. You mean to divide until the quotient reaches its radix point/decimal point and whatever remains is the remainder.

Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
• 1
• Number of slices to send:
Optional 'thank-you' note:
Yes.. That is..!!
I'm not fluent in English. I may have done mistakes...

Stephen-Austin Murphy
Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Thank you everyone. Yes, my problem was that I was confusing floating point division and integer division.

I am clear on this now.

Kind regards

Campbell Ritchie
Marshal
Posts: 79019
375
• Number of slices to send:
Optional 'thank-you' note:
You're welcome