Jump to content

Exponentiation with infix operators in (or operating on) the base: Difference between revisions

→‎{{header|Raku}}: re-write without EVAL, works in recent releases of Raku
(added langur language example)
(→‎{{header|Raku}}: re-write without EVAL, works in recent releases of Raku)
Line 768:
sub infix:<∧> is looser(&infix:<->) { $^a ** $^b }
 
for
use MONKEY;
('Default "\nEven moresoprecedence: custom looser infix exponentiation is loosertighter (lowerhigher) precedence than infixunary subtractionnegation."',
( '1 + -$x∧x**$p ', {1 + -$^a**$^b}, '1 + (-$x)**$p ', '{1 + (-($x^a)**$p)^b}, ', '(1 + (-($x)**$p )', '{1 + (-($x∧^a)**$p) '^b)},
'(1 + -$x)**$p', {(1 + -$^a)**$^b}, '1 + -($x**$p)', {1 + -($^a**$^b)}),
 
for 'Default precedence ("\nEasily modified: custom loose infix exponentiation is tighterlooser (higherlower) precedence than unary negation.'",
( '1 + -$x**x↑$p ', {1 + -$^a↑$^b}, '1 + (-$x)**$p ', '{1 + (-($x^a)**$p)'^b}, '(1 + (-($x)**$p) ', '{1 + (-($x**^a)↑$p)'^b)},
'(1 + -$x)↑$p ', {(1 + -$^a)↑$^b}, '1 + -($x↑$p) ', {1 + -($^a↑$^b)}),
 
("\nEasilynEven modifiedmore so: custom looselooser infix exponentiation is looser (lower) precedence than unaryinfix negationsubtraction.",
( '1 + -$x↑x∧$p ', {1 + -$^a∧$^b}, '1 + (-$x)$p ', '{1 + (-($x^a)$p)^b}, ', '(1 + (-($x)$p) ', '{1 + (-($x↑^a)∧$p) '^b)},
'(1 + -$x)∧$p ', {(1 + -$^a)∧$^b}, '1 + -($x∧$p) ', {1 + -($^a∧$^b)})
 
-> $case {
"\nEven moreso: custom looser infix exponentiation is looser (lower) precedence than infix subtraction.",
my ($title, @operations) = $case<>;
('1 + -$x∧$p ', '1 + (-$x)∧$p ', '1 + (-($x)∧$p) ', '(1 + -$x)∧$p ', '1 + -($x∧$p) ')
->say $message, $ops {title;
say $message;
for -5, 5 X 2, 3 -> ($x, $p) {
printf "x = %2d p = %d", $x, $p;
for @operations -> $oplabel, &code { printfprint " │ %s$label = %4d", $op, EVAL~ $opx.&code($p).fmt('%4d') } for |$ops;
printsay "\n";''
}
}</syntaxhighlight>
Line 799 ⟶ 802:
x = 5 p = 3 │ 1 + -$x↑$p = -124 │ 1 + (-$x)↑$p = -124 │ 1 + (-($x)↑$p) = -124 │ (1 + -$x)↑$p = -64 │ 1 + -($x↑$p) = -124
 
Even moresomore so: 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) = 26 │ (1 + -$x)∧$p = 36 │ 1 + -($x∧$p) = -24
x = -5 p = 3 │ 1 + -$x∧$p = 216 │ 1 + (-$x)∧$p = 216 │ 1 + (-($x)∧$p) = 126 │ (1 + -$x)∧$p = 216 │ 1 + -($x∧$p) = 126
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.