Factor out compensated summation

Fix #158.