Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

floating point number comparisons #17

Open
achubaty opened this issue Jul 9, 2024 · 2 comments
Open

floating point number comparisons #17

achubaty opened this issue Jul 9, 2024 · 2 comments

Comments

@achubaty
Copy link

achubaty commented Jul 9, 2024

This is a useful package, I'm sad I hadn't started using it sooner!

Have you considered using more robust comparisons to deal with floating point number problems? See below.

> x1 <- 0.5 - 0.3
> x2 <- 0.3 - 0.1
> 
> x1 %[]% c(0.2, 0.6)
[1] TRUE
> x2 %[]% c(0.2, 0.6)
[1] FALSE

My fpCompare package deals with this for the basic relational operators, but having equivalent functionality to test intervals/ranges may be useful. Although, I recognize that this kind of change may break backwards compatibility / reproducibility without a mechanism (package option?) to use the alternate behaviour.

@psolymos
Copy link
Owner

@achubaty thanks for raising this issue. This is indeed a problem.

If I import the operators from fpCompare, and setting an option to intrval.use_fpCompare=FALSE for backwards compatibility. When this option is set to TRUE, I would use the fpCompare functions inside the internals with the set fpCompare.tolerance option.

@psolymos
Copy link
Owner

psolymos commented Sep 21, 2024

Hi @achubaty intrval treats operators more holistically than numeric to numeric, and I get errors when I simply replace >= with %>=% etc. That is because:

1 %<=% 2
[1] TRUE

"a" %<=% "b"
Error in y + getOption("fpCompare.tolerance") : 
  non-numeric argument to binary operator

"a" <= "b"
[1] TRUE

I can do something like this in intrval:

"%>=%" <- function(e1, e2) {
    if (is.numeric(e1) && is.numeric(e2))
        fpCompare::`%>=%`(e1, e2) else base::`>=`(e1, e2)
}

but I was wondering if this is an improvement that should be made in fpCompare instead?

See: master...fpCompare

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants