jonkeane commented on a change in pull request #11176: URL: https://github.com/apache/arrow/pull/11176#discussion_r711753899
########## File path: r/tests/testthat/test-dplyr.R ########## @@ -989,19 +989,86 @@ test_that("sign()", { ) }) -test_that("ceiling(), floor(), trunc()", { +test_that("ceiling(), floor(), trunc(), round()", { df <- tibble(x = c(-1, -0.55, -0.5, -0.1, 0, 0.1, 0.5, 0.55, 1, NA, NaN)) expect_dplyr_equal( input %>% mutate( c = ceiling(x), f = floor(x), - t = trunc(x) + t = trunc(x), + r = round(x) ) %>% collect(), df ) + + # with digits set to 1 + expect_dplyr_equal( + input %>% + filter(x %% 0.5 == 0) %>% + mutate(r = round(x, 1)) %>% + collect(), + df + ) + + # with digits set to -1 + expect_dplyr_equal( + input %>% + mutate( + rd = round(floor(x * 111), -1), # double + y = ifelse(is.nan(x), NA_integer_, x), + ri = round(as.integer(y * 111), -1) # integer (with the NaN removed) + ) %>% + collect(), + df + ) + + # round(x, -2) is equivalent to round_to_multiple(x, 100) + expect_equal( + Table$create(x = 1111.1) %>% + mutate(r = round(x, -2)) %>% + collect(), + Table$create(x = 1111.1) %>% + mutate(r = arrow_round_to_multiple(x, options = list(multiple = 100))) %>% + collect() + ) + + skip_on_os("windows") # float representation error might cause inconsistency + + expect_dplyr_equal( + input %>% + mutate(r = round(x, 1)) %>% + collect(), + df + ) + + # verify that round mode HALF_TO_EVEN which is what the round() binding uses + # yields results consistent with R... + expect_equal( + as.vector( + call_function( + "round", + Array$create(df$x), + options = list(ndigits = 1L, round_mode = RoundMode$HALF_TO_EVEN) + ) + ), + round(df$x, 1) + ) + # ... but round mode HALF_TOWARDS_ZERO does not. Review comment: Should we include a (truncated) version of that explanation in a comment here? I see that's what it's doing — but I'm not sure I would have without this comment. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org