Author Topic: Modulo of negative numbers  (Read 1961 times)

(Blockland) 3 % -2 gives 3
(Windows calculator) 3 mod -2 gives 1
(Wolfram Alpha) 3 mod -2 gives -1

Which is right?
And can someone explain modulous with the second operand being negative to me?
« Last Edit: November 11, 2013, 02:46:47 AM by DYLANzzz »

If you need a proper mod function in Blockland that works better than the normal % operator, use this:

function mod(%a, %b) {
    %result = %a - %b * mFloor(%a / %b);

    if (%a < 0) {
        return %b + %result;
    }

    return %result;
}


Works for mod(n, n), mod(-n, n), mod(n, -n) and mod(-n, -n).
The % operator only works for n % n.
« Last Edit: November 11, 2013, 03:48:31 AM by Port »

3 % -1 = 2
Blockland gives 3
Windows Calculator gives 0
Wolfram Alpha gives 0

What exactly was that post for though?
« Last Edit: November 11, 2013, 02:48:58 AM by DYLANzzz »

Blockland gives 3
Windows Calculator gives 0
Wolfram Alpha gives 0

What exactly was that post for though?

Oh, never mind that, you meant for negative divisors - I somehow parsed it as -1 % 3, which is 2.

because I don't know, I'll ask you a question instead: EDIT: okay i do know i just had to think about it

firstly, remember that modulus is essentially the remainder of a division operation.
3 / 2 = 1 remainder 1 (like in grade school)
3 / 2 = 1.5 (assuming we don't care about integer-only)
3 % 2 = 1 (just the remainder)

so...
when you divide 3 by -2...
1: how many times does -2 go into 3?
2: what number is left over after doing so?

3 / -2 = -1
-2 goes into 3 negative 1 times

3 + -2 = -1
remainder is -1

therefore, 3 % -2 = -1

CHECK:
because -1 is one half of -2, we can say that 3 / -2 = -1.5

-1.5 * -2 = +3

my ti 84+ silver edition verifies these results.
« Last Edit: November 11, 2013, 03:08:11 AM by Lugnut »

The real use of modulus in coding is to constrain an answer to somewhere between 0 and n-1.  Think of it more in terms of what you would read on a 12 hour clock.  Negative answers should never be a thing.

If you want to know the time that is 9 hours before 4:00, you would do (4-9) % 12, which should result in 7.  A negative answer is not helpful.

The real use of modulus in coding is to constrain an answer to somewhere between 0 and n-1.  Think of it more in terms of what you would read on a 12 hour clock.  Negative answers should never be a thing.

If you want to know the time that is 9 hours before 4:00, you would do (4-9) % 12, which should result in 7.  A negative answer is not helpful.
no, the modulus tells you the remainder

if you want 9 hours before 4, you would do |4-9| % 12
absolute value
modulus should do exactly the right operation

I've confused myself but I agree with the following:

modulus should do exactly the right operation

Don't compromise how dynamic a low level function is. As a programmer, when you say:

Negative answers should never be a thing.

Then use other operators like absolute value to make it so, don't change existing code.
« Last Edit: November 11, 2013, 09:22:25 PM by Truce »

If you do modulus in a math class, you can get negative numbers because it is based around remainders.  If you are asked to do modulus in a computer science class, you should not.  The point is to constrain the answer to 0 to n-1, or the domain Zn

if you want 9 hours before 4, you would do |4-9| % 12
Except that would return 5 which is incorrect

I personally don't trust the modulus in blockland anyway and always use some thing like
%val = %x - mfloor(%x/12)*12;
I'm pretty sure something like -3.5 will round down to -4
« Last Edit: November 12, 2013, 12:48:08 AM by Nexus »

that's unpredictable

mathematical functions should produce mathematical outputs

Actually I misread, I thought there was some confusion about a negative number mod a positive number, but it is the other way around.  In that case the answer probably should be negative but honestly isn't particularly well defined because the domain from 0 to n-1 when n is negative doesn't exist.  In that case, just default to the pure math definition.  My bad there.