Mod with negative numbers gives a negative result

2019-04-17 17:01发布

问题:

Let's say I have (-5) mod 8.

I tried it in both languages Java and C, and they gave me a -5 result when I was expecting 3.

Why is this happening? Can a modulus be negative? And what should I change to get the correct result?

Java code

public class Example {
    public static void main(String[] args) {
        int x;
        x = -5%8;
        System.out.println(x);
    }
}

C code

int main(){
    int x;
    x = -5%8;
    printf("%d", x);
}

OUTPUTS

回答1:

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


回答2:

Why is this happening?

It's defined that way in C. % is the remainder operator. Per 6.5.5 Multiplicative Operators, paragraph 6 of the C Standard:

When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a ; otherwise, the behavior of both a/b and a%b is undefined.

It's also defined that way in Java. Per 15.17.3. Remainder Operator % of the Java 8 specification:

The binary % operator is said to yield the remainder of its operands from an implied division; the left-hand operand is the dividend and the right-hand operand is the divisor.

In C and C++, the remainder operator accepts only integral operands, but in the Java programming language, it also accepts floating-point operands.

The remainder operation for operands that are integers after binary numeric promotion (§5.6.2) produces a result value such that (a/b)*b+(a%b) is equal to a.

This identity holds even in the special case that the dividend is the negative integer of largest possible magnitude for its type and the divisor is -1 (the remainder is 0).

It follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the magnitude of the result is always less than the magnitude of the divisor.

So the remainder - the result of the % operator - must have the same sign as the dividend - the first value, before the % operator.



回答3:

It's not about the number is being negative or positive. 5%8 would return 5 because that's how mod operator works.

If you want 3 or -3 as result, you need to do 8%5 or -8%5 respectively.

Here's the documentation, this is what it says:

divides one operand by another and returns the remainder as its result.

So, it's always fir operand divided by second. Also, if you want positive result then you can do:

Math.abs(-8%5);


回答4:

You should have look at Stack Overflow question What's the difference between “mod” and “remainder”?.

To get mod positive do -


C Code -

int main(){
    int a, b;
    scanf("%d %d", &a, &b);
    int rem  = a%b;
    if (rem<0)
        rem += b;
    printf("%d", rem);
    return 0;
}

Similar would be the Java code.

Basically the approach is that first take mod irrespective of the sign (to make the magnitude of the answer less than that of divisor), and if the answer is negative then add the divisor to it.



标签: java c math mod