mpfr(pi, 120) ## the double-precision pi "translated" to 120-bit precision
pi. <- Const("pi", prec = 260) # pi "computed" to correct 260-bit precision
pi. # nicely prints 80 digits [260 * log10(2) ~= 78.3 ~ 80]
Const("gamma", 128L) # 0.5772...
Const("catalan", 128L) # 0.9159...
x <- mpfr(0:7, 100)/7 # a more precise version of k/7, k=0,..,7
x
1 / x
## character input :
mpfr("2.718281828459045235360287471352662497757") - exp(mpfr(1, 150))
## ~= -4 * 10^-40
## Also works for NA, NaN, ... :
cx <- c("1234567890123456", 345, "NA", "NaN", "Inf", "-Inf")
mpfr(cx)
## with some 'base' choices :
print(mpfr("111.1111", base=2)) * 2^4
mpfr("af21.01020300a0b0c", base=16)
## 68 bit prec. 44833.00393694653820642
mpfr("ugi0", base = 32) == 10^6 ## TRUE
## --- Large integers from package 'gmp':
Z <- as.bigz(7)^(1:200)
head(Z, 40)
## mfpr(Z) by default chooses the correct *maximal* default precision:
mZ. <- mpfr(Z)
## more efficiently chooses precision individually
m.Z <- mpfr(Z, precBits = frexpZ(Z)$exp)
## the precBits chosen are large enough to keep full precision:
stopifnot(identical(cZ <- as.character(Z),
as(mZ.,"character")),
identical(cZ, as(m.Z,"character")))
## compare mpfr-arithmetic with exact rational one:
stopifnot(all.equal(mpfr(as.bigq(355,113), 99),
mpfr(355, 99) / 113, tol = 2^-98))
## look at different "rounding modes":
sapply(c("N", "D","U","Z","A"), function(RND)
mpfr(c(-1,1)/5, 20, rnd.mode = RND), simplify=FALSE)
symnum(sapply(c("N", "D","U","Z","A"),
function(RND) mpfr(0.2, prec = 5:15, rnd.mode = RND) < 0.2 ))
Run the code above in your browser using DataLab