title: std:: accumulate division by zero
body: <p>I do not understand why I get a division by zero in the following code.</p>
  <pre><code>template &lt;typename T&gt;
  typename std::iterator_traits&lt;T&gt;::value_type avg_abs_difference(T first, T second)
  {
      using Type = typename std::iterator_traits&lt;T&gt;::value_type;
      if(first == second)return 0;
      Type oldValue = *first;
      Type count = 0;
      Type res = std::accumulate(std::next(first), second, 0, [&amp;](Type a, Type b)
                             {
                              Type result = a + std::abs(b- oldValue);
                              oldValue = b;
                              ++count;
                              return  result; }
                              )/ count;
      return res ;
  }
  </code></pre>
  <p>but when I pull the division outward like this, it does not occur.</p>
  <pre><code>template &lt;typename T&gt;
  typename std::iterator_traits&lt;T&gt;::value_type avg_abs_difference(T first, T second)
  {
      using Type = typename std::iterator_traits&lt;T&gt;::value_type;
      if(first == second)return 0;
      Type oldValue = *first;
      Type count = 0;
      Type res = std::accumulate(std::next(first), second, 0, [&amp;](Type a, Type b)
                             {
                              Type result = a + std::abs(b- oldValue);
                              oldValue = b;
                              ++count;
                              return  result; }
                              );
      return res / count;
  }
  </code></pre>