Fix infinite loops caused by rounding errors