Exponentiation with infix operators in (or operating on) the base: Difference between revisions
Exponentiation with infix operators in (or operating on) the base (view source)
Revision as of 20:17, 6 May 2021
, 3 years agoAdded R.
(Put all power functions in Haskell) |
ReeceGoding (talk | contribs) (Added R.) |
||
Line 401:
x = 5 p = 3 -x^p is -125, -(x)^p is -125, (-x)^p is -125, -(x^p) is -125
</pre>
=={{header|R}}==
<lang R>expressions <- alist(-x ^ p, -(x) ^ p, (-x) ^ p, -(x ^ p))
x <- c(-5, -5, 5, 5)
p <- c(2, 3, 2, 3)
output <- data.frame(x,
p,
setNames(lapply(expressions, eval), sapply(expressions, deparse)),
check.names = FALSE)
print(output, row.names = FALSE)</lang>
{{out}}
<pre> x p -x^p -(x)^p (-x)^p -(x^p)
-5 2 -25 -25 25 -25
-5 3 125 125 125 125
5 2 -25 -25 25 -25
5 3 -125 -125 -125 -125</pre>
=={{header|Raku}}==
In Raku by default, infix exponentiation binds tighter than unary negation. It is trivial however to define your own infix operators with whatever precedence level meets the needs of your program.
|