atorus-research / Tplyr

Home Page:https://atorus-research.github.io/Tplyr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The ut_round() function rounds differently from SAS

alexhomel opened this issue · comments

Description

The ut_round() function produces unexpected results with options(tplyr.IBMRounding = TRUE).

Tplyr/R/utils.R

Lines 313 to 321 in 6876054

ut_round <- function(x, n=0)
{
# x is the value to be rounded
# n is the precision of the rounding
scale <- 10^n
y <- trunc(x * scale + sign(x) * 0.5) / scale
# Return the rounded number
return(y)
}

Steps to Reproduce

Let's count the mean using a test vector and round it to 3 decimals using ut_round():

> vec <- c(2.64, -3.20, -2.88, 2.95)

> mvec <- mean(vec)

> mvec
[1] -0.1225

> ut_round(mvec, 3)
[1] -0.122

The expected value is -0.123 but I'm getting -0.122

Closed via #126