Why ruby modulo is different from java/other lang ?

java, modulo, ruby

Solution

In Java, the result of the modulo operation has the same sign as the dividend.

In Ruby, it has the same sign as the divisor. `remainder()` in Ruby has the same sign as the dividend.

You might also want to refer to modulo operation.

Problem

i am basically coming from java background and struggling to understand the modulo operation in Ruby. ``` (5 % 3) (-5 % 3) (5 % -3) (-5 % -3) ``` The above operation in Java yields, 2 -2 2 -2 But in Ruby, the same expression yields 2 1 -1 -2 . How logically ruby is good at this? How the module operation is implemented in Ruby ? If the same operation is defined as a web services, how both services can match the logic.

Original source