Skip to content

Commit 07b8134

Browse files
author
maechler
committed
pretty(): get bounds arg; chkDots; fix typo in (and extend) old checks for eps.correct=2
git-svn-id: https://svn.r-project.org/R/trunk@88881 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 7182176 commit 07b8134

File tree

4 files changed

+58
-17
lines changed

4 files changed

+58
-17
lines changed

doc/NEWS.Rd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@
130130
replacement versions now accept \code{i2 = NULL} to mean \dQuote{to
131131
the end_of_string}, thus fulfilling \I{Kevin Ushey} (and others')
132132
suggestions in \PR{18851}.
133+
134+
\item The default method for \code{pretty()} gets a new switch \code{bounds} for
135+
completeness, corresponding to \code{.pretty()}. Also, it now
136+
catches typos such as \code{eps.corrected = 2}, noted in \PR{18521}
137+
by \I{Mikko Korpela}.
133138
}
134139
}
135140

src/library/base/R/pretty.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# File src/library/base/R/pretty.R
22
# Part of the R package, https://www.R-project.org
33
#
4-
# Copyright (C) 1995-2021 The R Core Team
4+
# Copyright (C) 1995-2025 The R Core Team
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -30,13 +30,16 @@ pretty <- function(x, ...) UseMethod("pretty")
3030
pretty.default <-
3131
function(x, n = 5L, min.n = n %/% 3L, shrink.sml = 0.75,
3232
high.u.bias = 1.5, u5.bias = .5 + 1.5*high.u.bias,
33-
eps.correct = 0L, f.min = 2^-20, ...)
33+
eps.correct = 0L, f.min = 2^-20, bounds = TRUE, ...)
3434
{
35+
chkDots(...) # avoid typos
3536
x <- x[is.finite(x <- as.numeric(x))]
3637
if(!length(x)) return(x)
3738
z <- .Internal(pretty(min(x), max(x), n, min.n, shrink.sml,
38-
c(high.u.bias, u5.bias, f.min), eps.correct, TRUE))
39+
c(high.u.bias, u5.bias, f.min), eps.correct, bounds))
3940
n <- z$n
41+
if(!bounds) z <- list(l = z$ns * z$unit,
42+
u = z$nu * z$unit)
4043
s <- seq.int(z$l, z$u, length.out = n + 1L)
4144
if(!eps.correct && n) { # maybe zap smalls from seq() rounding errors
4245
## better than zapsmall(s, digits = 14) :

src/library/base/man/pretty.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pretty(x, \dots)
1313

1414
\method{pretty}{default}(x, n = 5, min.n = n \%/\% 3, shrink.sml = 0.75,
1515
high.u.bias = 1.5, u5.bias = .5 + 1.5*high.u.bias,
16-
eps.correct = 0, f.min = 2^-20, \dots)
16+
eps.correct = 0, f.min = 2^-20, bounds = TRUE, \dots)
1717

1818
.pretty(x, n = 5L, min.n = n \%/\% 3, shrink.sml = 0.75,
1919
high.u.bias = 1.5, u5.bias = .5 + 1.5*high.u.bias,

tests/reg-tests-1d.R

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ getVaW <- function(expr) {
1414
invokeRestart("muffleWarning") })
1515
structure(val, warning = W)
1616
}
17+
(sysinf <- Sys.info())
18+
Lnx <- sysinf[["sysname"]] == "Linux"
19+
isMac <- sysinf[["sysname"]] == "Darwin"
20+
arch <- sysinf[["machine"]]
21+
x86 <- arch == "x86_64"
1722
onWindows <- .Platform$OS.type == "windows"
18-
.M <- .Machine
19-
str(.M[grep("^sizeof", names(.M))]) ## also differentiate long-double..
20-
b64 <- .M$sizeof.pointer == 8
23+
str(.Machine[grep("^sizeof", names(.Machine))]) ## also differentiate long-double..
24+
(b64 <- .Machine$sizeof.pointer == 8L)
25+
(noLdbl <- .Machine$sizeof.longdouble <= 8L) ## TRUE when --disable-long-double
26+
(longD16 <- .Machine$sizeof.longdouble >= 16L)
27+
2128
options(nwarnings = 10000, # (rather than just 50)
2229
width = 99) # instead of 80
2330

@@ -2772,7 +2779,6 @@ spois <- summary( poisfit)
27722779
sqpois <- summary(qpoisfit)
27732780
sqpois.d1 <- summary(qpoisfit, dispersion=1)
27742781
SE1 <- sqrt(diag(V <- vcov(poisfit)))
2775-
(noLdbl <- (.Machine$sizeof.longdouble <= 8)) ## TRUE when --disable-long-double
27762782
stopifnot(exprs = { ## Same variances and same as V
27772783
all.equal(vcov(spois), V)
27782784
all.equal(vcov(qpoisfit, dispersion=1), V) ## << was wrong
@@ -4886,10 +4892,11 @@ for(i.n in seq_along(ns)) {
48864892
stopifnot(abs(rr-1) < 3.3/ns)
48874893
## many of these pretty() calls errored (because internally gave Inf) in R <= 4.1.0
48884894
##
4895+
48894896
##---------------- very small ranges ------------------
48904897
## The really smallest positive number (unless subnormals do "not exist"):
48914898
mm <- with(.Machine, double.xmin * double.eps)
4892-
log2(mm) == -1074 # T
4899+
log2(mm) == -1074 # TRUE (everywhere ??)
48934900
## "of course", this an extreme *sub normal* number, e.g.
48944901
mm == c(0.50001, 1.49999) * mm # TRUE TRUE (!)
48954902
(1.5*mm) / mm # 2 (!!)
@@ -4904,23 +4911,49 @@ fsS <- fs[fs <= 0.75]
49044911
options(warn=0) # (collect warnings)
49054912
psmm <- lapply(h.u, function(hu)
49064913
lapply(fsS, function(f)
4907-
lapply(nns, pretty, x = c(0, mm/f), high.u=hu, eps.correction = 2)))
4908-
summary(warnings())## many; mostly "very small range 'cell'=0, corrected to 2.122e-314"
4914+
lapply(nns, pretty, x = c(0, mm/f), high.u.bias=hu)))
4915+
summary(warnings())## many "very small range 'cell'=<nnn>e+32<n>, corrected to 2.122e-314"
49094916
(T <- table(psA <- unlist(psmm))) # is this portable?
49104917
(nT <- as.numeric(names(T)))
49114918
range(rEd <- abs(2e-314/diff(nT) - 1))
4912-
stopifnot(nT >= 0, length(nT) == 11,
4913-
rEd <= 2^-50) # only seen rEd == 0
4914-
##
4919+
stopifnot(exprs = {
4920+
nT >= 0
4921+
(nn <- length(nT)) <= 15 # always = 11 on Lnx 64b
4922+
7 <= nn
4923+
rEd <= if(b64) 2^-50 else 0.9 # Lnx 64b: only seen rEd == 0; 32bit ppc : 0.8 (!)
4924+
})
4925+
## This used to be _very_ slow in R <= 4.5.1 because it produced _HUGE_ (non-pretty!) vectors;
4926+
## On Linux, an OS daemon would typically kill the R process for using too much resources:
4927+
psm2 <- lapply(h.u, function(hu) {
4928+
## cat(sprintf("hu:%6g -- f =", hu)); on.exit(cat("\n"))
4929+
lapply(fsS, function(f) {
4930+
## cat(sprintf(" %g", f))
4931+
lapply(nns, \(n) pretty(c(0, mm/f), n=n, high.u.bias=hu, eps.correct = 2))
4932+
})
4933+
})
4934+
apply(sapply(psmm, \(L) sapply(L,lengths)), 2L, quantile)
4935+
apply(sapply(psm2, \(L) sapply(L,lengths)), 2L, quantile)
49154936
psmm.o <- lapply(h.u, function(hu)
49164937
lapply(fsS, function(f) # older R: f.min = 20 hardwired:
49174938
lapply(nns, pretty, x = c(0, mm/f), high.u=hu, f.min = 20) ))
49184939
summary(warnings())## many; mostly "very small range 'cell'=0, corrected to 4.45015e-307"
49194940
(To <- table(psAo <- signif(unlist(psmm.o), 13)))
49204941
(nTo <- as.numeric(names(To)))
4921-
range(rEdo <- abs(5e-307/diff(nTo) - 1))
4922-
stopifnot(nTo >= 0, length(nTo) == 11,
4923-
rEdo <= 2^-44) # seen max of 2^-51 on Lnx_64; 2^-44.5 on Win64
4942+
range(rEdo <- abs(5e-307/diff(nTo) - 1)) # 0 2.33e-15
4943+
r1 <- apply(sapply(psmm, \(L) sapply(L,lengths)), 2L, range)
4944+
r2 <- apply(sapply(psm2, \(L) sapply(L,lengths)), 2L, range)
4945+
r3 <- apply(sapply(psmm.o,\(L) sapply(L,lengths)), 2L, range)
4946+
stopifnot(exprs = {
4947+
nTo >= 0
4948+
(nn <- length(nTo)) <= 15 ## length(nTo) == 11
4949+
7 <= nn
4950+
rEdo <= if(b64) 2^-44 else 0.9 # Lnx 64b: seen max of 2^-48.608 (prev. 2^-51) Lnx_64; 2^-44.5 on Win64; ppc ??
4951+
if(b64 && x86) { ## platform ?
4952+
r1 == c(2, 11)
4953+
r2 == c(3, 11)
4954+
r3 == c(1, 11)
4955+
} else TRUE
4956+
})
49244957

49254958

49264959
## graphics::axis(), but also *engine* GScale() / GPretty() etc

0 commit comments

Comments
 (0)