Arbitrary-Precision Integer and Floating-Point Mathematics

Classes for storing and manipulating arbitrary-precision integer vectors and high-precision floating-point vectors. These extend the range and precision of the 'integer' and 'double' data types found in R. This package utilizes the 'Boost.Multiprecision' C++ library. It is specifically designed to work well with the 'tidyverse' collection of R packages.


bignum

Lifecycle:experimental CRANstatus R buildstatus Coveragestatus

bignum provides numeric vectors with greater precision than R atomic numeric vectors.

  • biginteger() stores any integer (i.e. arbitrary precision).
  • bigfloat() stores 50 decimal digits of precision.

They prioritize precision over performance, so computations are slower than those using integer() or double().

Under the hood, bignum uses the cpp_int and cpp_bin_float_50 data types from the Boost.Multiprecision C++ library.

Installation

You can install the released version of bignum from CRAN with:

install.packages("bignum")

Or you can install the development version from GitHub:

# install.packages("remotes")
remotes::install_github("davidchall/bignum")

Usage

Before starting, we’ll increase the displayed precision so we can see the benefits of bignum.

options(digits = 20)
options(bignum.sigfig = 50)
options(bignum.max_dec_width = 52)

Arbitrary-precision integer vector

The limited precision of atomic vectors introduces errors when working with very large integers. As an example, let’s calculate the factorial of 23. In base R, we’d calculate:

factorial(23)
#> [1] 25852016738884978212864

The factorial of 23 includes a factor of 10, and so the final digit must be zero. Using biginteger() yields the correct result:

prod(biginteger(1:23))
#> <biginteger[1]>
#> [1] 25852016738884976640000

High-precision floating-point vector

bigfloat() vectors support much higher precision than double() vectors:

1 / 3
#> [1] 0.33333333333333331483
bigfloat(1) / 3
#> <bigfloat[1]>
#> [1] 0.33333333333333333333333333333333333333333333333333

However, you need to be careful not to limit the precision accidentally:

bigfloat(1 / 3)
#> <bigfloat[1]>
#> [1] 0.333333333333333

Please note that the bignum project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("bignum")

0.3.2 by David Hall, 3 years ago


https://davidchall.github.io/bignum/, https://github.com/davidchall/bignum


Report a bug at https://github.com/davidchall/bignum/issues


Browse source code at https://github.com/cran/bignum


Authors: David Hall [aut, cre, cph]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports rlang, vctrs

Suggests knitr, pillar, rmarkdown, testthat

Linking to BH, cpp11


Suggested by friendlynumber, ipaddress, raddr.


See at CRAN