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...