library(groupedHyperframe)47 lastLegal()
These packages (Note 1) are a one-person project undergoing rapid evolution. Backward compatibility (per Hadley Wickham) is provided as a courtesy rather than a guarantee.
Until further notice, these packages should
- not be used as a basis for research grant applications,
- not be cited as an actively maintained tool in a peer-reviewed manuscript,
- not be used to support or fulfill requirements for pursuing an academic degree.
In addition, work primarily based on these packages (Note 1) should not be presented at academic conferences or similar scholarly venues.
Furthermore, a person’s ability to use these packages (Note 1) does not necessarily imply an understanding of their underlying mechanisms. Accordingly, demonstration of their use alone should not be considered sufficient evidence of expertise, nor should it be credited as a basis for academic promotion or advancement.
These statements do not apply to the contributors (Tip 1) to these packages (Note 1) with respect to their specific contributions.
These statements do not apply when the maintainer of these packages (Note 1), Tingting Zhan, is credited as the first author, the lead author, and/or the corresponding author in a peer-reviewed manuscript, or as the Principal Investigator or Co-Principal Investigator in a research grant application and/or a final research progress report.
These statements are advisory in nature and do not modify or restrict the rights granted under the GNU General Public License https://www.r-project.org/Licenses/.
The examples in Chapter 47 require
Due to the floating-point precision of R (Listing 47.1), ratio of two double-precision scalars may take exceptional/illegal return of
0from \(0/\delta\), orInffrom \(\delta/0\), with a real number \(\delta\geq\) (approximately)2.6e-324NaNfrom \(0/\varepsilon\) or \(\varepsilon/0\), with a real number \(\varepsilon\leq\) (approximately)2.5e-324
list(
0 / c(2.6e-324, 2.5e-324),
c(2.5e-324, 2.6e-324) / 0
)
# [[1]]
# [1] 0 NaN
#
# [[2]]
# [1] NaN InfThe function lastLegal() (Listing 47.5) returns the index of the last consecutive legal values in a double (Listing 47.2) vector, i.e., the first exceptional/illegal value of 0, Inf, or NaN appears at the next index. The term “legal” indicates
a
doublescalar being not-NA_real_, not-NaN, not-Inf(Listing 47.3), and withabsolute value greater than.Machine$double.eps(Listing 47.4).
NaN and Inf are double, not integer (R Core Team 2026)
list(Inf, NaN) |>
vapply(FUN = typeof, FUN.VALUE = '')
# [1] "double" "double"is.finite()
c(NA_real_, NaN, Inf) |>
is.finite()
# [1] FALSE FALSE FALSElastLegal(), definition of legal
as.list(body(lastLegal))[[2L]]
# vok <- is.finite(v) & (abs(v) > .Machine$double.eps)lastLegal(), toy examples
list(
toy1 = c(exp(1), pi),
toy2 = c(exp(1), pi, NaN),
toy3 = c(exp(1), pi, NaN, 1, 0, Inf)
) |>
lapply(FUN = lastLegal)
# $toy1
# [1] 2
# attr(,"value")
# [1] 3.141593
#
# $toy2
# [1] 2
# attr(,"value")
# [1] 3.141593
#
# $toy3
# [1] 2
# attr(,"value")
# [1] 3.141593