gmp (version 0.6-0)

frexpZ: Split Number into Fractional and Exponent of 2 Parts

Description

Breaks the number x into its binary significand (“fraction”) \(d \in [0.5, 1)\) and \(ex\), the integral exponent for 2, such that \(x = d \cdot 2^{ex}.\)

If x is zero, both parts (significand and exponent) are zero.

Usage

frexpZ(x)

Arguments

x

integer or big integer (bigz).

Value

a list with the two components

d

a numeric vector whose absolute values are either zero, or in \([\frac{1}{2}, 1)\).

exp

an integer vector of the same length; note that exp == 1 + floor(log2(x)), and hence always exp > log2(x).

See Also

log2, etc; for bigz objects built on (the C++ equivalent of) frexp(), actually GMP's mpz_get_d_2exp().

Examples

Run this code
# NOT RUN {
frexpZ(1:10)
## and confirm :
with(frexpZ(1:10),  d * 2^exp)
x <- rpois(1000, lambda=100) * (1 + rpois(1000, lambda=16))
X <- as.bigz(x)
stopifnot(all.equal(x, with(frexpZ(x), d* 2^exp)),
          1+floor(log2(x)) == (fx <- frexpZ(x)$exp),
          fx == frexpZ(X)$exp,
          1+floor(log2(X)) == fx
)
# }

Run the code above in your browser using DataCamp Workspace