Why Ruby Modulo Is Different from Java/Other Lang

Why ruby modulo is different from java/other lang ?

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.

Why is the behavior of the modulo operator (%) different between C and Ruby for negative integers?

Wiki says:

Given two positive numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n.

.... When either a or n is negative, the naive definition breaks down and programming languages differ in how these values are defined.


Now the question is why -40 % 3 is 2 in Ruby or in other words what is the mathematics behind it ?

Let's start with Euclidean division which states that:

Given two integers a and n, with n ≠ 0, there exist unique integers q and r such that a = n*q + r and 0 ≤ r < |n|, where |n| denotes the absolute value of n.

Now note the two definitions of quotient:

  1. Donald Knuth described floored division where the quotient is defined by the floor function q=floor(a/n) and the remainder r is:

Sample Image

Here the quotient (q) is always rounded downwards (even if it is already negative) and the remainder (r) has the same sign as the divisor.


  1. Some implementation define quotient as:

q = sgn(a)floor(|a| / n) whre sgn is signum function.

and the remainder (r) has the same sign as the dividend(a).

Now everything depends on q:

  • If implementation goes with definition 1 and define q as floor(a/n) then the value of 40 % 3 is 1 and -40 % 3 is 2. Which here seems the case for Ruby.
  • If implementation goes with definition 2 and define q as sgn(a)floor(|a| / n), then the value of 40 % 3 is 1 and -40 % 3 is -1. Which here seems the case for C and Java.

modulus operand in ruby compared to php

They can both be considered correct, depending on your definition. If a % n == r, then it should hold that:

a == q*n + r

where q == a / n.

Whether r is positive or negative is determined by the value of q. So in your example, either of:

-4 == -1*3 + (-1)   // PHP
-4 == -2*3 + 2 // Ruby

To put it another way, the definition of % depends on the definition of /.

See also the table here: http://en.wikipedia.org/wiki/Modulus_operator#Remainder_calculation_for_the_modulo_operation. You'll see that this varies substantially between various programming languages.

What does the % operator do in Ruby in N % 2?

% is the modulo operator. The result of counter % 2 is the remainder of counter / 2.

n % 2 is often a good way of determining if a number n is even or odd. If n % 2 == 0, the number is even (because no remainder means that the number is evenly divisible by 2); if n % 2 == 1, the number is odd.

Why is -7 mod 3 = 2 in Ruby?

Because -7 minus 2 is a multiple of 3.

More specifically, the implementation of modulus used in that case happens to choose the positive modulus. Some implementations choose the modulus with the same sign as the first operand, others always choose positive, etc.

How does Modulus work with negative integers?

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

Changing the sign of the first number changes the sign of the result. The sign of the second number doesn't matter.

This is true in many languages (C, C++, Java, Javascript) but not all languages (Python, Ruby).

Mod with negative numbers gives a negative result in Java and C

The % operator is treated as a remainder operator, so the sign of the result is the same as that of the dividend.

If you want a modulo function, you can do something like this:

int mod(int a, int b)
{
int ret = a % b;
if (ret < 0)
ret += b;
return ret;
}


Related Topics



Leave a reply



Submit