Avoid rounding errors (fixes #1068)

For @piobpl to review