-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from psolymos/fpCompare
Accurate floating point comparisons with fpCompare
- Loading branch information
Showing
8 changed files
with
183 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
.options_set <- FALSE | ||
|
||
.onLoad <- function(libname, pkgname) { | ||
if (is.null(getOption("intrval_options"))) { | ||
.options_set <<- TRUE | ||
options("intrval_options" = list( | ||
use_fpCompare = TRUE | ||
)) | ||
} | ||
invisible(NULL) | ||
} | ||
|
||
.onUnload <- function(libpath) { | ||
if (.options_set) { | ||
options("intrval_options" = NULL) | ||
} | ||
invisible(NULL) | ||
} | ||
|
||
intrval_options <- function(...) { | ||
opar <- getOption("intrval_options") | ||
args <- list(...) | ||
if (length(args)) { | ||
if (length(args) == 1L && is.list(args[[1L]])) { | ||
npar <- args[[1L]] | ||
} else { | ||
npar <- opar | ||
npar[match(names(args), names(npar))] <- args | ||
} | ||
options("intrval_options" = npar) | ||
} | ||
invisible(opar) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
\name{intrval-options} | ||
\alias{intrval-options} | ||
\alias{intrval_options} | ||
\title{Global options for the intrval package} | ||
\usage{ | ||
intrval_options(...) | ||
} | ||
\arguments{ | ||
\item{...}{Options to set.} | ||
} | ||
\value{ | ||
When parameters are set by \code{intrval_options}, their former values are | ||
returned in an invisible named list. Such a list can be passed as an | ||
argument to \code{intrval_options} to restore the parameter values. | ||
Tags are the following: | ||
\itemize{ | ||
\item \code{use_fpCompare}: use the fpCompare package for the reliable comparison of floating point numbers. | ||
} | ||
} | ||
\description{ | ||
Options store and allow to set global values for the intrval functions. | ||
} | ||
\examples{ | ||
str(intrval_options()) | ||
|
||
x1 <- 0.5 - 0.3 | ||
x2 <- 0.3 - 0.1 | ||
|
||
# save old values and set the new one | ||
op <- intrval_options(use_fpCompare = FALSE) | ||
|
||
# this is the base R behavior | ||
x1 %[]% c(0.2, 0.6) # TRUE | ||
x2 %[]% c(0.2, 0.6) # FALSE | ||
|
||
# reset defaults | ||
intrval_options(op) | ||
|
||
# using fpCompare | ||
x1 %[]% c(0.2, 0.6) # TRUE | ||
x2 %[]% c(0.2, 0.6) # TRUE | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters