roundVersions
round (55.55, 1)
roundX(55.55, 1, "r3")
## round() with all roundVersions; quite simple (w/ recycling!)
roundAll # shows the function's definition
roundAll(55.55, 1)
roundAll(55.555, 2)
roundAll(55.5555, 3)
roundAll(55.55555, 4)
roundAll(55.555555, 5)
roundAll(55.5555555, 6)
## other "controversial" cases
rEx <- cbind( x = c(10.7775, 12.345, 9.18665),
digits = c( 3 , 2 , 4 ))
resEx <- matrix(, length(roundVersions), nrow(rEx),
dimnames = list(roundVersions, as.character(rEx[,"x"])))
for(i in 1:nrow(rEx))
resEx[,i] <- roundAll(rEx[[i,"x"]], digits = rEx[[i,"digits"]])
resEx # r0.C & r2* agree and differ from the r1*;
# "r3*" is close to "r2*" but not for 12.345
## The parts of "r3" :
r3rE <- sapply(1:nrow(rEx), function(i)
round_r3(rEx[[i,"x"]], rEx[[i,"digits"]], info=TRUE))
colnames(r3rE) <- sapply(rEx[,"x"], format)
r3rE # rounding to even when D=0, but not when D < 0
## "Deterministic" Simulation - few digits only:
long <- interactive() # save time/memory e.g. when checking
I <- if(long) 0:9999 else 0:999
Ix <- I + 0.5
ndI <- 1L + as.integer(log10(pmax(1,I))) # number of (decimal) digits of I
nd2 <- outer(ndI, if(long) -3:4 else -2:3, `+`)
x <- c(t( Ix / (10^nd2) ))
nd2 <- c(t( nd2 ))
x <- x [nd2 > 0]
nd2 <- nd2[nd2 > 0]
rx <- roundAll(x, digits = nd2)
formatF <- function(.) format(., scientific=FALSE, drop0trailing=TRUE)
rownames(rx) <- formatF(x)
options(width = 123)
noquote(cbind(d = nd2, formatF(rx))[1:140,])
## -> The first cases already show a diverse picture; sprintf() a bit as outlier
## Error, assuming "r3" to be best, as it *does* really go to nearest:
Err <- rx - rx[, "r3"]
## careful : allowing small "noise" differences:
tErr <- abs(Err) > 1e-3* 10^-nd2 # "truly" differing from "r3"
colSums(tErr) ## --> old R "r1*" is best here, then sprintf (among non-r3):
## For F30 Linux 64-bit (gcc), and this selection of cases, r0+r2 are worst; r1 is best
## sprintf r0.C r1.C r1a.C r2.C r2a.C r3.C r3d.C r3
## 15559 19778 14078 14078 19778 19778 8 0 0 { long }
## 1167 1457 1290 1290 1457 1457 0 0 0 { !long }
if(long) { ## Q: where does "r3.C" differ from "r3" == "r3d.C" ? A: in 10 cases; 8 "real"
i3D <- which(Err[,"r3.C"] != 0)
print(cbind(d = nd2[i3D], formatF(rx[i3D,])), quote=FALSE)
print.table(zapsmall(Err[i3D,]), zero.print = ".")# differences (not very small ones!)
}
## Visualization of error happening (FIXME: do zapsmall()-like not count "noise")
cumErr <- apply(tErr[,colnames(rx) != "r3"], 2L, cumsum)
matPm <- function(y) {
matplot(y=y, type = "l", lwd = 2, xlab = "i", ylab = deparse(substitute(y)))
abline(h = 0, lty=2, col="gray")
legend("topleft", legend = setdiff(roundVersions, "r3"),
col = 1:6, lty = 1:5, lwd = 2, bty = "n")
}
matPm(head(cumErr, 100)) # sprintf seems worst
matPm(head(cumErr, 250)) # now r0+2 is worst, sprintf best
matPm(head(cumErr, 1000)) # now sprintf clearly worst again
matPm(head(cumErr, 2000)) # 0r/r2 best sprintf catching up
if(long) {
matPm(head(cumErr, 5000)) # now sprintf clearly worst again
matPm(head(cumErr,10000)) # now r0+2 is worst, r1 best
}
matPm( cumErr )
same_cols <- function(m) all(m == m[,1])
stopifnot(same_cols(Err[, c("r0.C", "r2.C", "r2a.C")]))
stopifnot(same_cols(Err[, c("r1.C", "r1a.C")]))
if(FALSE) ## *not* in 'long' case, see above
stopifnot(same_cols(Err[, c("r3", "r3.C", "r3d.C")]))
sp <- search()
if(long && require("Matrix")) {
showSp <- function(m) print(image(as(m, "sparseMatrix"), aspect = 4,
## fails, bug in lattice? useRaster = !dev.interactive(TRUE) && (nrow(m) >= 2^12),
border.col = if(nrow(m) < 1e3) adjustcolor(1, 1/2) else NA))
showSp(head(Err, 100))
showSp(head(Err, 1000))
showSp(Err)
showSp(Err != 0) # B&W version ..
if(!any(sp == "package:Matrix")) detach("package:Matrix")
}
## More digits random sample simulation tend go against "sprintf";
## see ../tests/ and also the vignette
Run the code above in your browser using DataLab