Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala, why could remainder (%) operator return a negative number?

Tags:

java

modulo

scala

For example, (-3) % 2 will return -1 instead of 1.

What is the preferred way to get the positive remainder in Scala? Such as (((-3) % 2) + 2) % 2, or abs(-3 % 2)?

like image 827
Hanfei Sun Avatar asked May 02 '15 15:05

Hanfei Sun


People also ask

Can modulo return a negative number?

When only the dividend is negative. If only the dividend is negative, then: Truncated modulo returns the negative remainder; and. Floored modulo returns the positive remainder.

How does modulo operator work for negative numbers?

Modulo and remainder operators differ with respect to negative values. With a remainder operator, the sign of the result is the same as the sign of the dividend (numerator) while with a modulo operator the sign of the result is the same as the divisor (denominator).

Is result of modulo always positive?

Is modulus always positive? The answer is “Yes”. Reason: The value of modulus of any number is always positive.

Can operators apply to negative numbers?

Numeric value is positive. Numeric value is negative. Returns the ones complement of the number. The + (Positive) and - (Negative) operators can be used on any expression of any one of the data types of the numeric data type category.


3 Answers

In scala, why could remainder (%) operator return a negative number?

There are different conventions for the sign of the result of a modulo operation; Wikipedia has a good article on it. Scala, like most but by no means all programming languages, has the result take the sign of the dividend (the -3 in your case).

What is the preferred way to get the positive remainder in Scala?

I doubt there's a generally-agreed preferred way; if it were me, either use Math.floorMod, which gives a result with the sign of the divisor (2 in your example) instead of the dividend (this doesn't just mean the same value as % with a different sign, see the linked JavaDoc for details). Or just an if afterward (if (result < 0) { result += M; } [where M is the divisor, 2 in your example]).

like image 104
T.J. Crowder Avatar answered Oct 18 '22 07:10

T.J. Crowder


The correct way to get the positive modulus is to add the divisor to the negative modulus:

(-18 % 5) + 5

Taking the absolute value will give you the wrong solution in this case, though it will work if the divisor happens to be 2.

If you don't know the sign of the dividend, you can do something like this:

((dividend % divisor) + divisor) % divisor
like image 27
Kevin Avatar answered Oct 18 '22 05:10

Kevin


Using math.abs(-x % y) does not usually yield the same behavior as returning a positive modulus:

scala> math.abs(-7 % 3)
res46: Int = 1

But that's not what python (a language that returns a positive modulus) says:

In [14]: -7 % 3
Out[14]: 2

If we look at increments of 3 from -7:

-7, -4, -1, 2, ..

scala stops at -1, and python stops at 2.

like image 3
Akavall Avatar answered Oct 18 '22 05:10

Akavall