knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" ) library(fracture) # remotes::install_github("GuangchuangYu/badger") library(badger) library(MASS) library(fractional)
r badge_cran_release(color = "brightgreen")
r badge_lifecycle("stable")
r badge_license(color = "blueviolet")
r badge_github_actions(action = "R-CMD-check")
r badge_dependencies()
Convert decimals to fractions in R
You can install the released version of fracture from CRAN with:
``` {r eval = FALSE} install.packages("fracture")
or the development version from [GitHub](https://github.com/rossellhayes/fracture) with: ``` {r eval = FALSE} # install.packages("remotes") remotes::install_github("rossellhayes/fracture")
fracture converts decimals into fractions.
fracture(0.5) fracture((1:11) / 12)
fracture
sfracture
s are implemented using an S3 class. This means we can perform mathematical operations on them like real fractions.
fracture(0.25) * 2 fracture(0.25) + fracture(1/6)
fracture
sfrac_style()
uses Unicode to provide stylish formatting for inline fractions.
`r frac_style(pi, mixed = TRUE, max_denom = 500)`
r frac_style(pi, mixed = TRUE, max_denom = 500)
Additional arguments help you get exactly the result you expect:
fracture((1:12) / 12, denom = 100)
fracture((1:12) / 12, common_denom = TRUE)
fracture(1 / (2:12), base_10 = TRUE)
fracture(sqrt(1 / (1:12)), max_denom = 100)
fracture((1:9) / 3, mixed = TRUE)
For more advanced work, you may prefer to work with a fraction matrix:
frac_mat((1:11) / 12)
frac_mat()
accepts all the same arguments as fracture()
.
When mixed fractions are used, frac_mat()
has three rows:
frac_mat((1:9) / 3, mixed = TRUE, common_denom = TRUE)
Use fracture to find the best approximations of π for each maximum denominator.
unique(purrr::map_chr(1:50000, ~ fracture(pi, max_denom = .x)))
Isn't is interesting that there's such a wide gap between r frac_style(355/113)
and r frac_style(103993/33102)
?
r emo::ji("rocket")
fracture is implemented using optimized C++ with Rcpp and S3 methods.
This allows it to run faster than alternatives like MASS::fractions()
or fractional::fractional()
.*
x <- round(runif(1000, 1, 1e6)) / round(runif(1000, 1, 1e6)) # Performance with a single value single_benchmark <- bench::mark( print(fracture(x[1])), print(MASS::fractions(x[1])), print(fractional::fractional(x[1])), check = FALSE, relative = TRUE, filter_gc = FALSE ) vector_benchmark <- bench::mark( print(fracture(x)), print(MASS::fractions(x)), print(fractional::fractional(x)), check = FALSE, relative = TRUE, filter_gc = FALSE )
# Performance with a single value single_benchmark # Performance with a vector of length 1000 vector_benchmark
* fractional()
does not compute a decimal's fractional equivalent until it is printed.
Therefore, benchmarking the time to print provides a fairer test of the three packages' capabilities.
Hex sticker fonts are Source Sans and Hasklig.
Please note that fracture is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.