Learn R Programming

Rmpfr (version 0.5-3)

formatMpfr: 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".

Usage

formatMpfr(x, digits = NULL, trim = FALSE, scientific = NA,
	   showNeg0 = TRUE,
	   big.mark = "", big.interval = 3L,
	   small.mark = "", small.interval = 5L, decimal.mark = ".",
	   zero.print = NULL, drop0trailing = FALSE, ...)
## S3 method for class 'mpfr':
formatN(x, drop0trailing = TRUE, \dots)

Arguments

x
an MPFR number (vector or array).
digits
how many significant digits are to be used for numeric and complex x. The default, NULL, uses enough digits to represent the full precission.
trim
logical; if FALSE, numbers are right-justified to a common width: if TRUE the leading blanks for justification are suppressed.
scientific
either a logical specifying whether MPFR numbers should be encoded in scientific format, or an integer penalty (see options("scipen")). Missing values correspond to the current default penalty.
showNeg0
logical indicating if negative zeros should be shown with a "-". The default, TRUE is intentially different from format().
big.mark, big.interval, small.mark, small.interval, decimal.mark, zero.print, drop0trailing
used for prettying decimal sequences, these are passed to prettyNum and that help page explains the details.
...
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.

References

The MPFR manual's description of mpfr_get_str() which is the C-internal workhorse for the (internal) Rfunction .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.

Examples

Run this code
## 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

Run the code above in your browser using DataLab