gmp-conversions | R Documentation |
Coerce from and to big integers (bigz
) and
mpfr
numbers.
Further, coerce from big rationals (bigq
) to
mpfr
numbers.
.bigz2mpfr(x, precB = NULL, rnd.mode = c('N','D','U','Z','A'))
.bigq2mpfr(x, precB = NULL, rnd.mode = c('N','D','U','Z','A'))
.mpfr2bigz(x, mod = NA)
.mpfr2bigq(x)
x |
an R object of class |
precB |
precision in bits for the result. The default,
|
rnd.mode |
a 1-letter string specifying how rounding
should happen at C-level conversion to MPFR, see details of
|
mod |
a possible modulus, see |
Note that we also provide the natural (S4) coercions,
as(x, "mpfr")
for x
inheriting from class "bigz"
or "bigq"
.
a numeric vector of the same length as x
, of the desired class.
mpfr()
, as.bigz
and
as.bigq
in package gmp.
S <- gmp::Stirling2(50,10)
show(S)
SS <- S * as.bigz(1:3)^128
stopifnot(all.equal(log2(SS[2]) - log2(S), 128, tolerance=1e-15),
identical(SS, .mpfr2bigz(.bigz2mpfr(SS))))
.bigz2mpfr(S) # 148 bit precision
.bigz2mpfr(S, precB=256) # 256 bit
## rational --> mpfr:
sq <- SS / as.bigz(2)^100
MP <- as(sq, "mpfr")
stopifnot(identical(MP, .bigq2mpfr(sq)),
SS == MP * as(2, "mpfr")^100)
## New since 2024-01-20: mpfr --> big rational "bigq"
Pi <- Const("pi", 128)
m <- Pi* 2^(-5:5)
(m <- c(m, mpfr(2, 128)^(-5:5)))
## 1 x large num/denom, then 2^(-5:5) as frac
tail( Q <- .mpfr2bigq(m) , 12)
getDenom <- Rmpfr:::getDenom
stopifnot(is.whole(m * (d.m <- getDenom(m))))
stopifnot(all.equal(m, mpfr(Q, 130), tolerance = 2^-130)) # I see even
all.equal(m, mpfr(Q, 130), tolerance = 0) # TRUE
m <- m * mpfr(2, 128)^200 # quite a bit larger
tail( Q. <- .mpfr2bigq(m) , 12) # large integers ..
stopifnot(is.whole(m * (d.m <- getDenom(m))))
stopifnot(all.equal(m, mpfr(Q., 130), tolerance = 2^-130)) # I see even
all.equal(m, mpfr(Q., 130), tolerance = 0) # TRUE
m2 <- m * mpfr(2, 128)^20000 ## really huge
stopifnot(is.whole(m2 * (d.m2 <- getDenom(m2))))
denominator(Q2 <- .mpfr2bigq(m2)) ## all 1 ! (all m2 ~~ 2^20200 )
stopifnot(all.equal(m2, mpfr(Q2, 130), tolerance = 2^-130)) # I see even
all.equal(m2, mpfr(Q2, 130), tolerance = 0) # TRUE
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.