Skip to content

Commit

Permalink
Use floor() instead of round() in %% (#365)
Browse files Browse the repository at this point in the history
* Fix `%/%` and `%%`

* Enhance tests `%/%` and `%%`
  • Loading branch information
UchidaMizuki authored Feb 21, 2024
1 parent 928e883 commit 23751a7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion R/arith.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Ops.units <- function(e1, e2) {

if (mod) {
div <- e1 / e2
int <- round(div)
int <- floor(div)
if (.Generic == "%/%") {
return(int)
} else {
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test_arith.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ test_that("we can undo logatithms", {
})

test_that("%/% and %% work", {
x <- set_units(3, m^2)
x <- set_units(1:5, m^2)
y <- set_units(1.4, foot)

expect_true(all.equal(x, y * (x %/% y) + x %% y))
Expand All @@ -193,6 +193,8 @@ test_that("%/% and %% work", {
expect_equal(set_units(x, foot^2) %% drop_units(y), z)
expect_equal(set_units(x, foot^2) %% set_units(drop_units(y), 1), z)
expect_error(drop_units(set_units(x, foot^2)) %% y)

expect_true(all(x %% y >= set_units(0, m^2)))
})

#test_that("%*% work", {
Expand Down

0 comments on commit 23751a7

Please sign in to comment.