Exponentiation with infix operators in (or operating on) the base: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (→{{header|Raku}}: re-write without EVAL, works in recent releases of Raku) |
|||
Line 844: | Line 844: | ||
───── ──────╨─────────── ───────╨─────────── ───────╨─────────── ───────╨─────────── ────── |
───── ──────╨─────────── ───────╨─────────── ───────╨─────────── ───────╨─────────── ────── |
||
</pre> |
</pre> |
||
=={{header|RPL}}== |
|||
Using infix exponentiation as required, even if not RPLish: |
|||
≪ → x p |
|||
≪ '''{ 'x' 'p' '-x^p' '-(x)^p' '(-x)^p' '-(x^p)' }''' 1 |
|||
DO GETI EVAL ROT ROT UNTIL DUP 1 == END |
|||
DROP 7 ROLLD 6 →LIST |
|||
≫ ≫ 'SHOXP' STO |
|||
{{in}} |
|||
<pre> |
|||
-5 2 SHOXP |
|||
-5 3 SHOXP |
|||
5 2 SHOXP |
|||
5 3 SHOXP |
|||
</pre> |
|||
{{out}} |
|||
<pre> |
|||
8: { 'x' 'p' '-x^p' '-x^p' '(-x)^p' '-x^p' } |
|||
7: { -5 2 -25 -25 25 -25 } |
|||
6: { 'x' 'p' '-x^p' '-x^p' '(-x)^p' '-x^p' } |
|||
5: { -5 3 125 125 125 125 } |
|||
4: { 'x' 'p' '-x^p' '-x^p' '(-x)^p' '-x^p' } |
|||
3: { 5 2 -25 -25 25 -25 } |
|||
2: { 'x' 'p' '-x^p' '-x^p' '(-x)^p' '-x^p' } |
|||
1: { 5 3 -125 -125 -125 -125 } |
|||
</pre> |
|||
Original infix expressions (see code above in bold characters) have been simplified by the interpreter when storing the program. |
|||
In reverse Polish notation, there is only one way to answer the task: |
|||
≪ → x p |
|||
≪ x NEG p ^ |
|||
≫ ≫ 'SHOXP' STO |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight lang="ruby">nums = [-5, 5] |
<syntaxhighlight lang="ruby">nums = [-5, 5] |