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)))
getDenom <- Rmpfr:::getDenom
stopifnot(is.whole(m * (d.m <- getDenom(m))))
stopifnot(all.equal(m, mpfr(.mpfr2bigq(m), 130), tolerance = 2^-130)) # I see even
all.equal(m, mpfr(.mpfr2bigq(m), 130), tolerance = 0) # TRUE
m <- m * mpfr(2, 128)^200 # quite a bit larger
stopifnot(is.whole(m * (d.m <- getDenom(m))))
stopifnot(all.equal(m, mpfr(.mpfr2bigq(m), 130), tolerance = 2^-130)) # I see even
all.equal(m, mpfr(.mpfr2bigq(m), 130), tolerance = 0) # TRUE
m2 <- m * mpfr(2, 128)^20000 ## really huge
stopifnot(is.whole(m2 * (d.m2 <- getDenom(m2))))
stopifnot(all.equal(m2, mpfr(.mpfr2bigq(m2), 130), tolerance = 2^-130)) # I see even
all.equal(m2, mpfr(.mpfr2bigq(m2), 130), tolerance = 0) # TRUE
Run the code above in your browser using DataLab