frexpMpfr | R Documentation |
MPFR - versions of the C99 (and POSIX) standard C (and C++) mathlib
functions frexp()
and ldexp()
.
frexpMpfr(x)
computes base-2 exponent e
and “mantissa”,
or fraction r
, such that x = r * 2^e
, where r \in
[0.5, 1)
(unless when x
is in c(0, -Inf, Inf, NaN)
where r == x
and e
is 0),
and e
is integer valued.
ldexpMpfr(f, E)
is the inverse of frexpMpfr()
: Given
fraction or mantissa f
and integer exponent E
, it returns
x = f * 2^E
.
Viewed differently, it's the fastest way to multiply or divide MPFR
numbers with 2^E
.
frexpMpfr(x, rnd.mode = c("N", "D", "U", "Z", "A"))
ldexpMpfr(f, E, rnd.mode = c("N", "D", "U", "Z", "A"))
x |
numeric (coerced to |
f |
numeric fraction (vector), in |
E |
integer valued, exponent of |
rnd.mode |
a 1-letter string specifying how rounding
should happen at C-level conversion to MPFR, see |
frexpMpfr
returns a list
with named components r
(of class mpfr
) and e
(integer valued, of type
integer
is small enough, "double"
otherwise).
Martin Maechler
On unix-alikes, typically man frexp
and man ldexp
Somewhat related, .mpfr2exp()
.
frexp()
and ldexp()
in package DPQ.
set.seed(47)
x <- c(0, 2^(-3:3), (-1:1)/0,
sort(rlnorm(2^12, 10, 20) * sample(c(-1,1), 512, replace=TRUE)))
head(xM <- mpfr(x, 128), 11)
str(rFM <- frexpMpfr(xM))
d.fr <- with(rFM, data.frame(x=x, r=asNumeric(r), e=e))
head(d.fr , 16)
tail(d.fr)
ar <- abs(rFM$r)
stopifnot(0.5 <= ar[is.finite(x) & x != 0], ar[is.finite(x)] < 1,
is.integer(rFM$e))
ldx <- with(rFM, ldexpMpfr(r, e))
(iN <- which(is.na(x))) # 10
stopifnot(exprs = {
all.equal(xM, ldx, tol = 2^-124) # allow 4 bits loss, but apart from the NA, even:
identical(xM[-iN], ldx[-iN])
is.na(xM [iN])
is.na(ldx[iN])
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.