fix the averages wrt negative values and divisions by zero

it could be discussed whether it's better to throw a
std::runtime_error instead...
Merge pull request #390 from andlaus/fix_averages

fix the averages wrt negative values and divisions by zero