• 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

About the mod function! (-8)%14=?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int j=-8;
int k=14;
int i=j%k;
System.out.println(i);
what should be the result!
I just think you should do this:
(j%k) should be equal yo (j+k*m)%k (m can be any integer)
than if you let m=1 then the result be 6
if you let m=0 then the result be ?

------------------
I love this game!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you make up your own rules, Java will ignore them
Any JVM will tell you that -8 % 14 == -8. See RHE for an explanation of how the modulus operator works.
J.Lacar

Originally posted by qhliaok:
I love this game!


 
qhliaok
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hehe, I got it . TKS

The remainder operation for operands that are integers after binary numeric promotion (��5.6.2) produces a result value such that
(a/b)*b+(a%b) is equal to a. This identity holds even in the special case that the dividend is the negative integer of largest
possible magnitude for its type and the divisor is -1 (the remainder is 0). It follows from this rule that the result of the remainder
operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive; moreover, the
magnitude of the result is always less than the magnitude of the divisor. If the value of the divisor for an integer remainder operator
is 0, then an ArithmeticException is thrown.Examples:
5%3 produces 2 (note that 5/3 produces 1)
5%(-3) produces 2 (note that 5/(-3) produces -1)
(-5)%3 produces -2 (note that (-5)/3 produces -1)
(-5)%(-3) produces -2 (note that (-5)/(-3) produces 1)

Originally posted by JUNILU LACAR:
And if you make up your own rules, Java will ignore them
Any JVM will tell you that -8 % 14 == -8. See RHE for an explanation of how the modulus operator works.
J.Lacar


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic