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 17:32, 3 November 2020
, 3 years ago→{{header|Raku}}: Add another precedence level, demo
(Added Wren) |
Thundergnat (talk | contribs) (→{{header|Raku}}: Add another precedence level, demo) |
||
Line 86:
=={{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.
A slight departure from the task specs. Use <code>1 + {expression}</code> rather than just <code>{expression}</code> to better demonstrate the relative precedence levels. Where <code>{expression}</code> is one of:
<lang perl6>sub infix-exp (\x, \p) {▼
* <code>-x{exponential operator}p</code>
printf "x = %2d p = %2d │ %s = %4d │ %s = %4d │ %s = %4d │ %s = %4d\n", x, p,▼
* <code>-(x){exponential operator}p</code>
'-x**p', -x**p, '-(x)**p', -(x)**p, '(-x)**p', (-x)**p, '-(x**p)', -(x**p);▼
* <code>(-x){exponential operator}p</code>
* <code>-(x{exponential operator}p)</code>
<lang perl6>say 'Default precedence: infix exponentiation is tighter (higher) precedence than unary negation.';▼
▲ '1 + -x**p', 1 + -x**p, '1 + -(x)**p', 1 + -(x)**p, '1 + (-x)**p', 1 + (-x)**p, '1 + -(x**p)', 1 + -(x**p);
}
▲say 'Default precedence: infix exponentiation is tighter (higher) precedence than unary negation.';
-> $x, $p { infix-exp($x, $p) } for -5, 2, -5, 3, 5, 2, 5, 3;
say "\nEasily modified: custom loose infix exponentiation is looser (lower) precedence than unary negation.";▼
sub infix:<↑> is looser(&prefix:<->) { $^a ** $^b }
sub infix-loose-exp (\x, \p) {
printf "x = %2d 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);
}
▲say "\nEasily modified: custom loose infix exponentiation is looser (lower) precedence than unary negation.";
▲-> $x, $p { infix-loose-exp($x, $p) } for -5, 2, -5, 3, 5, 2, 5, 3;</lang>
say "\nEven moreso: custom looser infix exponentiation is looser (lower) precedence than infix subtraction.";
sub infix:<^> is looser(&infix:<->) { $^a ** $^b }
sub infix-looser-exp (\x, \p) {
printf "x = %2d p = %d │ %s = %4d │ %s = %4d │ %s = %4d │ %s = %4d\n", 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);
}
-> $x, $p { infix-looser-exp($x, $p) } for -5, 2, -5, 3, 5, 2, 5, 3;</lang>
{{out}}
<pre>Default precedence: infix exponentiation is tighter (higher) precedence than unary negation.
x = -5 p =
x = -5 p =
x = 5 p =
x = 5 p =
Easily modified: custom loose infix exponentiation is looser (lower) precedence than unary negation.
x = -5 p =
x = -5 p =
x = 5 p =
x = 5 p =
Even moreso: custom looser infix exponentiation is looser (lower) precedence than infix subtraction.
x = -5 p = 2 │ 1 + -x^p = 36 │ 1 + -(x)^p = 36 │ 1 + (-x)^p = 36 │ 1 + -(x^p) = -24
x = -5 p = 3 │ 1 + -x^p = 216 │ 1 + -(x)^p = 216 │ 1 + (-x)^p = 216 │ 1 + -(x^p) = 126
x = 5 p = 2 │ 1 + -x^p = 16 │ 1 + -(x)^p = 16 │ 1 + (-x)^p = 16 │ 1 + -(x^p) = -24
x = 5 p = 3 │ 1 + -x^p = -64 │ 1 + -(x)^p = -64 │ 1 + (-x)^p = -64 │ 1 + -(x^p) = -124</pre>
=={{header|REXX}}==
|