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