## 30 digit precision
str(x <- mpfr(c(2:3, pi), prec = 30 * log2(10)))
x^2
x[1] / x[2] # 0.66666... ~ 30 digits
## indexing - as with numeric vectors
stopifnot(identical(x[2], x[[2]]),
## indexing "outside" gives NA (well: "mpfr-NaN" for now):
is.na(x[5]),
## whereas "[[" cannot index outside:
is(try(x[[5]]), "try-error"),
## and only select *one* element:
is(try(x[[2:3]]), "try-error"))
## factorial() & lfactorial would work automagically via [l]gamma(),
## but factorial() additionally has an "mpfr" method which rounds
f200 <- factorial(mpfr(200, prec = 1500)) # need high prec.!
f200
as.numeric(log2(f200))# 1245.38 -- need precBits >~ 1246 for full precision
##--> see factorialMpfr() for more such computations.
##--- "Underflow" **much** later -- exponents have 30(+1) bits themselves:
mpfr.min.exp2 <- - (2^30 + 1)
two <- mpfr(2, 55)
stopifnot(two ^ mpfr.min.exp2 == 0)
## whereas
two ^ (mpfr.min.exp2 * (1 - 1e-15))
## 2.38256490488795107e-323228497 ["typically"]
##--- "Assert" that {sort}, {order}, {quantile}, {rank}, all work :
p <- mpfr(rpois(32, lambda=500), precBits=128)^10
np <- as.numeric(log(p))
stopifnot(all(diff(sort(p)) >= 0),
identical(order(p), order(np)),
identical(rank (p), rank (np)),
all.equal(sapply(1:9, function(Typ) quantile(np, type=Typ, names=FALSE)),
sapply(lapply(1:9, function(Typ) quantile( p, type=Typ, names=FALSE)),
function(x) as.numeric(log(x))),
tol = 1e-3),# quantiles: interpolated in orig. <--> log scale
TRUE)
m0 <- mpfr(numeric(), 99)
stopifnot(identical(which.min(m0), integer(0)),
identical(which.max(m0), integer(0)),
max(m0) == mpfr(-Inf, 53), # hmm, the 53 is not a feature
min(m0) == mpfr(+Inf, 53), # (ditto)
sum(m0) == 0, prod(m0) == 1)
Run the code above in your browser using DataLab