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
Post a Comment