A friendly place for programming greenhorns!
Big Moose Saloon
Search

Java FAQ

Recent Topics

Flagged Topics

Hot Topics

Zero Replies
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
Using Modulus
John Daniels
Greenhorn
Joined: May 09, 2006
Posts: 4
posted
May 09, 2006 09:08:00
0
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
Joined: Dec 31, 2004
Posts: 961
posted
May 09, 2006 09:43:00
0
The formula
Java
use for integral modulo operations is
[I]x == (x/y) * y + (x%y)[/I]
Let's see some examples:
Case 1:
Positive Numbers > 7 % 2
7 = (7/2) * 2 + (7 % 2) 7 = 3 * 2 + (7 % 2) 7 = 6 + (7 % 2) 76 = (7 % 2) 1 = (7 % 2)
Conclusion: Positive yield possitive
Case 2:
Negative Divisor > 7 % 2
7 = (7/2) * 2 + (7 % 2) 7 = 3 * 2 + (7 % 2) 7 = 6 + (7 % 2) 76 = (7 % 2) 1 = (7 % 2)
Conclusion: Negative divisor yields positive results.
Case 3:
Negative dividend > 7 % 2
7 = (7/2) * 2 + (7 % 2) 7 = 3 * 2 + (7 % 2) 7 = 6 + (7 % 2) 7+6 = (7 % 2) 1 = (7 % 2)
Conclusion: Negative dividend yields negative results
Case 4:
Negative dividend and divisor > 7 % 2
7 = (7/2) * 2 + (7 % 2) 7 = 3 * 2 + (7 % 2) 7 = 6 + (7 % 2) 7+6 = (7 % 2) 1 = (7 % 2)
Conclusion: Only negative dividend affect the result
[ May 09, 2006: Message edited by: Edwin Dalorzo ]
John Daniels
Greenhorn
Joined: May 09, 2006
Posts: 4
posted
May 09, 2006 09:58:00
0
Thanks
I agree. Here's the link:
http://aspose.com/filetools
subject: Using Modulus
Similar Threads
Dynamic form/checkbox problem
Read RSA Public Key from XML
division and testing for whole number result
Help: traverse through one array and store in another array.
1.1  blank lines
All times are in JavaRanch time: GMT6 in summer, GMT7 in winter