RosettaCodeData/Task/Tropical-algebra-overloading/R/tropical-algebra-overloading.r
2023-07-01 13:44:08 -04:00

18 lines
575 B
R

"%+%"<- function(x, y) max(x, y)
"%*%" <- function(x, y) x + y
"%^%" <- function(x, y) {
stopifnot(round(y) == y && y > 0)
x * y
}
cat("2 %*% -2 ==", 2 %*% -2, "\n")
cat("-0.001 %+% -Inf ==", 0.001 %+% -Inf, "\n")
cat("0 %*% -Inf ==", 0 %*% -Inf, "\n")
cat("1.5 %+% -1 ==", 1.5 %+% -1, "\n")
cat("-0.5 %*% 0 ==", -0.5 %*% 0, "\n")
cat("5^7 ==", 5 %^% 7, "\n")
cat("5 %*% (8 %+% 7)) ==", 5 %*% (8 %+% 7), "\n")
cat("5 %*% 8 %+% 5 %*% 7 ==", (5 %*% 8) %+% (5 %*% 7), "\n")
cat("5 %*% 8 %+% 5 %*% 7 == 5 %*% (8 %+% 7))", 5 %*% (8 %+% 7) == (5 %*% 8) %+% (5 %*% 7), "\n")