formatMpfr {Rmpfr} | R Documentation |
Formatting MPFR (multiprecision) Numbers
Description
Flexible formatting of “multiprecision numbers”, i.e., objects
of class mpfr
. formatMpfr()
is also the
mpfr
method of the generic format
function.
The formatN()
methods for mpfr
numbers
renders them differently than their double precision equivalents, by
appending "_M"
.
Function .mpfr2str()
is the low level work horse for
formatMpfr()
and hence all print()
ing of
"mpfr"
objects.
Usage
formatMpfr(x, digits = NULL, trim = FALSE, scientific = NA,
maybe.full = (!is.null(digits) && is.na(scientific)) || isFALSE(scientific),
base = 10, showNeg0 = TRUE, max.digits = Inf,
big.mark = "", big.interval = 3L,
small.mark = "", small.interval = 5L,
decimal.mark = ".",
exponent.char = if(base <= 14) "e" else if(base <= 36) "E" else "|e",
exponent.plus = TRUE,
zero.print = NULL, drop0trailing = FALSE, ...)
## S3 method for class 'mpfr'
formatN(x, drop0trailing = TRUE, ...)
.mpfr2str(x, digits = NULL, maybe.full = !is.null(digits), base = 10L)
Arguments
x |
an MPFR number (vector or array). |
digits |
how many significant digits (in the |
trim |
logical; if |
scientific |
either a logical specifying whether
MPFR numbers should be encoded in scientific
format (“exponential representation”), or an integer penalty
(see |
maybe.full |
|
base |
an integer in |
showNeg0 |
logical indicating if “negative” zeros
should be shown with a |
exponent.char |
the “exponent” character to be used in
scientific notation. The default takes into account that for
|
exponent.plus |
|
max.digits |
a (large) positive number to limit the number of
(mantissa) digits, notably when |
big.mark , big.interval , small.mark , small.interval , decimal.mark , zero.print , drop0trailing |
used for prettying decimal sequences, these are passed to
|
... |
further arguments passed to or from other methods. |
Value
a character vector or array, say cx
, of the same length as
x
. Since Rmpfr version 0.5-3 (2013-09), if x
is an
mpfrArray
, then cx
is a character
array
with the same dim
and
dimnames
as x
.
Note that in scientific notation, the integer exponent is always in
decimal, i.e., base 10 (even when base
is not 10), but
of course meaning base
powers, e.g., in base 32,
"u.giE3"
is the same as "ugi0"
which is 32^3
times
"u.gi"
. This is in contrast, e.g., with
sprintf("%a", x)
where the powers after "p"
are
powers of 2
.
Note
Currently, formatMpfr(x, scientific = FALSE)
does not work
correctly, e.g., for x <- Const("pi", 128) * 2^c(-200,200)
, i.e., it
uses the scientific / exponential-style format.
This is considered bogous and hopefully will change.
Author(s)
Martin Maechler
References
The MPFR manual's description of ‘mpfr_get_str()’ which is the
C-internal workhorse for .mpfr2str()
(on which formatMpfr()
builds).
See Also
mpfr
for creation and
the mpfr
class description with its many methods.
The format
generic, and the prettyNum
utility on which formatMpfr
is based as well.
The S3 generic function formatN
from package
gmp.
.mpfr_formatinfo(x)
provides the (cheap) non-string parts of
.mpfr2str(x)
; the (base 2) exp
exponents are also available
via .mpfr2exp(x)
.
Examples
## Printing of MPFR numbers uses formatMpfr() internally.
## Note how each components uses the "necessary" number of digits:
( x3 <- c(Const("pi", 168), mpfr(pi, 140), 3.14) )
format(x3[3], 15)
format(x3[3], 15, drop0 = TRUE)# "3.14" .. dropping the trailing zeros
x3[4] <- 2^30
x3[4] # automatically drops trailing zeros
format(x3[1], dig = 41, small.mark = "'") # (41 - 1 = ) 40 digits after "."
rbind(formatN( x3, digits = 15),
formatN(as.numeric(x3), digits = 15))
(Zero <- mpfr(c(0,1/-Inf), 20)) # 0 and "-0"
xx <- c(Zero, 1:2, Const("pi", 120), -100*pi, -.00987)
format(xx, digits = 2)
format(xx, digits = 1, showNeg0 = FALSE)# "-0" no longer shown
## Output in other bases :
formatMpfr(mpfr(10^6, 40), base=32, drop0trailing=TRUE)
## "ugi0"
mpfr("ugi0", base=32) #-> 1'000'000
## This now works: The large number shows "as" large integer:
x <- Const("pi", 128) * 2^c(-200,200)
formatMpfr(x, scientific = FALSE) # was 1.955...e-60 5.048...e+60
i32 <- mpfr(1:32, precBits = 64)
format(i32, base= 2, drop0trailing=TRUE)
format(i32, base= 16, drop0trailing=TRUE)
format(1/i32, base= 2, drop0trailing=TRUE)# using scientific notation for [17..32]
format(1/i32, base= 32)
format(1/i32, base= 62, drop0trailing=TRUE)
format(mpfr(2, 64)^-(1:16), base=16, drop0trailing=TRUE)