R Under development (unstable) (2024-01-20 r85814 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > require(Rmpfr) Loading required package: Rmpfr 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 object is masked from 'package:gmp': outer The following objects are masked from 'package:stats': dbinom, dgamma, dnbinom, dnorm, dpois, dt, pnorm The following objects are masked from 'package:base': cbind, pmax, pmin, rbind > sessionInfo() R Under development (unstable) (2024-01-20 r85814 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows Server 2022 x64 (build 20348) Matrix products: default locale: [1] LC_COLLATE=C LC_CTYPE=German_Germany.utf8 [3] LC_MONETARY=C LC_NUMERIC=C [5] LC_TIME=C time zone: Europe/Berlin tzcode source: internal attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] Rmpfr_0.9-5 gmp_0.7-4 loaded via a namespace (and not attached): [1] compiler_4.4.0 > > nums <- c(0, .625, .1, .3, .3125, .5, .6, (.3+.6), .9, 1, 3.3125) > nums9 <- mpfr(nums, precBits = 9) > > n5.b2 <- mpfr("101", base=2) > stopifnot(identical(n5.b2, mpfr(5, precBits=3)), + identical(n5.b2, mpfr("0b101", base=2))) > if(FALSE)## 0b101 could also be a Hex number with no initial '0x' -- hence NOT true: + identical(n5.b2, mpfr("0b101")) > ## We *could* say that anything starting with '0b' is binary, anything with '0x' is hexadecimal > > > ### mpfr_Bcharacter() {no longer method} [was 'mpfrBchar', was 'scanBin'] : > > ##' Check the inverse of formatBin(), i.e., mpfr() working correctly > chkInv.fBin <- function(x, ...) { + stopifnot(is(x, "mpfr")) + nb <- formatBin(x, ...) + xx <- mpfr(nb) + ## Inverse should work {apart from 0: that is not uniquely represented in MPFR!} + stopifnot(identical(mpfrIs0(x ) -> i0, + mpfrIs0(xx)), + identical(x[!i0], xx[!i0])) + invisible(nb) + } > (nums9bc <- chkInv.fBin(nums9)) [1] +0b0.00000000p+0 +0b1.01000000p-1 +0b1.10011010p-4 +0b1.00110011p-2 [5] +0b1.01000000p-2 +0b1.00000000p-1 +0b1.00110011p-1 +0b1.11001101p-1 [9] +0b1.11001101p-1 +0b1.00000000p+0 +0b1.10101000p+1 > (nums9bF <- chkInv.fBin(nums9, scientific=FALSE)) # "F": Fixed format (as in Fortran) [1] +0b_0.00000000____ +0b__.101000000___ +0b__.___110011010 +0b__._100110011__ [5] +0b__._101000000__ +0b__.100000000___ +0b__.100110011___ +0b__.111001101___ [9] +0b__.111001101___ +0b_1.00000000____ +0b11.0101000_____ > > ## higher precision, 0,Inf, sign change: > (i12 <- 1 / mpfr(c(-2:12, Inf), 64)) 16 'mpfr' numbers of precision 64 bits [1] -0.5 -1 Inf [4] 1 0.5 0.333333333333333333342 [7] 0.25 0.200000000000000000003 0.166666666666666666671 [10] 0.142857142857142857141 0.125 0.11111111111111111111 [13] 0.100000000000000000001 0.0909090909090909090934 0.0833333333333333333356 [16] 0 > (i12.50 <- roundMpfr(i12, precBits=50)) # "same", with 50 (< 53) bits 16 'mpfr' numbers of precision 50 bits [1] -0.5 -1 Inf [4] 1 0.5 0.33333333333333348 [7] 0.25 0.19999999999999996 0.16666666666666674 [10] 0.14285714285714279 0.125 0.11111111111111116 [13] 0.099999999999999978 0.090909090909090939 0.08333333333333337 [16] 0 > try({ ## FIXME -- formatBin() bug -- fails for 'Inf' ! + (nI.12 <- chkInv.fBin(i12 )) + (nI.12.50 <- chkInv.fBin(i12.50)) + }) [1] -0b1.0000000000000000000000000000000000000000000000000p-1 [2] -0b1.0000000000000000000000000000000000000000000000000p+0 [3] Inf [4] +0b1.0000000000000000000000000000000000000000000000000p+0 [5] +0b1.0000000000000000000000000000000000000000000000000p-1 [6] +0b1.0101010101010101010101010101010101010101010101011p-2 [7] +0b1.0000000000000000000000000000000000000000000000000p-2 [8] +0b1.1001100110011001100110011001100110011001100110011p-3 [9] +0b1.0101010101010101010101010101010101010101010101011p-3 [10] +0b1.0010010010010010010010010010010010010010010010010p-3 [11] +0b1.0000000000000000000000000000000000000000000000000p-3 [12] +0b1.1100011100011100011100011100011100011100011100100p-4 [13] +0b1.1001100110011001100110011001100110011001100110011p-4 [14] +0b1.0111010001011101000101110100010111010001011101001p-4 [15] +0b1.0101010101010101010101010101010101010101010101011p-4 [16] +0b0.0000000000000000000000000000000000000000000000000p+0 > ii <- mpfr(c(-Inf, 17, Inf), 7) > formatHex(ii) ## fine [1] -Inf +0x1.10p+4 Inf > formatDec(ii) ## not 100% ok, as it has "." [FIXME ?] [1] -Inf 17.00 Inf > > n9. <- Rmpfr:::mpfr.Ncharacter(nums9bc) > n9_ <- mpfr(nums9bc) > ## Inverse worked {apart from 0: it is not at all uniquely represented in MPFR!} > stopifnot(identical(mpfrIs0(n9.), mpfrIs0(n9_)), + all.equal(n9_, n9., tolerance=0), + identical(nums9[-1], n9.[-1])) > > mpfr(nums9bc, precBits=5) 11 'mpfr' numbers of precision 5 bits [1] 0 0.625 0.102 0.297 0.312 0.5 0.594 0.906 0.906 1 3.25 > > (n9.5 <- mpfr(nums9bF, scientific=FALSE, precBits=5)) 11 'mpfr' numbers of precision 5 bits [1] 0 0.625 0.102 0.297 0.312 0.5 0.594 0.906 0.906 1 3.25 > stopifnot(all.equal(n9.5, mpfr(nums9bF, precBits=5), tol=0)) > stopifnot(all.equal(n9., n9.5, tol = 0.02), getPrec(n9.5) == 5) > mpfr(nums9bF, scientific=FALSE) 11 'mpfr' numbers of precision 9 bits [1] 0 0.625 0.1001 0.2998 0.3125 0.5 0.5996 0.9004 0.9004 1 [11] 3.312 > mpfr(nums9bF) 11 'mpfr' numbers of precision 9 bits [1] 0 0.625 0.1001 0.2998 0.3125 0.5 0.5996 0.9004 0.9004 1 [11] 3.312 > > ### mpfr() -> mpfrHchar (was 'scanHex') : > nums9hc <- formatHex(nums9) > stopifnot(all.equal(n9., mpfr(nums9hc), tol = 0), + all.equal(n9.5, mpfr(nums9hc, precBits=5), tol = 0)) > > ### Rest from ../R/formatHex.R -- heavily modified > > (m32 <- matrix(0:31, 8, 4, dimnames = list(0:7, c(0,8,16,24)))) 0 8 16 24 0 0 8 16 24 1 1 9 17 25 2 2 10 18 26 3 3 11 19 27 4 4 12 20 28 5 5 13 21 29 6 6 14 22 30 7 7 15 23 31 > FourBits <- mpfr(m32, precBits=4) > FiveBits <- mpfr(m32, precBits=5) > stopifnot(all.equal(m32, asNumeric(FiveBits), tol=0), + all.equal(m32, asNumeric(FourBits), tol= 0.05)) > > ## HH:::formatHexInternal(FourBits) > formatHex(FourBits) 0 8 16 24 0 +0x0.0p+0 +0x1.0p+3 +0x1.0p+4 +0x1.8p+4 1 +0x1.0p+0 +0x1.2p+3 +0x1.0p+4 +0x1.8p+4 2 +0x1.0p+1 +0x1.4p+3 +0x1.2p+4 +0x1.ap+4 3 +0x1.8p+1 +0x1.6p+3 +0x1.4p+4 +0x1.cp+4 4 +0x1.0p+2 +0x1.8p+3 +0x1.4p+4 +0x1.cp+4 5 +0x1.4p+2 +0x1.ap+3 +0x1.4p+4 +0x1.cp+4 6 +0x1.8p+2 +0x1.cp+3 +0x1.6p+4 +0x1.ep+4 7 +0x1.cp+2 +0x1.ep+3 +0x1.8p+4 +0x1.0p+5 > formatHex(FourBits, precBits=5) 0 8 16 24 0 +0x0.0p+0 +0x1.0p+3 +0x1.0p+4 +0x1.8p+4 1 +0x1.0p+0 +0x1.2p+3 +0x1.0p+4 +0x1.8p+4 2 +0x1.0p+1 +0x1.4p+3 +0x1.2p+4 +0x1.ap+4 3 +0x1.8p+1 +0x1.6p+3 +0x1.4p+4 +0x1.cp+4 4 +0x1.0p+2 +0x1.8p+3 +0x1.4p+4 +0x1.cp+4 5 +0x1.4p+2 +0x1.ap+3 +0x1.4p+4 +0x1.cp+4 6 +0x1.8p+2 +0x1.cp+3 +0x1.6p+4 +0x1.ep+4 7 +0x1.cp+2 +0x1.ep+3 +0x1.8p+4 +0x1.0p+5 > formatHex(FourBits, precBits=6) 0 8 16 24 0 +0x0.00p+0 +0x1.00p+3 +0x1.00p+4 +0x1.80p+4 1 +0x1.00p+0 +0x1.20p+3 +0x1.00p+4 +0x1.80p+4 2 +0x1.00p+1 +0x1.40p+3 +0x1.20p+4 +0x1.a0p+4 3 +0x1.80p+1 +0x1.60p+3 +0x1.40p+4 +0x1.c0p+4 4 +0x1.00p+2 +0x1.80p+3 +0x1.40p+4 +0x1.c0p+4 5 +0x1.40p+2 +0x1.a0p+3 +0x1.40p+4 +0x1.c0p+4 6 +0x1.80p+2 +0x1.c0p+3 +0x1.60p+4 +0x1.e0p+4 7 +0x1.c0p+2 +0x1.e0p+3 +0x1.80p+4 +0x1.00p+5 > formatBin(FourBits) 0 8 16 24 0 +0b0.000p+0 +0b1.000p+3 +0b1.000p+4 +0b1.100p+4 1 +0b1.000p+0 +0b1.001p+3 +0b1.000p+4 +0b1.100p+4 2 +0b1.000p+1 +0b1.010p+3 +0b1.001p+4 +0b1.101p+4 3 +0b1.100p+1 +0b1.011p+3 +0b1.010p+4 +0b1.110p+4 4 +0b1.000p+2 +0b1.100p+3 +0b1.010p+4 +0b1.110p+4 5 +0b1.010p+2 +0b1.101p+3 +0b1.010p+4 +0b1.110p+4 6 +0b1.100p+2 +0b1.110p+3 +0b1.011p+4 +0b1.111p+4 7 +0b1.110p+2 +0b1.111p+3 +0b1.100p+4 +0b1.000p+5 > formatBin(FourBits, precBits=5) 0 8 16 24 0 +0b0.0000p+0 +0b1.0000p+3 +0b1.0000p+4 +0b1.1000p+4 1 +0b1.0000p+0 +0b1.0010p+3 +0b1.0000p+4 +0b1.1000p+4 2 +0b1.0000p+1 +0b1.0100p+3 +0b1.0010p+4 +0b1.1010p+4 3 +0b1.1000p+1 +0b1.0110p+3 +0b1.0100p+4 +0b1.1100p+4 4 +0b1.0000p+2 +0b1.1000p+3 +0b1.0100p+4 +0b1.1100p+4 5 +0b1.0100p+2 +0b1.1010p+3 +0b1.0100p+4 +0b1.1100p+4 6 +0b1.1000p+2 +0b1.1100p+3 +0b1.0110p+4 +0b1.1110p+4 7 +0b1.1100p+2 +0b1.1110p+3 +0b1.1000p+4 +0b1.0000p+5 > formatBin(FourBits, scientific=FALSE) 0 8 16 24 0 +0b_____0.000 +0b__1000.___ +0b_1000_.___ +0b_1100_.___ 1 +0b_____1.000 +0b__1001.___ +0b_1000_.___ +0b_1100_.___ 2 +0b____10.00_ +0b__1010.___ +0b_1001_.___ +0b_1101_.___ 3 +0b____11.00_ +0b__1011.___ +0b_1010_.___ +0b_1110_.___ 4 +0b___100.0__ +0b__1100.___ +0b_1010_.___ +0b_1110_.___ 5 +0b___101.0__ +0b__1101.___ +0b_1010_.___ +0b_1110_.___ 6 +0b___110.0__ +0b__1110.___ +0b_1011_.___ +0b_1111_.___ 7 +0b___111.0__ +0b__1111.___ +0b_1100_.___ +0b1000__.___ > formatBin(FourBits, scientific=FALSE, precBits=5) 0 8 16 24 0 +0b_____0.0000 +0b__1000.0___ +0b_10000.____ +0b_11000.____ 1 +0b_____1.0000 +0b__1001.0___ +0b_10000.____ +0b_11000.____ 2 +0b____10.000_ +0b__1010.0___ +0b_10010.____ +0b_11010.____ 3 +0b____11.000_ +0b__1011.0___ +0b_10100.____ +0b_11100.____ 4 +0b___100.00__ +0b__1100.0___ +0b_10100.____ +0b_11100.____ 5 +0b___101.00__ +0b__1101.0___ +0b_10100.____ +0b_11100.____ 6 +0b___110.00__ +0b__1110.0___ +0b_10110.____ +0b_11110.____ 7 +0b___111.00__ +0b__1111.0___ +0b_11000.____ +0b10000_.____ > formatDec(FourBits) 0 8 16 24 0 0.00 8.00 16.0 24.0 1 1.00 9.00 16.0 24.0 2 2.00 10.0 18.0 26.0 3 3.00 11.0 20.0 28.0 4 4.00 12.0 20.0 28.0 5 5.00 13.0 20.0 28.0 6 6.00 14.0 22.0 30.0 7 7.00 15.0 24.0 32.0 > formatDec(FourBits, precBits=5) 0 8 16 24 0 0.00 8.00 16.0 24.0 1 1.00 9.00 16.0 24.0 2 2.00 10.0 18.0 26.0 3 3.00 11.0 20.0 28.0 4 4.00 12.0 20.0 28.0 5 5.00 13.0 20.0 28.0 6 6.00 14.0 22.0 30.0 7 7.00 15.0 24.0 32.0 > formatDec(FourBits, precBits=7) 0 8 16 24 0 0.000 8.000 16.00 24.00 1 1.000 9.000 16.00 24.00 2 2.000 10.00 18.00 26.00 3 3.000 11.00 20.00 28.00 4 4.000 12.00 20.00 28.00 5 5.000 13.00 20.00 28.00 6 6.000 14.00 22.00 30.00 7 7.000 15.00 24.00 32.00 > > ## HH:::formatHexInternal(FiveBits) > formatHex(FiveBits) 0 8 16 24 0 +0x0.0p+0 +0x1.0p+3 +0x1.0p+4 +0x1.8p+4 1 +0x1.0p+0 +0x1.2p+3 +0x1.1p+4 +0x1.9p+4 2 +0x1.0p+1 +0x1.4p+3 +0x1.2p+4 +0x1.ap+4 3 +0x1.8p+1 +0x1.6p+3 +0x1.3p+4 +0x1.bp+4 4 +0x1.0p+2 +0x1.8p+3 +0x1.4p+4 +0x1.cp+4 5 +0x1.4p+2 +0x1.ap+3 +0x1.5p+4 +0x1.dp+4 6 +0x1.8p+2 +0x1.cp+3 +0x1.6p+4 +0x1.ep+4 7 +0x1.cp+2 +0x1.ep+3 +0x1.7p+4 +0x1.fp+4 > formatHex(FiveBits, precBits=5) 0 8 16 24 0 +0x0.0p+0 +0x1.0p+3 +0x1.0p+4 +0x1.8p+4 1 +0x1.0p+0 +0x1.2p+3 +0x1.1p+4 +0x1.9p+4 2 +0x1.0p+1 +0x1.4p+3 +0x1.2p+4 +0x1.ap+4 3 +0x1.8p+1 +0x1.6p+3 +0x1.3p+4 +0x1.bp+4 4 +0x1.0p+2 +0x1.8p+3 +0x1.4p+4 +0x1.cp+4 5 +0x1.4p+2 +0x1.ap+3 +0x1.5p+4 +0x1.dp+4 6 +0x1.8p+2 +0x1.cp+3 +0x1.6p+4 +0x1.ep+4 7 +0x1.cp+2 +0x1.ep+3 +0x1.7p+4 +0x1.fp+4 > formatHex(FiveBits, precBits=6) 0 8 16 24 0 +0x0.00p+0 +0x1.00p+3 +0x1.00p+4 +0x1.80p+4 1 +0x1.00p+0 +0x1.20p+3 +0x1.10p+4 +0x1.90p+4 2 +0x1.00p+1 +0x1.40p+3 +0x1.20p+4 +0x1.a0p+4 3 +0x1.80p+1 +0x1.60p+3 +0x1.30p+4 +0x1.b0p+4 4 +0x1.00p+2 +0x1.80p+3 +0x1.40p+4 +0x1.c0p+4 5 +0x1.40p+2 +0x1.a0p+3 +0x1.50p+4 +0x1.d0p+4 6 +0x1.80p+2 +0x1.c0p+3 +0x1.60p+4 +0x1.e0p+4 7 +0x1.c0p+2 +0x1.e0p+3 +0x1.70p+4 +0x1.f0p+4 > formatBin(FiveBits) 0 8 16 24 0 +0b0.0000p+0 +0b1.0000p+3 +0b1.0000p+4 +0b1.1000p+4 1 +0b1.0000p+0 +0b1.0010p+3 +0b1.0001p+4 +0b1.1001p+4 2 +0b1.0000p+1 +0b1.0100p+3 +0b1.0010p+4 +0b1.1010p+4 3 +0b1.1000p+1 +0b1.0110p+3 +0b1.0011p+4 +0b1.1011p+4 4 +0b1.0000p+2 +0b1.1000p+3 +0b1.0100p+4 +0b1.1100p+4 5 +0b1.0100p+2 +0b1.1010p+3 +0b1.0101p+4 +0b1.1101p+4 6 +0b1.1000p+2 +0b1.1100p+3 +0b1.0110p+4 +0b1.1110p+4 7 +0b1.1100p+2 +0b1.1110p+3 +0b1.0111p+4 +0b1.1111p+4 > formatBin(FiveBits, precBits=5) 0 8 16 24 0 +0b0.0000p+0 +0b1.0000p+3 +0b1.0000p+4 +0b1.1000p+4 1 +0b1.0000p+0 +0b1.0010p+3 +0b1.0001p+4 +0b1.1001p+4 2 +0b1.0000p+1 +0b1.0100p+3 +0b1.0010p+4 +0b1.1010p+4 3 +0b1.1000p+1 +0b1.0110p+3 +0b1.0011p+4 +0b1.1011p+4 4 +0b1.0000p+2 +0b1.1000p+3 +0b1.0100p+4 +0b1.1100p+4 5 +0b1.0100p+2 +0b1.1010p+3 +0b1.0101p+4 +0b1.1101p+4 6 +0b1.1000p+2 +0b1.1100p+3 +0b1.0110p+4 +0b1.1110p+4 7 +0b1.1100p+2 +0b1.1110p+3 +0b1.0111p+4 +0b1.1111p+4 > formatBin(FiveBits, precBits=6) 0 8 16 24 0 +0b0.00000p+0 +0b1.00000p+3 +0b1.00000p+4 +0b1.10000p+4 1 +0b1.00000p+0 +0b1.00100p+3 +0b1.00010p+4 +0b1.10010p+4 2 +0b1.00000p+1 +0b1.01000p+3 +0b1.00100p+4 +0b1.10100p+4 3 +0b1.10000p+1 +0b1.01100p+3 +0b1.00110p+4 +0b1.10110p+4 4 +0b1.00000p+2 +0b1.10000p+3 +0b1.01000p+4 +0b1.11000p+4 5 +0b1.01000p+2 +0b1.10100p+3 +0b1.01010p+4 +0b1.11010p+4 6 +0b1.10000p+2 +0b1.11000p+3 +0b1.01100p+4 +0b1.11100p+4 7 +0b1.11000p+2 +0b1.11100p+3 +0b1.01110p+4 +0b1.11110p+4 > formatBin(FiveBits, scientific=FALSE) 0 8 16 24 0 +0b____0.0000 +0b_1000.0___ +0b10000.____ +0b11000.____ 1 +0b____1.0000 +0b_1001.0___ +0b10001.____ +0b11001.____ 2 +0b___10.000_ +0b_1010.0___ +0b10010.____ +0b11010.____ 3 +0b___11.000_ +0b_1011.0___ +0b10011.____ +0b11011.____ 4 +0b__100.00__ +0b_1100.0___ +0b10100.____ +0b11100.____ 5 +0b__101.00__ +0b_1101.0___ +0b10101.____ +0b11101.____ 6 +0b__110.00__ +0b_1110.0___ +0b10110.____ +0b11110.____ 7 +0b__111.00__ +0b_1111.0___ +0b10111.____ +0b11111.____ > formatBin(FiveBits, scientific=FALSE, precBits=6) 0 8 16 24 0 +0b____0.00000 +0b_1000.00___ +0b10000.0____ +0b11000.0____ 1 +0b____1.00000 +0b_1001.00___ +0b10001.0____ +0b11001.0____ 2 +0b___10.0000_ +0b_1010.00___ +0b10010.0____ +0b11010.0____ 3 +0b___11.0000_ +0b_1011.00___ +0b10011.0____ +0b11011.0____ 4 +0b__100.000__ +0b_1100.00___ +0b10100.0____ +0b11100.0____ 5 +0b__101.000__ +0b_1101.00___ +0b10101.0____ +0b11101.0____ 6 +0b__110.000__ +0b_1110.00___ +0b10110.0____ +0b11110.0____ 7 +0b__111.000__ +0b_1111.00___ +0b10111.0____ +0b11111.0____ > formatDec(FiveBits) 0 8 16 24 0 0.00 8.00 16.0 24.0 1 1.00 9.00 17.0 25.0 2 2.00 10.0 18.0 26.0 3 3.00 11.0 19.0 27.0 4 4.00 12.0 20.0 28.0 5 5.00 13.0 21.0 29.0 6 6.00 14.0 22.0 30.0 7 7.00 15.0 23.0 31.0 > formatDec(FiveBits, precBits=5) 0 8 16 24 0 0.00 8.00 16.0 24.0 1 1.00 9.00 17.0 25.0 2 2.00 10.0 18.0 26.0 3 3.00 11.0 19.0 27.0 4 4.00 12.0 20.0 28.0 5 5.00 13.0 21.0 29.0 6 6.00 14.0 22.0 30.0 7 7.00 15.0 23.0 31.0 > formatDec(FiveBits, precBits=7) 0 8 16 24 0 0.000 8.000 16.00 24.00 1 1.000 9.000 17.00 25.00 2 2.000 10.00 18.00 26.00 3 3.000 11.00 19.00 27.00 4 4.000 12.00 20.00 28.00 5 5.000 13.00 21.00 29.00 6 6.000 14.00 22.00 30.00 7 7.000 15.00 23.00 31.00 > > TenPowers <- mpfr(matrix(10^(-3:5)), precBits=53) > row.names(TenPowers) <- -3:5 > TenPowers 'mpfrMatrix' of dim(.) = (9, 1) of precision 53 bits [,1] -3 0.0010000000000000000 -2 0.010000000000000000 -1 0.10000000000000001 0 1.0000000000000000 1 10.000000000000000 2 100.00000000000000 3 1000.0000000000000 4 10000.000000000000 5 100000.00000000000 > > options(width = 180) # for several "wide" displays below > > ## This indirectly "checks' as.data.frame.Ncharacter(): > mkDF <- function(mv, displaydigits = 4, stringsAsFactors = FALSE) { + stopifnot(is.numeric(mv) || inherits(mv, "mpfr")) + data.frame(Hex = formatHex(mv), + Bin = formatBin(mv), + BinF= formatBin(mv, scientific=FALSE), + Dec = formatDec(mv), + Dec4= formatDec(mv, displaydigits=displaydigits), + Dec.= formatDec(mv, scientific=TRUE), + Dec.4=formatDec(mv, scientific=TRUE, displaydigits=displaydigits), + stringsAsFactors = stringsAsFactors) + } > nmsMkDF <- c("Hex", "Bin", "BinF", "Dec", "Dec4", "Dec.", "Dec.4") > > d10p. <- mkDF(TenPowers) > (d10p <- mkDF(as.vector(TenPowers))) Hex Bin BinF 1 +0x1.0624dd2f1a9fcp-10 +0b1.0000011000100100110111010010111100011010100111111100p-10 +0b_________________._________10000011000100100110111010010111100011010100111111100 2 +0x1.47ae147ae147bp-7 +0b1.0100011110101110000101000111101011100001010001111011p-7 +0b_________________.______10100011110101110000101000111101011100001010001111011___ 3 +0x1.999999999999ap-4 +0b1.1001100110011001100110011001100110011001100110011010p-4 +0b_________________.___11001100110011001100110011001100110011001100110011010______ 4 +0x1.0000000000000p+0 +0b1.0000000000000000000000000000000000000000000000000000p+0 +0b________________1.0000000000000000000000000000000000000000000000000000__________ 5 +0x1.4000000000000p+3 +0b1.0100000000000000000000000000000000000000000000000000p+3 +0b_____________1010.0000000000000000000000000000000000000000000000000_____________ 6 +0x1.9000000000000p+6 +0b1.1001000000000000000000000000000000000000000000000000p+6 +0b__________1100100.0000000000000000000000000000000000000000000000________________ 7 +0x1.f400000000000p+9 +0b1.1111010000000000000000000000000000000000000000000000p+9 +0b_______1111101000.0000000000000000000000000000000000000000000___________________ 8 +0x1.3880000000000p+13 +0b1.0011100010000000000000000000000000000000000000000000p+13 +0b___10011100010000.000000000000000000000000000000000000000_______________________ 9 +0x1.86a0000000000p+16 +0b1.1000011010100000000000000000000000000000000000000000p+16 +0b11000011010100000.000000000000000000000000000000000000__________________________ Dec Dec4 Dec. Dec.4 1 0.0010000000000000000 0.0010000000000000000 1.0000000000000000e-3 1.0000000000000000e-3 2 0.010000000000000000 0.010000000000000000 1.0000000000000000e-2 1.0000000000000000e-2 3 0.10000000000000001 0.10000000000000001 1.0000000000000001e-1 1.0000000000000001e-1 4 1.0000000000000000 1.0000000000000000 1.0000000000000000e+0 1.0000000000000000e+0 5 10.000000000000000 10.000000000000000 1.0000000000000000e+1 1.0000000000000000e+1 6 100.00000000000000 100.00000000000000 1.0000000000000000e+2 1.0000000000000000e+2 7 1000.0000000000000 1000.0000000000000 1.0000000000000000e+3 1.0000000000000000e+3 8 10000.000000000000 10000.000000000000 1.0000000000000000e+4 1.0000000000000000e+4 9 100000.00000000000 100000.00000000000 1.0000000000000000e+5 1.0000000000000000e+5 > > > TenFrac <- matrix((1:10)/10, dimnames=list(1:10, expression(1/x))) > TenFrac 1/x 1 0.1 2 0.2 3 0.3 4 0.4 5 0.5 6 0.6 7 0.7 8 0.8 9 0.9 10 1.0 > stopifnot(exprs = { + is.matrix(print(formatHex(TenFrac))) # stays a matrix + is.matrix(print(formatBin(TenFrac))) + is.matrix(print(formatBin(TenFrac, scientific=FALSE))) + is.matrix(print(formatDec(TenFrac))) + }) 1/x 1 +0x1.999999999999ap-4 2 +0x1.999999999999ap-3 3 +0x1.3333333333333p-2 4 +0x1.999999999999ap-2 5 +0x1.0000000000000p-1 6 +0x1.3333333333333p-1 7 +0x1.6666666666666p-1 8 +0x1.999999999999ap-1 9 +0x1.ccccccccccccdp-1 10 +0x1.0000000000000p+0 1/x 1 +0b1.1001100110011001100110011001100110011001100110011010p-4 2 +0b1.1001100110011001100110011001100110011001100110011010p-3 3 +0b1.0011001100110011001100110011001100110011001100110011p-2 4 +0b1.1001100110011001100110011001100110011001100110011010p-2 5 +0b1.0000000000000000000000000000000000000000000000000000p-1 6 +0b1.0011001100110011001100110011001100110011001100110011p-1 7 +0b1.0110011001100110011001100110011001100110011001100110p-1 8 +0b1.1001100110011001100110011001100110011001100110011010p-1 9 +0b1.1100110011001100110011001100110011001100110011001101p-1 10 +0b1.0000000000000000000000000000000000000000000000000000p+0 1/x 1 +0b_.___11001100110011001100110011001100110011001100110011010 2 +0b_.__11001100110011001100110011001100110011001100110011010_ 3 +0b_._10011001100110011001100110011001100110011001100110011__ 4 +0b_._11001100110011001100110011001100110011001100110011010__ 5 +0b_.10000000000000000000000000000000000000000000000000000___ 6 +0b_.10011001100110011001100110011001100110011001100110011___ 7 +0b_.10110011001100110011001100110011001100110011001100110___ 8 +0b_.11001100110011001100110011001100110011001100110011010___ 9 +0b_.11100110011001100110011001100110011001100110011001101___ 10 +0b1.0000000000000000000000000000000000000000000000000000____ 1/x 1 0.10000000000000001 2 0.20000000000000001 3 0.29999999999999999 4 0.40000000000000002 5 0.50000000000000000 6 0.59999999999999998 7 0.69999999999999996 8 0.80000000000000004 9 0.90000000000000002 10 1.0000000000000000 > > TenFrac9 <- mpfr(TenFrac, precBits=9) > TenFrac9 'mpfrMatrix' of dim(.) = (10, 1) of precision 9 bits 1/x 1 0.1001 2 0.2002 3 0.2998 4 0.4004 5 0.5000 6 0.5996 7 0.6992 8 0.8008 9 0.9004 10 1.000 > data.frame(Hex = formatHex(TenFrac9), ## checking as.data.frame.Ncharacter as well + Bin = formatBin(TenFrac9), + BinF= formatBin(TenFrac9, scientific=FALSE), + Dec = formatDec(TenFrac9)) -> d9 > d9 1/x 1/x 1/x 1/x 1 +0x1.9ap-4 +0b1.10011010p-4 +0b_.___110011010 0.1001 2 +0x1.9ap-3 +0b1.10011010p-3 +0b_.__110011010_ 0.2002 3 +0x1.33p-2 +0b1.00110011p-2 +0b_._100110011__ 0.2998 4 +0x1.9ap-2 +0b1.10011010p-2 +0b_._110011010__ 0.4004 5 +0x1.00p-1 +0b1.00000000p-1 +0b_.100000000___ 0.5000 6 +0x1.33p-1 +0b1.00110011p-1 +0b_.100110011___ 0.5996 7 +0x1.66p-1 +0b1.01100110p-1 +0b_.101100110___ 0.6992 8 +0x1.9ap-1 +0b1.10011010p-1 +0b_.110011010___ 0.8008 9 +0x1.cdp-1 +0b1.11001101p-1 +0b_.111001101___ 0.9004 10 +0x1.00p+0 +0b1.00000000p+0 +0b1.00000000____ 1.000 > ## Does not print the column names but the colnames of each 1-col. matrix ("1/x"). > ## This is how R in general works { in format.data.frame() } > ## now use vectors: > tenfrac9 <- as.vector(TenFrac9) > stopifnot(identical(tenfrac9, c(TenFrac9))) > data.frame(Hex = formatHex(tenfrac9), + Bin = formatBin(tenfrac9), + BinF= formatBin(tenfrac9, scientific=FALSE), + Dec = formatDec(tenfrac9)) -> dl9 > dl9 # (now prints as d9 had printed in the past) Hex Bin BinF Dec 1 +0x1.9ap-4 +0b1.10011010p-4 +0b_.___110011010 0.1001 2 +0x1.9ap-3 +0b1.10011010p-3 +0b_.__110011010_ 0.2002 3 +0x1.33p-2 +0b1.00110011p-2 +0b_._100110011__ 0.2998 4 +0x1.9ap-2 +0b1.10011010p-2 +0b_._110011010__ 0.4004 5 +0x1.00p-1 +0b1.00000000p-1 +0b_.100000000___ 0.5000 6 +0x1.33p-1 +0b1.00110011p-1 +0b_.100110011___ 0.5996 7 +0x1.66p-1 +0b1.01100110p-1 +0b_.101100110___ 0.6992 8 +0x1.9ap-1 +0b1.10011010p-1 +0b_.110011010___ 0.8008 9 +0x1.cdp-1 +0b1.11001101p-1 +0b_.111001101___ 0.9004 10 +0x1.00p+0 +0b1.00000000p+0 +0b1.00000000____ 1.000 > > stopifnot(exprs = { + getPrec(TenFrac ) == 53 + getPrec(TenFrac9) == 9 + colnames(d9) == c("Hex", "Bin", "BinF", "Dec") + inherits(d9, "data.frame") + all.equal(dim(d9), c(10,4)) + }) > > (Ten <- matrix(1:10 + 0.0, dimnames=list(1:10, "x"))) ## + 0.0 forces double precision x 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 > > dT <- mkDF(Ten) > dt <- mkDF(as.vector(Ten)) > dt # large Hex Bin BinF Dec 1 +0x1.0000000000000p+0 +0b1.0000000000000000000000000000000000000000000000000000p+0 +0b___1.0000000000000000000000000000000000000000000000000000 1.0000000000000000 2 +0x1.0000000000000p+1 +0b1.0000000000000000000000000000000000000000000000000000p+1 +0b__10.000000000000000000000000000000000000000000000000000_ 2.0000000000000000 3 +0x1.8000000000000p+1 +0b1.1000000000000000000000000000000000000000000000000000p+1 +0b__11.000000000000000000000000000000000000000000000000000_ 3.0000000000000000 4 +0x1.0000000000000p+2 +0b1.0000000000000000000000000000000000000000000000000000p+2 +0b_100.00000000000000000000000000000000000000000000000000__ 4.0000000000000000 5 +0x1.4000000000000p+2 +0b1.0100000000000000000000000000000000000000000000000000p+2 +0b_101.00000000000000000000000000000000000000000000000000__ 5.0000000000000000 6 +0x1.8000000000000p+2 +0b1.1000000000000000000000000000000000000000000000000000p+2 +0b_110.00000000000000000000000000000000000000000000000000__ 6.0000000000000000 7 +0x1.c000000000000p+2 +0b1.1100000000000000000000000000000000000000000000000000p+2 +0b_111.00000000000000000000000000000000000000000000000000__ 7.0000000000000000 8 +0x1.0000000000000p+3 +0b1.0000000000000000000000000000000000000000000000000000p+3 +0b1000.0000000000000000000000000000000000000000000000000___ 8.0000000000000000 9 +0x1.2000000000000p+3 +0b1.0010000000000000000000000000000000000000000000000000p+3 +0b1001.0000000000000000000000000000000000000000000000000___ 9.0000000000000000 10 +0x1.4000000000000p+3 +0b1.0100000000000000000000000000000000000000000000000000p+3 +0b1010.0000000000000000000000000000000000000000000000000___ 10.000000000000000 Dec4 Dec. Dec.4 1 1.0000000000000000 1.0000000000000000e+0 1.0000000000000000e+0 2 2.0000000000000000 2.0000000000000000e+0 2.0000000000000000e+0 3 3.0000000000000000 3.0000000000000000e+0 3.0000000000000000e+0 4 4.0000000000000000 4.0000000000000000e+0 4.0000000000000000e+0 5 5.0000000000000000 5.0000000000000000e+0 5.0000000000000000e+0 6 6.0000000000000000 6.0000000000000000e+0 6.0000000000000000e+0 7 7.0000000000000000 7.0000000000000000e+0 7.0000000000000000e+0 8 8.0000000000000000 8.0000000000000000e+0 8.0000000000000000e+0 9 9.0000000000000000 9.0000000000000000e+0 9.0000000000000000e+0 10 10.000000000000000 1.0000000000000000e+1 1.0000000000000000e+1 > > (Ten4 <- mpfr(Ten, precBits=4)) 'mpfrMatrix' of dim(.) = (10, 1) of precision 4 bits x 1 1.00 2 2.00 3 3.00 4 4.00 5 5.00 6 6.00 7 7.00 8 8.00 9 9.00 10 10.0 > ten4 <- as.vector(Ten4) > D4 <- mkDF(Ten4) # would be printing "x" --- because we added one-column matrices !! > (d4 <- mkDF(ten4)) # printing fine ! Hex Bin BinF Dec Dec4 Dec. Dec.4 1 +0x1.0p+0 +0b1.000p+0 +0b___1.000 1.00 1.00 1.00e+0 1.00e+0 2 +0x1.0p+1 +0b1.000p+1 +0b__10.00_ 2.00 2.00 2.00e+0 2.00e+0 3 +0x1.8p+1 +0b1.100p+1 +0b__11.00_ 3.00 3.00 3.00e+0 3.00e+0 4 +0x1.0p+2 +0b1.000p+2 +0b_100.0__ 4.00 4.00 4.00e+0 4.00e+0 5 +0x1.4p+2 +0b1.010p+2 +0b_101.0__ 5.00 5.00 5.00e+0 5.00e+0 6 +0x1.8p+2 +0b1.100p+2 +0b_110.0__ 6.00 6.00 6.00e+0 6.00e+0 7 +0x1.cp+2 +0b1.110p+2 +0b_111.0__ 7.00 7.00 7.00e+0 7.00e+0 8 +0x1.0p+3 +0b1.000p+3 +0b1000.___ 8.00 8.00 8.00e+0 8.00e+0 9 +0x1.2p+3 +0b1.001p+3 +0b1001.___ 9.00 9.00 9.00e+0 9.00e+0 10 +0x1.4p+3 +0b1.010p+3 +0b1010.___ 10.0 10.0 1.00e+1 1.00e+1 > stopifnot(identical(names(d4), names(D4)), + identical(names(d4), nmsMkDF)) > > > > Two8 <- matrix(2^seq(-8, 8)) > Two8.3 <- mpfr(Two8, precBits=3) > ## formatBin > stopifnot( + identical(sapply(as(Two8.3, "mpfr")[-c(1:3, 15:17)], + function(t83) formatBin(t83, scientific=FALSE)), + c("+0b.____100", "+0b.___100", "+0b.__100", "+0b._100", "+0b.100", + "+0b1.00","+0b10.0","+0b100.","+0b100_.","+0b100__.","+0b100___."))) > ## formatDec --- these must be perfectly aligned on the "." ! > formatDec(Two8.3, digits = 3) [,1] [1,] 0.00391 [2,] 0.00781 [3,] 0.0156 [4,] 0.0312 [5,] 0.0625 [6,] 0.125 [7,] 0.250 [8,] 0.500 [9,] 1.00 [10,] 2.00 [11,] 4.00 [12,] 8.00 [13,] 16.0 [14,] 32.0 [15,] 64.0 [16,] 128. [17,] 256. > formatDec(Two8.3, digits = 8) [,1] [1,] 0.0039062500 [2,] 0.0078125000 [3,] 0.015625000 [4,] 0.031250000 [5,] 0.062500000 [6,] 0.12500000 [7,] 0.25000000 [8,] 0.50000000 [9,] 1.0000000 [10,] 2.0000000 [11,] 4.0000000 [12,] 8.0000000 [13,] 16.000000 [14,] 32.000000 [15,] 64.000000 [16,] 128.00000 [17,] 256.00000 > stopifnot( + identical(capture.output(formatDec(Two8.3, digits = 3))[2:4], + c(" [1,] 0.00391", " [2,] 0.00781", " [3,] 0.0156 ")) + , + identical(capture.output(formatDec(Two8.3, digits = 8))[c(2:3,8,13,16)], + c(" [1,] 0.0039062500", " [2,] 0.0078125000", " [7,] 0.25000000 ", + "[12,] 8.0000000 ", "[15,] 64.000000 ")) + ) > > > > proc.time() user system elapsed 0.73 0.06 0.79