-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
102 lines (69 loc) · 2.89 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# bignum
<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![CRAN status](https://www.r-pkg.org/badges/version/bignum)](https://CRAN.R-project.org/package=bignum)
[![R build status](https://github.com/davidchall/bignum/workflows/R-CMD-check/badge.svg)](https://github.com/davidchall/bignum/actions)
[![Coverage status](https://codecov.io/gh/davidchall/bignum/branch/master/graph/badge.svg)](https://app.codecov.io/gh/davidchall/bignum?branch=master)
<!-- badges: end -->
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](https://www.boost.org/doc/libs/1_77_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints/cpp_int.html) and [cpp_bin_float_50](https://www.boost.org/doc/libs/1_77_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/cpp_bin_float.html) data types from the Boost.Multiprecision C++ library.
## Installation
You can install the released version of bignum from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("bignum")
```
Or you can install the development version from GitHub:
``` r
# install.packages("remotes")
remotes::install_github("davidchall/bignum")
```
## Usage
```{r, echo = FALSE}
library(bignum)
```
Before starting, we'll increase the displayed precision so we can see the benefits of bignum.
```{r}
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:
```{r}
factorial(23)
```
The factorial of 23 includes a factor of 10, and so the final digit _must_ be zero.
Using `biginteger()` yields the correct result:
```{r}
prod(biginteger(1:23))
```
### High-precision floating-point vector
`bigfloat()` vectors support much higher precision than `double()` vectors:
```{r}
1 / 3
bigfloat(1) / 3
```
However, you need to be careful not to limit the precision accidentally:
```{r}
bigfloat(1 / 3)
```
---
Please note that the bignum project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html).
By contributing to this project, you agree to abide by its terms.