## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set(collapse = TRUE, comment = "#>") ## ----------------------------------------------------------------------------- 0.1 + 0.2 == 0.3 ## ----setup-------------------------------------------------------------------- library(decimal) ## ----------------------------------------------------------------------------- x <- decimal(c("1.20", "2.30", "3.40")) x ## ----------------------------------------------------------------------------- decimal(c(1L, 2L, NA_integer_)) decimal(c("1.20", "-0", "Infinity", "NaN")) ## ----------------------------------------------------------------------------- as.character(x) ## ----------------------------------------------------------------------------- decimal(c("1.2", "1.20", "1.234")) ## ----------------------------------------------------------------------------- decimal("1.2") == decimal("1.20") ## ----------------------------------------------------------------------------- decimal_from_double(0.1, scale = 25) ## ----------------------------------------------------------------------------- decimal("0.1") ## ----------------------------------------------------------------------------- withr::with_options( list(decimal.default_scale = 7L), as_decimal(0.002) ) ## ----------------------------------------------------------------------------- tibble::tibble( item = c("coffee", "bagel", "juice"), price = decimal(c("2.50", "1.25", "3.95")) ) ## ----------------------------------------------------------------------------- vctrs::vec_c(decimal("1.20"), 2L, NA) ## ----error = TRUE------------------------------------------------------------- try({ vctrs::vec_c(decimal("1.20"), 0.5) }) ## ----------------------------------------------------------------------------- decimal("1.20") + decimal("2.3") sum(decimal(c("1.20", "2.30", "3.40"))) mean(decimal(c("1", "2", "3"))) ## ----------------------------------------------------------------------------- -7 %% 4 # base R: floored, remainder takes divisor's sign decimal("-7") %% decimal("4") # decimal: truncated, remainder takes dividend's sign -7 %/% 4 decimal("-7") %/% decimal("4") ## ----------------------------------------------------------------------------- x <- decimal(c(NA_character_, "NaN", "sNaN", "Infinity", "-0")) x is.na(x) is.nan(x) number_class(x) ## ----------------------------------------------------------------------------- quantize(decimal("1.2345"), decimal("0.01")) ## ----------------------------------------------------------------------------- normalize(decimal(c("1.2300", "1.2"))) ## ----------------------------------------------------------------------------- fma(decimal("2"), decimal("3"), decimal("4")) ## ----------------------------------------------------------------------------- same_quantum(decimal(c("1.20", "2.0")), decimal(c("2.30", "3.00"))) ## ----------------------------------------------------------------------------- adjusted(decimal(c("1.2300", "1E-3")))