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
Run the code above in your browser using DataLab