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) andn
(the divisor), a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division ofa by n
.
.... When eithera
orn
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
andn
, withn ≠ 0
, there exist unique integersq
andr
such thata = n*q + r
and0 ≤ r < |n|
, where|n|
denotes the absolute value ofn
.
Now note the two definitions of quotient:
- Donald Knuth described floored division where the quotient is defined by the floor function
q=floor(a/n)
and the remainderr
is:
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.
- 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 defineq
asfloor(a/n)
then the value of40 % 3
is1
and-40 % 3
is2
. Which here seems the case for Ruby.- If implementation goes with definition
2
and defineq
assgn(a)floor(|a| / n)
, then the value of40 % 3
is1
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
How to Set -Source 1.7 in Android Studio and Gradle
Spring Boot - Handle to Hibernate Sessionfactory
Why Ruby Modulo Is Different from Java/Other Lang
Pyspark: Exception: Java Gateway Process Exited Before Sending the Driver Its Port Number
Fixing 'Path' in Environment Variables in Windows 7 for Multiple Applications
Serializing with Jackson (JSON) - Getting "No Serializer Found"
Integration Testing Frameworks for Testing a Distributed System
Convert from Enum Ordinal to Enum Type
Create Random Pixel Images in Swift
Passing a String by Reference in Java
How to Escape Apostrophe or Quotes on a Jsp (Used by JavaScript)
What Java Xml Library Do You Recommend (To Replace Dom4J)
Execute Managebean Method from JavaScript Onload Event
Why Does Changing the Sum Order Returns a Different Result
Authenticated Http Proxy with Java