• Post Reply Bookmark Topic Watch Topic
  • New Topic
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

Easy Question - Modulo Operator

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does 2%3 produce 2?
Thanks,
TINA
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Modulo (%) means, what is the remainder after I divide the left-hand-side (lhs) from the right-hand-side (rhs) operands.
Another way of saying it is, how much is left over after I divide 2 into 3 parts?
Well, you can't divide 2 into 3 parts, so 2 is left over.
If we write 3 % 3, the question is "how many are left over after dividing 3 by 3?" The answer is zero, because 3 exactly divides 3.
4 % 3 = 1
When you divide 4 by 3, you get 1, with 1 left over.
 
Tina Ang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. thanks Rob!
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a small addition -
10.5%2.6 will produce 0.1
This is how it works in this case -
2.6 * 4 = 10.4 (multiplier is an integral)
so 10.5 - 10 .4 = 0.1
HTH,
- Manish
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Manish,
could you please elaborate ? Is the multiplier a constant 4 ?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
10.5 % 2.6 will produce 0.1

Another method to calculate this:

10.5 - 2.6 = 7.9
7.9 - 2.6 = 5.3
5.3 - 2.6 = 2.7
2.7 - 2.6 = 0.1

0.1 - 2.6 cannot be done in this context, so 0.1 is the answer.

(Notice that I subtracted 2.6 four times - which is how Manish came up with his '4' multiplier)
 
Ratna Singh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the response.
I am new to the Ranch and after adding to the thread I was wondering if you can add questions/doubts to an old thread and if it is active and read.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ratna Singh:
thanks for the response.
I am new to the Ranch and after adding to the thread I was wondering if you can add questions/doubts to an old thread and if it is active and read.


If you read a thread and have a question about it, you can always post a reply within that thread. That thread will then get popped to the top of the list in the forum so that everyone will take notice.
Corey
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great explanation Rob !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic