Skip to content

Commit

Permalink
Address floating point imprecision in round_half_up() #396 (#399)
Browse files Browse the repository at this point in the history
* fix #396
  • Loading branch information
JJSteph authored Aug 18, 2020
1 parent 4adef4e commit cbafdd1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Bug fixes

* Fixed rounding issue in round_half_up() function (#396, thanks to @JJSteph)

* Warnings for incomplete argument names are fixed (fix #367, thanks to @pabecerra for reporting and @billdenney for fixing)

* 3-way tabyls with factors have columns and rows sorted in the correct order, by factor level (#379).
Expand Down
2 changes: 1 addition & 1 deletion R/round_half_up.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
round_half_up <- function(x, digits = 0) {
posneg <- sign(x)
z <- abs(x) * 10 ^ digits
z <- z + 0.5
z <- z + 0.5 + sqrt(.Machine$double.eps)
z <- trunc(z)
z <- z / 10 ^ digits
z * posneg
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ test_that("round_half_up works", {
expect_equal(round_half_up(0.5, 0), 1)
expect_equal(round_half_up(1.125, 2), 1.13)
expect_equal(round_half_up(1.135, 2), 1.14)
})
expect_equal(round_half_up(2436.845, 2), 2436.85)
})

0 comments on commit cbafdd1

Please sign in to comment.