• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Modulus

 
John Daniels
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using the modulus, if a<b, then a%b = a. But why, if a is negative, does -a%b = -a? According to the division algorithm, the remainder r must be >= 0 and <|b|.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formula Java use for integral modulo operations is



Let's see some examples:

Case 1: Positive Numbers -> 7 % 2



Conclusion: Positive yield possitive

Case 2: Negative Divisor -> 7 % -2



Conclusion: Negative divisor yields positive results.

Case 3: Negative dividend -> -7 % 2



Conclusion: Negative dividend yields negative results

Case 4: Negative dividend and divisor -> -7 % -2



Conclusion: Only negative dividend affect the result
[ May 09, 2006: Message edited by: Edwin Dalorzo ]
 
John Daniels
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic