Modulus

posted Wed, 10 Nov 2004 22:59:00 GMT by Jonas Bengtsson

In Python I get this:
>>> 2 % 5
2
>>> -2 % 5
3
>>> 2 % -5
-3
>>> -2 % -5
-2
But in Java I get this:
System.out.println(2%5);  //2
System.out.println(-2%5);  //-2
System.out.println(2%-5);  //2
System.out.println(-2%-5);  //-2
Who’s right I don’t know. I guess that you might say that the way Java does it is more correct since no division is taking place. But the way I wanted it to work today at work (coding C++) is the Python way.

Comments Zero comments

Comments

Respond

This is where you come in. Please leave a comment whether you agree, disagree or none of the above.

(required)

(to your blog or homepage)

(won't be shown nor shared)


  Preview