47  lastLegal()

ImportantDisclaimer

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/.

Note

The examples in Chapter 47 require

library(groupedHyperframe)

Due to the floating-point precision of R (Listing 47.1), ratio of two double-precision scalars may take exceptional/illegal return of

Listing 47.1: Review: exceptional/illegal ratio due to floating-point precision (R Core Team 2026)
list(
  0 / c(2.6e-324, 2.5e-324),
  c(2.5e-324, 2.6e-324) / 0
)
# [[1]]
# [1]   0 NaN
# 
# [[2]]
# [1] NaN Inf

The 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 double scalar being not-NA_real_, not-NaN, not-Inf (Listing 47.3), and with absolute value greater than .Machine$double.eps (Listing 47.4).

Listing 47.2: Advanced: NaN and Inf are double, not integer (R Core Team 2026)
list(Inf, NaN) |> 
  vapply(FUN = typeof, FUN.VALUE = '')
# [1] "double" "double"
Listing 47.3: Advanced: function is.finite()
c(NA_real_, NaN, Inf) |>
  is.finite()
# [1] FALSE FALSE FALSE
Listing 47.5: Example: function 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