Mod in Java Produces Negative Numbers

Mod in Java produces negative numbers

The problem here is that in Python the % operator returns the modulus and in Java it returns the remainder. These functions give the same values for positive arguments, but the modulus always returns positive results for negative input, whereas the remainder may give negative results. There's some more information about it in this question.

You can find the positive value by doing this:

int i = (((-1 % 2) + 2) % 2)

or this:

int i = -1 % 2;
if (i<0) i += 2;

(obviously -1 or 2 can be whatever you want the numerator or denominator to be)

Java MOD operator returns negative value

A little hint. If you have unexpected negative value when multiply (or sum) numbers, mostly this is number overflow:

private static int generateNo(int randomNo, int value) {
return (int)(((long)randomNo * value) % 256);
}

How does java do modulus calculations with negative numbers?

Both definitions of modulus of negative numbers are in use - some languages use one definition and some the other.

If you want to get a negative number for negative inputs then you can use this:

int r = x % n;
if (r > 0 && x < 0)
{
r -= n;
}

Likewise if you were using a language that returns a negative number on a negative input and you would prefer positive:

int r = x % n;
if (r < 0)
{
r += n;
}

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;
}

Is there a reason some languages allow a negative modulus?

I doubt that the remainder operator was deliberately designed to have those semantics, which I agree aren't very useful. (Would you ever write a calendar program that shows the weekdays Sunday, Anti-Saturday, Anti-Friday, ..., Anti-Monday for dates before the epoch?)

Rather, negative remainders are a side effect of the way integer division is defined.

A rem B := A - (A div B) * B

If A div B is defined as trunc(A/B), you get C's % operator. If A div B is defined as floor(A/B), you get Python's % operator. Other definitions are possible.

So, the real question is:

Why do C++, Java, C#, etc. use truncating integer division?

Because that's the way that C does it.

Why does C use truncating division?

Originally, C didn't specify how / should handle negative numbers. It left it up to the hardware.

In practice, every significant C implementation used truncating division, so in 1999 these semantics were formally made a part of the C standard.

Why does hardware use truncating division?

Because it's easier (=cheaper) to implement in terms of unsigned division. You just calculate abs(A) div abs(B) and flip the sign if (A < 0) xor (B < 0).

Floored division has the additional step of subtracting 1 from the quotient if the remainder is nonzero.

JavaScript % (modulo) gives a negative result for negative numbers

Number.prototype.mod = function (n) {
"use strict";
return ((this % n) + n) % n;
};

Taken from this article: The JavaScript Modulo Bug

Modulo of a negative number

I don't want to bother you with some complex mathematical concepts, so i'll try to keep it simple.
When we say that a = b (mod c), we simply say that a-b is a multiple of c.
This means that when we want to know what is the value of a mod c, saying that it is a or a-c or a+c or a+1000*c is true.
Thus, your 2 formulas are valid.

But what you want is to know the answer that a computer would give to you, right ?
Well, it depends of the language you are using.
With Java for example, a mod b has the sign of a and has is absolute value strictly inferior to b. This means that with a = 7, b = 3 and N = 5, (a-b)%N = 4, but your two expressions will return -1.

What I would suggest you to do if you want to do arithmetics with modulos is to create your own mod function, so it always give you a positive integer for example. This way, your 2 expressions will always be equal to the original one.

An example here in pseudocode :

function mod (int a, int N)
return (a%N+N)%N


Related Topics



Leave a reply



Submit