Fix ceiling floating point error

Problem:

ceiling(31)
#31
ceiling(31/60*60)
#32

What is the correct way to fix such errors?

Doing multiplication before division is not an option; my code looks something like this:

x <- 31/60
...
y <- ceiling(x*60)

I am going to make a new function:

ceil <- function(x) {
  ceiling(signif(x))
}

But I'm new to R, maybe there is a better way.

UPDATE
Sorry, I did not provide more details, I have the same problem in different parts of my code for various reasons, but always with a ceiling.

I am aware of a rounding error in floating point calculation. Perhaps the title of the question can be improved. I do not want to correct the inaccuracy of the ceiling function , what I want to do, maybe, on the contrary, make the ceiling less accurate. A way to tell R to ignore numbers that clearly sound:

options(digits=17)
31/60*60
#31.000000000000004

, -, , , .

+4
1

, , The Data Munger Guru tagline, : " , ? , , . "

, , " +/- ", , "", .. - , .

, , , . , , trunc(x/60)->y, trunc(y*60), :-). , y<-round(x/60*60) +1, jhoward. , , , .

+1

Source: https://habr.com/ru/post/1568933/


All Articles