95 - Arbitrary precision arithmetic

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(caracas)
inline_code <- function(x) {
  x
}

if (!has_sympy()) {
  # SymPy not available, so the chunks shall not be evaluated
  knitr::opts_chunk$set(eval = FALSE)

  inline_code <- function(x) {
    deparse(substitute(x))
  }
}

An example

n_2 <- as_sym("2")
n_pi <- as_sym("pi", declare_symbols = FALSE) # pi is a constant in SymPy
x <- sqrt(n_2) * n_pi
x
N(x)
N(x, 5)
N(x, 50)
as.character(N(x, 50))

Another example

As SymPy requires mpmath this can be used directly, for example like this (example due to Hans W Borchers, thanks!):

mpmath <- reticulate::import('mpmath')
mpmath$mp$dps <- 30
z <- mpmath$mpc(real = '1.0', imag = '63.453')
zeta_z <- mpmath$zeta(z)
zeta_z
as.character(zeta_z$real)
as.character(zeta_z$imag)


Try the caracas package in your browser

Any scripts or data that you put into this service are public.

caracas documentation built on Oct. 17, 2023, 5:08 p.m.