2

To be clear, I understand the behavior of modulo in the following 3 scenarios.

I understand why 16 % 3 = 1.

I understand why -16 % 3 = 2. (This one can be tricky, I realize)

I understand why 3 % 16 = 3.

But I do not understand why -3 % 16 = 13. Nor do I understand why some online sources (and some programming languages) give the answer as -3 instead. I'm a beginner, so a practical answer would be most useful to me. Thank you!

Flok
  • 21
  • 1
  • ["The modulo operator always yields a result with the same sign as its second operand"](https://stackoverflow.com/questions/4432208/what-is-the-result-of-in-python) – sshashank124 Jan 05 '20 at 05:44
  • 2
    [Wiki](https://en.wikipedia.org/wiki/Modulo_operation#In_programming_languages) shows a table for different languages – MBo Jan 05 '20 at 05:49
  • If you understand why `-16 % 3 = 2` then you should understand why `-3 % 16 = 13` – President James K. Polk Jan 05 '20 at 15:24
  • Does this answer your question? [What is the result of % in Python?](https://stackoverflow.com/questions/4432208/what-is-the-result-of-in-python) – Peter O. Jan 06 '20 at 06:22
  • "But I do not understand why `-3 % 16 = 13` [and not `-3`]." **For the same reason** that `-16 % 3 = 2` (and not `-1`). The **only** thing that changed is the number of times that the dividend goes into the divisor, but the entire point of `%` is to ignore that part, and focus on **how to present** the fact that it doesn't divide evenly. – Karl Knechtel Jan 12 '23 at 00:35

3 Answers3

2

in python modulus will use this method to get the result

mod(a,n) = a - {n * Floor(a/n)}

So if it is a -3 % 16 it will be like this :

mod(-3,16) = -3 - {16 * Floor(-3/16)}
           = -3 - {16 * Floor(-0.1875)}
           = -3 - {16 * -1}
           = -3 - (-16)
           = 13

please check for a great explanation here

z.io
  • 166
  • 4
0

    Observe carefully, it is as simple as that.
    Your issue is as follows:-
    Why -3%16 = 13?
    Before starting my answer to your question, kindly observe the following as well.
    2 > 0  | -2  -1 | -2  -2 | -2 = -2
    2 > -3 | -2 Also notice that 0 is not taken into consideration as the quotient to -3 % 16 because it is trivial to any case in this particular.

    Dividend = -3
    Divisor = 16
    Quotient = -1
    Remainder = 13 ==> This is the answer to your issue.

    Immediately, you are going to think how?
    Explanation to your issue as per college mathematics, I hope you have knowledge on addition modulo and multiplication modulo:-
    Your issue falls under multiplication modulo.
    Analytical Answer based on the Conceptual Understanding:-
                      16)-03(-1
                         -16
    Sign Convertion:    (+)
    ------------------------------------
                 -03+16 = 13
    ------------------------------------

    If you are interested and familiar in understanding mathematics conceptually, kindly refer the Degree Mathematics Text Book containing the concepts of Addition Modulo  as well as Multiplication Modulo in Group Theory and Vector Theory.

    In those textbooks, modulos look like these:-

    A+kB
    A-kB
    A*kB

    k is called the respective modulo ranges over Integer Set.
    A and B are operands to perform the execution.

    If you refer those books without fail then you have a basic knowledge of how to compute modulos mathematically.

    Still you faced any issues feel free to comment on my answer.


    >>> # ******************
    >>> # Technical Answer:-
    >>> # ******************
    >>> x = -3
    >>> x
    -3
    >>> y = 16
    >>> y
    16
    >>> x % y
    13
    >>> # is same as follows:-
    >>> z = x % y
    >>> z
    13
    >>> # only the difference between the previous answer and this answer is as follows:-
    >>> # if you use like x % y as the above, whenever you require the resultant value of this computation for further actions (if applicable), you must always perform this task. Otherwise, Python Interpreter raises either ValueError or NameError based on the context of the logical code.
    >>> # if you use like z = x % y as the above, and you also require the computational value, then you just call the object z itself, automatically it gets the result of the operation x % y. Also you can reuse it any number of times.

ds_vvk
  • 11
  • 2
  • Sorry, but this answer is fairly poor. Besides being very difficult to read, it consists of just multiple restatements of the fact that `-3%16==3`. The op knows that and is asking why that's so. – President James K. Polk Jan 05 '20 at 15:28
-1

To calculate you can use

     =  -3 % 16
     =  (13-16) % 16
     =   13%16 - 0
     =   13
Deepak Patankar
  • 3,076
  • 3
  • 16
  • 35