Modulo operation with negative numbers

前端 未结 12 1432
旧巷少年郎
旧巷少年郎 2020-11-22 06:10

In a C program i was trying the below operations(Just to check the behavior )

 x = 5 % (-3);
 y = (-5) % (3);
 z = (-5) % (-3); 

printf(\"%d ,%d ,%d\", x, y         


        
相关标签:
12条回答
  • 2020-11-22 06:31

    The % operator in C is not the modulo operator but the remainder operator.

    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 while with a modulo operator the sign of the result is the same as the divisor.

    C defines the % operation for a % b as:

      a == (a / b * b) + a % b
    

    with / the integer division with truncation towards 0. That's the truncation that is done towards 0 (and not towards negative inifinity) that defines the % as a remainder operator rather than a modulo operator.

    0 讨论(0)
  • 2020-11-22 06:36

    The result of Modulo operation depends on the sign of numerator, and thus you're getting -2 for y and z

    Here's the reference

    http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_14.html

    Integer Division

    This section describes functions for performing integer division. These functions are redundant in the GNU C library, since in GNU C the '/' operator always rounds towards zero. But in other C implementations, '/' may round differently with negative arguments. div and ldiv are useful because they specify how to round the quotient: towards zero. The remainder has the same sign as the numerator.

    0 讨论(0)
  • 2020-11-22 06:40

    I don't think there isn't any need to check if the number is negative.

    A simple function to find the positive modulo would be this -

    Edit: Assuming N > 0 and N + N - 1 <= INT_MAX

    int modulo(int x,int N){
        return (x % N + N) %N;
    }
    

    This will work for both positive and negative values of x.

    Original P.S: also as pointed out by @chux, If your x and N may reach something like INT_MAX-1 and INT_MAX respectively, just replace int with long long int.

    And If they are crossing limits of long long as well (i.e. near LLONG_MAX), then you shall handle positive and negative cases separately as described in other answers here.

    0 讨论(0)
  • 2020-11-22 06:44

    Modulus operator gives the remainder. Modulus operator in c usually takes the sign of the numerator

    1. x = 5 % (-3) - here numerator is positive hence it results in 2
    2. y = (-5) % (3) - here numerator is negative hence it results -2
    3. z = (-5) % (-3) - here numerator is negative hence it results -2

    Also modulus(remainder) operator can only be used with integer type and cannot be used with floating point.

    0 讨论(0)
  • 2020-11-22 06:45

    In Mathematics, where these conventions stem from, there is no assertion that modulo arithmetic should yield a positive result.

    Eg.

    1 mod 5 = 1, but it can also equal -4. That is, 1/5 yields a remainder 1 from 0 or -4 from 5. (Both factors of 5)

    Similarly, -1 mod 5 = -1, but it can also equal 4. That is, -1/5 yields a remainder -1 from 0 or 4 from -5. (Both factors of 5)

    For further reading look into equivalence classes in Mathematics.

    0 讨论(0)
  • 2020-11-22 06:45

    I believe it's more useful to think of mod as it's defined in abstract arithmetic; not as an operation, but as a whole different class of arithmetic, with different elements, and different operators. That means addition in mod 3 is not the same as the "normal" addition; that is; integer addition.

    So when you do:

    5 % -3
    

    You are trying to map the integer 5 to an element in the set of mod -3. These are the elements of mod -3:

    { 0, -2, -1 }
    

    So:

    0 => 0, 1 => -2, 2 => -1, 3 => 0, 4 => -2, 5 => -1
    

    Say you have to stay up for some reason 30 hours, how many hours will you have left of that day? 30 mod -24.

    But what C implements is not mod, it's a remainder. Anyway, the point is that it does make sense to return negatives.

    0 讨论(0)
提交回复
热议问题