Package {fpCompare}


Type: Package
Title: Reliable Comparison of Floating Point Numbers
Version: 0.2.6
Description: Comparisons of floating point numbers are problematic due to errors associated with the binary representation of decimal numbers. Despite being aware of these problems, people still use numerical methods that fail to account for these and other rounding errors (this pitfall is the first to be highlighted in Circle 1 of Burns (2012) 'The R Inferno' https://www.burns-stat.com/pages/Tutor/R_inferno.pdf). This package provides new relational operators useful for performing floating point number comparisons with a set tolerance.
URL: https://fpcompare.predictiveecology.org, https://github.com/PredictiveEcology/fpCompare
BugReports: https://github.com/PredictiveEcology/fpCompare/issues
License: GPL-3
Depends: R (≥ 4.1)
Suggests: knitr, rmarkdown, testthat (≥ 3.0.0), withr
Encoding: UTF-8
Language: en-CA
VignetteBuilder: knitr
ByteCompile: yes
Config/Needs/coverage: covr
Config/Needs/website: pkgdown
Config/testthat/edition: 3
Config/roxygen2/version: 8.0.0
NeedsCompilation: no
Packaged: 2026-05-26 02:05:41 UTC; achubaty
Author: Alex M Chubaty ORCID iD [aut, cre], His Majesty the King in Right of Canada, as represented by the Minister of Natural Resources Canada [cph]
Maintainer: Alex M Chubaty <achubaty@for-cast.ca>
Repository: CRAN
Date/Publication: 2026-05-26 05:10:02 UTC

fpCompare: Reliable Comparison of Floating Point Numbers

Description

Comparisons of floating point numbers are problematic due to errors associated with the binary representation of decimal numbers. Despite being aware of these problems, people still use numerical methods that fail to account for these and other rounding errors (this pitfall is the first to be highlighted in Circle 1 of Burns (2012) 'The R Inferno' https://www.burns-stat.com/pages/Tutor/R_inferno.pdf). This package provides new relational operators useful for performing floating point number comparisons with a set tolerance.

Author(s)

Maintainer: Alex M Chubaty achubaty@for-cast.ca (ORCID)

Authors:

Other contributors:

See Also

Useful links:


Relational operators with tolerance

Description

Binary operators which allow the comparison of values in numeric vectors.

Usage

x %>=% y

x %>>% y

x %<=% y

x %<<% y

x %==% y

x %!=% y

Arguments

x

Any numeric object.

y

Any numeric object.

Details

These are similar to their counterparts in base, except a tolerance fpCompare.tolerance can be specified via options to account for floating point rounding errors:

fpCompare base
⁠%>=%⁠ >=
⁠%>>%⁠ >
⁠%<=%⁠ <=
⁠%<<%⁠ <
⁠%==%⁠ ==
⁠%!=%⁠ !=

Inspired by R FAQ 7.31 (https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f) and this post (https://stackoverflow.com/a/2769618/1380598).

Value

A logical vector indicating the result of the element-by-element comparison. The elements of shorter vectors are recycled as necessary.

Author(s)

Alex Chubaty

See Also

all.equal(), .Machine()

Examples

x1 <- 0.5 - 0.3
x2 <- 0.3 - 0.1
x1 == x2 ## FALSE on most machines
x1 %==% x2 ## TRUE everywhere
identical(all.equal(x1, x2), TRUE) ## TRUE everywhere

set.seed(123)
a <- 1:6
b <- jitter(1:6, 1e-7)
print(rbind(a, b), digits = 16)

b %<=% a
b %<<% a
b %>=% a
b %>>% a
b %==% a
b %!=% a