formatMpfr: Formatting MPFR (multiprecision) Numbers

Description Usage Arguments Value Author(s) References See Also Examples

View source: R/as.R

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

1
2
3
4
5
6
7
8
9
formatMpfr(x, digits = NULL, trim = FALSE, scientific = NA,
	   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",
           zero.print = NULL, drop0trailing = FALSE, ...)
## S3 method for class 'mpfr'
formatN(x, drop0trailing = TRUE, ...)

Arguments

x

an MPFR number (vector or array).

digits

how many significant digits (in the base chosen!) are to be used in the result. The default, NULL, uses enough digits to represent the full precision, often one or two digits more than “you” would expect. For bases 2,4,8,16, or 32, MPFR requires digits at least 2. For such bases, digits = 1 is changed into 2, with a message.

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.

base

an integer in 2,3,..,62; the base (“basis”) in which the numbers should be represented. Apart from the default base 10, binary (base = 2) or hexadecimal (base = 16) are particularly interesting.

showNeg0

logical indicating if “negative” zeros should be shown with a "-". The default, TRUE is intentially different from format(<numeric>).

exponent.char

the “exponent” character to be used in scientific notation. The default takes into account that for base B >= 15, "e" is part of the (mantissa) digits and the same is true for "E" when B >= 37.

max.digits

a (large) positive number to limit the number of (mantissa) digits, notably when digits is NULL (as by default). Otherwise, a numeric digits is preferred to setting max.digits (which should not be smaller than digits).

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.

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.

Author(s)

Martin Maechler

References

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 ## 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


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)

Example output

Loading required package: gmp

Attaching package: 'gmp'

The following objects are masked from 'package:base':

    %*%, apply, crossprod, matrix, tcrossprod

C code of R package 'Rmpfr': GMP using 64 bits per limb


Attaching package: 'Rmpfr'

The following objects are masked from 'package:stats':

    dbinom, dnorm, dpois, pnorm

The following objects are masked from 'package:base':

    cbind, pmax, pmin, rbind

3 'mpfr' numbers of precision  128 .. 168  bits 
[1] 3.141592653589793238462643383279502884197169399375101
[2]         3.1415926535897931159979634685441851615905762
[3]             3.140000000000000124344978758017532527447
[1] "3.14000000000000"
[1] "3.14"
1 'mpfr' number of precision  53   bits 
[1] 1073741824
[1] "3.14159'26535'89793'23846'26433'83279'50288'41972"
     [,1]                 [,2]                 [,3]     [,4]          
[1,] "3.14159265358979_M" "3.14159265358979_M" "3.14_M" "1073741824_M"
[2,] "3.14159265358979"   "3.14159265358979"   "3.14"   "1073741824." 
2 'mpfr' numbers of precision  20   bits 
[1]  0 -0
[1] "0.0"     "-0.0"    "1.0"     "2.0"     "3.1"     "-310."   "-0.0099"
[1] "0."    "0."    "1."    "2."    "3."    "-3.e2" "-0.01"
[1] "ugi0"
1 'mpfr' number of precision  20   bits 
[1] 1000000
 [1] "1"      "10"     "11"     "100"    "101"    "110"    "111"    "1000"  
 [9] "1001"   "1010"   "1011"   "1100"   "1101"   "1110"   "1111"   "10000" 
[17] "10001"  "10010"  "10011"  "10100"  "10101"  "10110"  "10111"  "11000" 
[25] "11001"  "11010"  "11011"  "11100"  "11101"  "11110"  "11111"  "100000"
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "a"  "b"  "c"  "d"  "e"  "f" 
[16] "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "1a" "1b" "1c" "1d" "1e"
[31] "1f" "20"
 [1] "1"                                                                    
 [2] "0.1"                                                                  
 [3] "0.01010101010101010101010101010101010101010101010101010101010101011"  
 [4] "0.01"                                                                 
 [5] "0.001100110011001100110011001100110011001100110011001100110011001101" 
 [6] "0.001010101010101010101010101010101010101010101010101010101010101011" 
 [7] "0.001001001001001001001001001001001001001001001001001001001001001001" 
 [8] "0.001"                                                                
 [9] "0.000111000111000111000111000111000111000111000111000111000111000111" 
[10] "0.0001100110011001100110011001100110011001100110011001100110011001101"
[11] "0.00010111010001011101000101110100010111010001011101000101110100011"  
[12] "0.0001010101010101010101010101010101010101010101010101010101010101011"
[13] "0.000100111011000100111011000100111011000100111011000100111011000101" 
[14] "0.0001001001001001001001001001001001001001001001001001001001001001001"
[15] "0.0001000100010001000100010001000100010001000100010001000100010001001"
[16] "0.0001"                                                               
[17] "1.111000011110000111100001111000011110000111100001111000011110001e-5" 
[18] "1.1100011100011100011100011100011100011100011100011100011100011e-5"   
[19] "1.1010111100101000011010111100101000011010111100101000011010111e-5"   
[20] "1.100110011001100110011001100110011001100110011001100110011001101e-5" 
[21] "1.100001100001100001100001100001100001100001100001100001100001e-5"    
[22] "1.011101000101110100010111010001011101000101110100010111010001e-5"    
[23] "1.011001000010110010000101100100001011001000010110010000101100e-5"    
[24] "1.010101010101010101010101010101010101010101010101010101010101011e-5" 
[25] "1.0100011110101110000101000111101011100001010001111010111000010e-5"   
[26] "1.0011101100010011101100010011101100010011101100010011101100010e-5"   
[27] "1.001011110110100001001011110110100001001011110110100001001011111e-5" 
[28] "1.001001001001001001001001001001001001001001001001001001001001001e-5" 
[29] "1.000110100111101110010110000100011010011110111001011000010001101e-5" 
[30] "1.000100010001000100010001000100010001000100010001000100010001001e-5" 
[31] "1.00001000010000100001000010000100001000010000100001000010000e-5"     
[32] "1e-5"                                                                 
 [1] "1.0000000000000"  "0.g0000000000000" "0.alalalalalalb0" "0.80000000000000"
 [5] "0.6cpj6cpj6cpj6g" "0.5alalalalalalg" "0.4i94i94i94i94g" "0.40000000000000"
 [9] "0.3hose73hose73g" "0.36cpj6cpj6cpj8" "0.2t2t2t2t2t2t30" "0.2lalalalalalao"
[13] "0.2eojm4th7c9r2g" "0.294i94i94i94i8" "0.248h248h248h28" "0.20000000000000"
[17] "0.1s7gu3of1s7gu4" "0.1ose73hose73ho" "0.1lsk6nigqua3bo" "0.1j6cpj6cpj6cpk"
[21] "0.1goc631goc631g" "0.1ehehehehehehg" "0.1cgm8b45i2p1cg" "0.1alalalalalalc"
[25] "0.18un18un18un18" "0.17c9r2eojm4th8" "0.15tk4nmgiuq2bs" "0.14i94i94i94i94"
[29] "0.139tpc4d7n5ghk" "0.1248h248h248h4" "0.11111111111110" "0.10000000000000"
 [1] "1"              "0."             "0.KfKfKfKfKfL8" "0.F"           
 [5] "0.COnbCOnbCOnk" "0.AKfKfKfKfKfZ" "0.8r8r8r8r8r8l" "0.7k"          
 [9] "0.6t6t6t6t6t6o" "0.6COnbCOnbCOs" "0.5dSBGuMXoj5l" "0.5AKfKfKfKfKn"
[13] "0.4lgvEJ4lgvEM" "0.4QZQZQZQZQZN" "0.48GX48GX48Gh" "0.3sF"         
[17] "0.3e7IEaTAwLsi" "0.3RYRYRYRYRYP" "0.3GJZtTMqD3GG" "0.36COnbCOnbCR"
[21] "0.2x2x2x2x2x2v" "0.2oj5dSBGuMXt" "0.2h85OGAmWLZ1" "0.2aAKfKfKfKfO"
[25] "0.2Tl7RHMJq4xU" "0.2NqSc9XNqScB" "0.2IMxhd2IMxhf" "0.2DHiHiHiHiHh"
[29] "0.28YCpJExrRn8" "0.248GX48GX48M" "0.1zzzzzzzzzzx" "0.1w7k"        
 [1] "0."    "0."    "0."    "0."    "0.0"   "0.0"   "0.0"   "0.0"   "0.00" 
[10] "0.00"  "0.00"  "0.00"  "0.000" "0.000" "0.000" "0.000"

Rmpfr documentation built on July 31, 2018, 1 a.m.