The moose likes Programming Diversions and the fly likes minus modulo minus equals plus? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Reply Bookmark "minus modulo minus equals plus?" Watch "minus modulo minus equals plus?" New topic
Author

minus modulo minus equals plus?

Seetharaman Venkatasamy
Bartender

Joined: Jan 28, 2008
Posts: 4503


output is -4 . but it should be 4 . am i missing something?


Not everything that counts can be counted, and not everything that can be counted counts-Albert Einstein
John Jai
Rancher

Joined: May 31, 2011
Posts: 1372
"fall down seven times, get up eight times"-Bodhidharman
+1
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 26716
How on earth would you get 4 out of 9 remainder 5? If you want a positive remainder, it would be 1.
You should read about the conventions behind the % operator. It is defined as a “Euclidean” operator, which means that multiplying back and adding the remainder gives the original dividend. If you try -9 % -5 and get a remainder of 1, then that is not “Euclidean”; if you multiply back, you will get -4.
Seetharaman Venkatasamy
Bartender

Joined: Jan 28, 2008
Posts: 4503

Thanks Campbell
 
IntelliJ Java IDE
 
subject: minus modulo minus equals plus?
 
Threads others viewed
Q. from JQ+
generating random numbers
Very New (obviously) - Program terminates
Posting Density Stastics
XML as calculator input
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture