R Under development (unstable) (2023-11-06 r85483 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 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.17 0.03 0.20