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 19:04, 3 November 2020
, 3 years ago→{{header|Raku}}: DRY, pass operations as strings which requires EVAL and precludes sigiless variables
Thundergnat (talk | contribs) (→{{header|Raku}}: Add another parenthesis grouping) |
Thundergnat (talk | contribs) (→{{header|Raku}}: DRY, pass operations as strings which requires EVAL and precludes sigiless variables) |
||
Line 91:
* <code>-x{exponential operator}p</code>
* <code>-(x){exponential operator}p</code>
* <code>((-x){exponential operator}p)</code>
* <code>-(x{exponential operator}p)</code>
Also add a different grouping: <code>(1 + -x){exponential operator}p</code>
<lang perl6>
sub infix
'1 + -x**p', 1 + -x**p, '1 + -(x)**p', 1 + -(x)**p, '1 + (-x)**p', 1 + (-x)**p, '(1 + -x)**p', (1 + -x)**p, '1 + -(x**p)', 1 + -(x**p);▼
use MONKEY;
-> $x, $p { infix-exp($x, $p) } for -5, 2, -5, 3, 5, 2, 5, 3;▼
▲
"\nEven moreso: custom looser infix exponentiation is looser (lower) precedence than infix subtraction.",
('1 + -$x^$p ', '1 + (-$x)^$p ', '1 + (-($x)^$p) ', '(1 + -$x)^$p ', '1 + -($x^$p) ')
-> $message, $ops {
say $message;
-> $op { printf " │ %s = %4d", $op, EVAL $op } for |$ops;
print "\n";
}
▲say "\nEven moreso: custom looser infix exponentiation is looser (lower) precedence than infix subtraction.";
}</lang>
▲ '1 + -x^p ', 1 + -x^p, '1 + -(x)^p ', 1 + -(x)^p, '1 + (-x)^p ', 1 + (-x)^p, '(1 + -x)^p ', (1 + -x)^p, '1 + -(x^p) ', 1 + -(x^p);
{{out}}
<pre>Default precedence: infix exponentiation is tighter (higher) precedence than unary negation.
x = -5 p = 2 │ 1 + -$x**$p = -24 │ 1 +
x = -5 p = 3 │ 1 + -$x**$p = 126 │ 1 +
x = 5 p = 2 │ 1 + -$x**$p = -24 │ 1 +
x = 5 p = 3 │ 1 + -$x**$p = -124 │ 1 +
Easily modified: custom loose infix exponentiation is looser (lower) precedence than unary negation.
x = -5 p = 2 │ 1 + -
x = -5 p = 3 │ 1 + -
x = 5 p = 2 │ 1 + -
x = 5 p = 3 │ 1 + -
Even moreso: custom looser infix exponentiation is looser (lower) precedence than infix subtraction.
x = -5 p = 2 │ 1 + -$x^$p = 36 │ 1 +
x = -5 p = 3 │ 1 + -$x^$p = 216 │ 1 +
x = 5 p = 2 │ 1 + -$x^$p = 16 │ 1 +
x = 5 p = 3 │ 1 + -$x^$p = -64 │ 1 +
=={{header|REXX}}==
|