title: Why does C++ output negative numbers when using modulo?
body: <p><strong>Math</strong>:</p>
  
  <p>If you have an equation like this:</p>
  
  <pre><code>x = 3 mod 7
  </code></pre>
  
  <p>x could be ... -4, 3, 10, 17, ..., or more generally:</p>
  
  <pre><code>x = 3 + k * 7
  </code></pre>
  
  <p>where k can be any integer. I don't know of a modulo operation is defined for math, but the  factor ring certainly is.</p>
  
  <p><strong>Python</strong>:</p>
  
  <p>In Python, you will always get non-negative values when you use <code>%</code> with a positive <code>m</code>:</p>
  
  <pre><code>#!/usr/bin/python
  # -*- coding: utf-8 -*-
  
  m = 7
  
  for i in xrange(-8, 10 + 1):
      print(i % 7)
  </code></pre>
  
  <p>Results in:</p>
  
  <pre><code>6    0    1    2    3    4    5    6    0    1    2    3    4    5    6    0    1    2    3
  </code></pre>
  
  <p><strong>C++:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int main(){
      int m = 7;
  
      for(int i=-8; i &lt;= 10; i++) {
          cout &lt;&lt; (i % m) &lt;&lt; endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Will output:</p>
  
  <pre><code>-1    0    -6    -5    -4    -3    -2    -1    0    1    2    3    4    5    6    0    1    2    3    
  </code></pre>
  
  <p><strong>ISO/IEC 14882:2003(E) - 5.6 Multiplicative operators:</strong></p>
  
  <blockquote>
    <p>The binary / operator yields the quotient, and the binary % operator
    yields the remainder from the division of the first expression by the
    second. If the second operand of / or % is zero the behavior is
    undefined; otherwise (a/b)*b + a%b is equal to a. If both operands are
    nonnegative then the remainder is nonnegative; if not, <strong>the sign of the
    remainder is implementation-defined 74)</strong>.</p>
  </blockquote>
  
  <p>and</p>
  
  <blockquote>
    <p>74) According to work underway toward the revision of ISO C, the
    preferred algorithm for integer division follows the rules defined in
    the ISO Fortran standard, ISO/IEC 1539:1991, in which the quotient is
    always rounded toward zero.</p>
  </blockquote>
  
  <p>Source: <a href="http://cs.nyu.edu/courses/summer12/CSCI-GA.2110-001/downloads/C++%20Standard%202003.pdf" rel="noreferrer">ISO/IEC 14882:2003(E)</a> </p>
  
  <p>(I couldn't find a free version of <code>ISO/IEC 1539:1991</code>. Does anybody know where to get it from?)</p>
  
  <p>The operation seems to be defined like this:</p>
  
  <p><img src="https://i.stack.imgur.com/8p2MN.png" alt="enter image description here"></p>
  
  <p><strong>Question</strong>: </p>
  
  <p>Does it make sense to define it like that? </p>
  
  <p>What are arguments for this specification? Is there a place where the people who create such standards discuss about it? Where I can read something about the reasons why they decided to make it this way?</p>
  
  <p>Most of the time when I use modulo, I want to access elements of a datastructure. In this case, I have to make sure that mod returns a non-negative value. So, for this case, it would be good of mod always returned a non-negative value. 
  (Another usage is the <a href="http://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations" rel="noreferrer">Euclidean algorithm</a>. As you could make both numbers positive before using this algorithm, the sign of modulo would matter.)</p>
  
  <p><strong>Additional material</strong>: </p>
  
  <p>See <a href="http://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation" rel="noreferrer">Wikipedia</a> for a long list of what modulo does in different languages.</p>