Avoid division by zero in average calculation