Fix a few bugs and change rounding mode to HALF_EVEN to match R implementation.