Best Way to Make Java's Modulus Behave Like It Should with 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)

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

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

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

Why is was my code not performing with negative values inputted?

The remainder of a negative number divided by a positive number will be negative.

-1 % 2  

is

-1

but since that is not 1, you will declare it to be even.

Why does modulo operation gives different results in Java VS Perl?

The second case:

  • % operates on integers and floating-point numbers in Java,
  • % only operates on integers in Perl.

The third case:

  • Java defines the modulus operation such that the following equation is true:

    dividend == ((int)(dividend/divisor)) * divisor + (dividend % divisor)

    e.g. -1 = 0 * 1000 + -1
  • Perl defines the modulus operation such that the following equation is true:

    $dividend == floor($dividend/$divisor) * divisor + ($dividend % $divisor)

    e.g. -1 = -1 * 1000 + 999

The Perl way has the advantage that the quotient (floor($dividend/$divisor)) will always have the same sign as the dividend.


To get the same behaviour as Java in Perl, use the POSIX::fmod function.

This is identical to the C function fmod().

$r = fmod($x, $y);

It returns the remainder $r = $x - $n*$y, where $n = trunc($x/$y). The $r has the same sign as $x and magnitude (absolute value) less than the magnitude of $y.

use POSIX 'fmod';

$m1 = fmod(1, 1000); # 1
$m2 = fmod(0.01, 1000); # 0.01
$m3 = fmod(-1, 1000); # -1

Modulo of negative numbers

This solution is branchless, but performs % twice:

function wrapIndex(i, i_max) {
return ((i % i_max) + i_max) % i_max;
}

It should be said the C#/Java behavior of % is assumed, i.e. the result has the same sign as the dividend. Some languages define the remainder calculation to take the sign of the divisor instead (e.g. mod in Clojure). Some languages have both variants (mod/rem pair in Common Lisp, Haskell, etc). Algol-68 has %x which always returns a non-negative number. C++ left it up to implementation until C++11, now the sign of the remainder is (almost) fully specified according to the dividend sign.

See also

  • Wikipedia/Modulo operation


Related Topics



Leave a reply



Submit