R version 4.4.0 beta (2024-04-09 r86391 ucrt) -- "Puppy Cup" 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. > library("matrixStats") > > for (mode in c("integer", "double")) { + cat("mode: ", mode, "\n", sep = "") + + # Empty vector + x <- 0 + storage.mode(x) <- mode + y <- prod(x, na.rm = TRUE) + print(y) + z <- product(x, na.rm = TRUE) + print(z) + stopifnot(all.equal(z, y)) + + # Test negative values + x <- c(1, -4, 2) + storage.mode(x) <- mode + y <- prod(x, na.rm = TRUE) + print(y) + z <- product(x, na.rm = TRUE) + print(z) + stopifnot(all.equal(z, y)) + + # Test missing values + x <- c(1, NA, NaN, 2) + storage.mode(x) <- mode + y <- prod(x, na.rm = TRUE) + print(y) + z <- product(x, na.rm = TRUE) + print(z) + stopifnot(all.equal(z, y)) + + x <- c(1, NA, NaN, 2) + storage.mode(x) <- mode + y <- prod(x, na.rm = FALSE) + print(y) + z <- product(x, na.rm = FALSE) + print(z) + stopifnot(all(is.na(z), is.na(y))) + + x <- c(1, NaN, 2) + storage.mode(x) <- mode + y <- prod(x, na.rm = FALSE) + print(y) + stopifnot(is.na(y)) + z <- product(x, na.rm = FALSE) + print(z) + stopifnot(is.na(z)) + + } # for (mode ...) mode: integer [1] 0 [1] 0 [1] -8 [1] -8 [1] 2 [1] 2 [1] NA [1] NA [1] NA [1] NA mode: double [1] 0 [1] 0 [1] -8 [1] -8 [1] 2 [1] 2 [1] NA [1] NA [1] NaN [1] NA > > > # NAs following 0s > x <- c(0L, NA_integer_) > y <- prod(x, na.rm = FALSE) > print(y) [1] NA > z <- product(x, na.rm = FALSE) > print(z) [1] NA > stopifnot(identical(z, y)) > > proc.time() user system elapsed 0.15 0.03 0.18