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

m (syntax highlighting fixup automation)
 
(6 intermediate revisions by 5 users not shown)
Line 320:
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
for x in [ -5 5 ]
for p in [ 2 3 ]
print x & "^" & p & " = " & pow x p
.
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 448 ⟶ 457:
1
</pre>
 
=={{header|Julia}}==
In Julia, the ^ symbol is the power infix operator. The ^ operator has a higher precedence than the - operator,
Line 462 ⟶ 472:
x is 5, p is 2, -x^p is -25, -(x)^p is -25, (-x)^p is 25, -(x^p) is -25
x is 5, p is 3, -x^p is-125, -(x)^p is -125, (-x)^p is -125, -(x^p) is -125
</pre>
 
=={{header|langur}}==
<syntaxhighlight lang="langur">writeln [-5^2, -(5)^2, (-5)^2, -(5^2)]
writeln [-5^3, -(5)^3, (-5)^3, -(5^3)]
</syntaxhighlight>
 
{{out}}
<pre>[-25, -25, 25, -25]
[-125, -125, -125, -125]
</pre>
 
Line 749 ⟶ 769:
 
<syntaxhighlight lang="raku" line>sub infix:<↑> is looser(&prefix:<->) { $^a ** $^b }
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^**$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 782 ⟶ 805:
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^x∧$p = 36 │ 1 + (-$x)^$p = 36 │ 1 + (-($x)^$p) = 26 │ (1 + -$x)^$p = 36 │ 1 + -($x^x∧$p) = -24
x = -5 p = 3 │ 1 + -$x^x∧$p = 216 │ 1 + (-$x)^$p = 216 │ 1 + (-($x)^$p) = 126 │ (1 + -$x)^$p = 216 │ 1 + -($x^x∧$p) = 126
x = 5 p = 2 │ 1 + -$x^x∧$p = 16 │ 1 + (-$x)^$p = 16 │ 1 + (-($x)^$p) = 26 │ (1 + -$x)^$p = 16 │ 1 + -($x^x∧$p) = -24
x = 5 p = 3 │ 1 + -$x^x∧$p = -64 │ 1 + (-$x)^$p = -64 │ 1 + (-($x)^$p) = -124 │ (1 + -$x)^$p = -64 │ 1 + -($x^x∧$p) = -124</pre>
 
=={{header|REXX}}==
Line 824 ⟶ 847:
───── ──────╨─────────── ───────╨─────────── ───────╨─────────── ───────╨─────────── ──────
</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}}==
<syntaxhighlight lang="ruby">nums = [-5, 5]
Line 928 ⟶ 982:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
Wren uses the pow() method for exponentiation of numbers and, whilst it supports operator overloading, there is no way of adding a suitable infix operator to the existing Num class or inheriting from that class.
 
Also inheriting from the Num class is not recommended and will probably be banned altogether from the next version.
 
However, what we can do is to wrap Num objects in a new Num2 class and then add exponentiation and unary minus operators to that.
 
Ideally what we'd like to do is to use a new operator such as '**' for exponentiation (because '^' is the bitwise exclusive or operator) but we can only overload existing operators with their existing precedence and so, for the purposes of this task, '^' is the only realistic choice.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
class Num2 {
889

edits