<p>Can you please tell me how much is <code>(-2) % 5</code>?
According to my Python interpreter is 3, but do you have a wise explanation for this?</p>

<p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>

[{'answer_id': 43780, 'author': 'Tnilsson', 'author_id': 4165, 'author_profile': 'https://Stackoverflow.com/users/4165', 'pm_score': 4, 'selected': False, 'text': '<p>Your Python interpreter is correct.\nOne (stupid) way of calculating a modulus is to subtract or add the modulus until the resulting value is between 0 and (modulus − 1).</p>\n\n<p>e.g.:\n13 mod 5 = (13 − 5) mod 5 = (13 − 10) mod 5 = 3</p>\n\n<p>or in your case: −2 mod 5 = (−2 + 5) mod 5 = 3</p>\n'}, {'answer_id': 43781, 'author': 'Jason Baker', 'author_id': 2147, 'author_profile': 'https://Stackoverflow.com/users/2147', 'pm_score': 0, 'selected': False, 'text': "<p>Well, -2 divided by 5 would be 0 with a remainder of 3.  I don't believe that should be very platform dependent, but I've seen stranger things.</p>\n"}, {'answer_id': 43782, 'author': 'Matt Dillard', 'author_id': 863, 'author_profile': 'https://Stackoverflow.com/users/863', 'pm_score': 0, 'selected': False, 'text': '<p>It is indeed 3.  In <a href="http://en.wikipedia.org/wiki/Modular_arithmetic" rel="nofollow noreferrer">modular arithmetic</a>, a modulus is simply the remainder of a division, and the remainder of -2 divided by 5 is 3.</p>\n'}, {'answer_id': 43783, 'author': 'martinatime', 'author_id': 1353, 'author_profile': 'https://Stackoverflow.com/users/1353', 'pm_score': 4, 'selected': False, 'text': '<p>The result of the modulus operation on negatives seems to be programming language dependent and here is a listing <a href="http://en.wikipedia.org/wiki/Modulo_operation" rel="noreferrer">http://en.wikipedia.org/wiki/Modulo_operation</a></p>\n'}, {'answer_id': 43785, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 2, 'selected': False, 'text': "<p>Well, 0 % 5 should be 0, right?</p>\n\n<p>-1 % 5 should be 4 because that's the next allowed digit going in the reverse direction (i.e., it can't be 5, since that's out of range).</p>\n\n<p>And following along by that logic, -2 must be 3.</p>\n\n<p>The easiest way to think of how it will work is that you keep adding or subtracting 5 until the number falls between 0 (inclusive) and 5 (exclusive).</p>\n\n<p>I'm not sure about machine dependence - I've never seen an implementation that was, but I can't say it's never done.</p>\n"}, {'answer_id': 43794, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 5, 'selected': True, 'text': '<p>By the way: most programming languages would disagree with Python and give the result <code>-2</code>. Depending on the interpretation of modulus this is correct. However, the most agreed-upon mathematical definition states that the modulus of <em>a</em> and <em>b</em> is the (strictly positive) rest <em>r</em> of the division of <em>a</em> / <em>b</em>. More precisely, 0 &lt;= <em>r</em> &lt; <em>b</em> by definition.</p>\n'}, {'answer_id': 43799, 'author': 'Ozgur Ozcitak', 'author_id': 976, 'author_profile': 'https://Stackoverflow.com/users/976', 'pm_score': 0, 'selected': False, 'text': '<p>The result depends on the language. Python returns the sign of the divisor, where for example c# returns the sign of the dividend (ie. -2 % 5 returns -2 in c#).</p>\n'}, {'answer_id': 43837, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>One explanation might be that negative numbers are stored using <a href="http://en.wikipedia.org/wiki/Two%27s_complement" rel="nofollow noreferrer">2\'s complement</a>. When the python interpreter tries to do the modulo operation it converts to unsigned value. As such instead of doing (-2) % 5 it actually computes 0xFFFF_FFFF_FFFF_FFFD % 5 which is 3. </p>\n'}, {'answer_id': 43863, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 2, 'selected': False, 'text': '<p>As explained in other answers, there are many choices for a modulo operation with negative values. In general different languages (and different machine architectures) will give a different result.</p>\n\n<p>According to the <a href="http://docs.python.org/ref/binary.html" rel="nofollow noreferrer">Python reference manual</a>,</p>\n\n<blockquote>\n  <p>The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand.</p>\n</blockquote>\n\n<p>is the choice taken by Python. Basically modulo is defined so that this always holds:</p>\n\n<pre><code>x == (x/y)*y + (x%y)\n</code></pre>\n\n<p>so it makes sense that (-2)%5 = -2 - (-2/5)*5 = 3</p>\n'}, {'answer_id': 43916, 'author': 'Jared Updike', 'author_id': 2543, 'author_profile': 'https://Stackoverflow.com/users/2543', 'pm_score': 0, 'selected': False, 'text': '<p>Be careful not to rely on this mod behavior in C/C++ on all OSes and architectures. If I recall correctly, I tried to rely on C/C++ code like</p>\n\n<pre><code>float x2 = x % n;\n</code></pre>\n\n<p>to keep x2 in the range from 0 to n-1 but negative numbers crept in when I would compile on one OS, but things would work fine on another OS. This made for an evil time debugging since it only happened half the time!</p>\n'}, {'answer_id': 3224825, 'author': 'tzot', 'author_id': 6899, 'author_profile': 'https://Stackoverflow.com/users/6899', 'pm_score': 3, 'selected': False, 'text': '<p>Like the documentation says in <a href="http://docs.python.org/reference/expressions.html#binary" rel="noreferrer">Binary arithmetic operations</a>, Python assures that:</p>\n\n<blockquote>\n  <p>The integer division and modulo operators are connected by the following identity: <code>x == (x/y)*y + (x%y)</code>. Integer division and modulo are also connected with the built-in function divmod(): <code>divmod(x, y) == (x/y, x%y)</code>.</p>\n</blockquote>\n\n<p>And truly,</p>\n\n<pre><code>&gt;&gt;&gt; divmod(-2, 5)\n(-1, 3).\n</code></pre>\n\n<p>Another way to visualize the uniformity of this method is to calculate <code>divmod</code> for a small sequence of numbers:</p>\n\n<pre><code>&gt;&gt;&gt; for number in xrange(-10, 10):\n...     print divmod(number, 5)\n...\n(-2, 0)\n(-2, 1)\n(-2, 2)\n(-2, 3)\n(-2, 4)\n(-1, 0)\n(-1, 1)\n(-1, 2)\n(-1, 3)\n(-1, 4)\n(0, 0)\n(0, 1)\n(0, 2)\n(0, 3)\n(0, 4)\n(1, 0)\n(1, 1)\n(1, 2)\n(1, 3)\n(1, 4)\n</code></pre>\n'}, {'answer_id': 5203460, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>There seems to be a common confusion between the terms "modulo" and "remainder".</p>\n\n<p>In math, a remainder should <em>always</em> be defined consistent with the quotient, so that if <code>a / b == c rem d</code> then <code>(c * b) + d == a</code>. Depending on how you round your quotient, you get different remainders.</p>\n\n<p>However, modulo should always give a result <code>0 &lt;= r &lt; divisor</code>, which is only consistent with round-to-minus-infinity division if you allow negative integers. If division rounds towards zero (which is common), modulo and remainder are only equivalent for non-negative values.</p>\n\n<p>Some languages (notably C and C++) don\'t define the required rounding/remainder behaviours and <code>%</code> is ambiguous. Many define rounding as towards zero, yet use the term modulo where remainder would be more correct. Python is relatively unusual in that it rounds to negative infinity, so modulo and remainder are equivalent.</p>\n\n<p>Ada rounds towards zero IIRC, but has both <code>mod</code> and <code>rem</code> operators.</p>\n\n<p>The C policy is intended to allow compilers to choose the most efficient implementation for the machine, but IMO is a false optimisation, at least these days. A good compiler will probably be able to use the equivalence for optimisation wherever a negative number cannot occur (and almost certainly if you use unsigned types). On the other hand, where negative numbers can occur, you almost certainly care about the details - for portability reasons you have to use very carefully designed overcomplex algorithms and/or checks to ensure that you get the results you want irrespective of the rounding and remainder behaviour.</p>\n\n<p>In other words, the gain for this "optimisation" is mostly (if not always) an illusion, whereas there are very real costs in some cases - so it\'s a false optimisation.</p>\n'}]
['https://Stackoverflow.com/questions/43775', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1876/']