Why does order of operation [PEMDAS] within functions work right in R?
These work fine:
-27^(1/3) == -3 # TRUE
-27^0.3333333333333333 == -3 # TRUE
But when used inside a function the result is NaN
foo <- function(x, y){return(x^(1/y))}
foo(x = -27, y = 3) # NaN
bar <- function(x, y){
a = 1 / y
b = x^a
return(b)
}
bar(x = -27, y = 3) # NaN
Thanks in advance for the explanation.