java - How does Modulus work with negative integers? -


i running loop , inside loop have following:

for(int = 0; < 12; = + 2){         system.out.println("i = " + i);         system.out.print("3 - % 3 (i @ " + + ") = " + (3 - % 3));         system.out.println();         system.out.println("3 - (" + (i) + ") = " + (3 - i));     } 

i understand how modulus works or positive numbers, not understand how works negative integers? can explain me please?

many thanks.

 4 %  3 ==  1 -4 %  3 == -1  4 % -3 ==  1 -4 % -3 == -1 

changing sign of first number changes sign of result. sign of second number doesn't matter.

this true in many languages (c, c++, java, javascript) not languages (python, ruby).


Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -