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

 
(9 intermediate revisions by 8 users not shown)
Line 74:
</pre>
Ada provides an exponentiation operator for integer types and floating point types.
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 122:
end Main;
 
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 140:
In Algol 68, all unary operators have a higher precedence than any binary operator.
<br>Algol 68 also allows UP and ^ for the exponentiation operator.
<langsyntaxhighlight lang="algol68">BEGIN
# show the results of exponentiation and unary minus in various combinations #
FOR x FROM -5 BY 10 TO 5 DO
Line 152:
OD
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 162:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f EXPONENTIATION_WITH_INFIX_OPERATORS_IN_OR_OPERATING_ON_THE_BASE.AWK
# converted from FreeBASIC
Line 175:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 185:
5 3 | -125 -125 -125 -125
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="gwbasic">S$=" ":M$=CHR$(13):?M$" X P -X^P -(X)^P (-X)^P -(X^P)":FORX=-5TO+5STEP10:FORP=2TO3:?M$MID$("+",1+(X<0));X" "PRIGHT$(S$+STR$(-X^P),8)RIGHT$(S$+STR$(-(X)^P),8)RIGHT$(S$+STR$((-X)^P),8)RIGHT$(S$+STR$(-(X^P)),8);:NEXTP,X</syntaxhighlight>
{{out}}
<pre>
 
X P -X^P -(X)^P (-X)^P -(X^P)
 
-5 2 25 25 25 -25
-5 3 125 125 125 125
+5 2 25 25 25 -25
+5 3 -125 -125 -125 -125
</pre>
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">print " x p | -x^p -(x)^p (-x)^p -(x^p)"
print ("-"*15); "+"; ("-"*45)
for x = -5 to 5 step 10
for p = 2 to 3
print " "; rjust(x,2); " "; ljust(p,2); " | "; rjust((-x^p),6); " "; rjust((-(x)^p),6); (" "*6); rjust(((-x)^p),6); " "; rjust((-(x^p)),6)
next p
next x</syntaxhighlight>
{{out}}
<pre> x p | -x^p -(x)^p (-x)^p -(x^p)
---------------+---------------------------------------------
-5 2 | -25.0 -25.0 25.0 -25.0
-5 3 | 125.0 125.0 125.0 125.0
5 2 | -25.0 -25.0 25.0 -25.0
5 3 | -125.0 -125.0 -125.0 -125.0</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="gambas">Public Sub Main()
 
Print " x p | -x^p -(x)^p (-x)^p -(x^p)"
Print "----------------+---------------------------------------------"
For x As Integer = -5 To 5 Step 10
For p As Integer = 2 To 3
Print " "; Format$(x, "-#"); " "; Format$(p, "-#"); " | "; Format$((-x ^ p), "-###"); " "; Format$((-(x) ^ p), "-###"); " "; Format$(((-x) ^ p), "-###"); " "; Format$((-(x ^ p)), "-###")
Next
Next
 
End</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|FreeBASIC}}
<syntaxhighlight lang="qbasic">PRINT " x p | -x^p -(x)^p (-x)^p -(x^p)"
PRINT "----------------+---------------------------------------------"
FOR x = -5 TO 5 STEP 10
FOR p = 2 TO 3
PRINT USING " ## ## | #### #### #### ####"; x; p; (-x ^ p); (-(x) ^ p); ((-x) ^ p); (-(x ^ p))
NEXT p
NEXT x</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|QBasic}}
{{works with|True BASIC}}
<syntaxhighlight lang="lb">print " x"; chr$(9); " p"; chr$(9); " | "; chr$(9); "-x^p"; chr$(9); " "; chr$(9); "-(x)^p"; chr$(9); " "; chr$(9); "(-x)^p"; chr$(9); " "; chr$(9); "-(x^p)"
print "----------------------------+-----------------------------------------------------------------"
for x = -5 to 5 step 10
for p = 2 to 3
print " "; x; chr$(9); " "; chr$(9); p; chr$(9); " | "; chr$(9); (-1*x^p); chr$(9); " "; chr$(9); (-1*(x)^p); chr$(9); " "; chr$(9); ((-1*x)^p); chr$(9); " "; chr$(9); (-1*(x^p))
next p
next x
end</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">PRINT " x p | -x^p -(x)^p (-x)^p -(x^p)"
PRINT "----------------+---------------------------------------------"
FOR x = -5 TO 5 STEP 10
FOR p = 2 TO 3
PRINT USING " ## ## | #### #### #### ####": x, p, (-x^p), (-(x)^p), ((-x)^p), (-(x^p))
NEXT p
NEXT x
END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">OpenConsole()
PrintN(" x p | -x^p -(x)^p (-x)^p -(x^p)")
PrintN("----------------+---------------------------------------")
For x.i = -5 To 5 Step 10
For p.i = 2 To 3
PrintN(" " + Str(x) + " " + Str(p) + " | " + StrD(Pow(-x,p),0) + #TAB$ + StrD(Pow(-1*(x),p),0) + #TAB$ + StrD(Pow((-x),p),0) + #TAB$ + " " + StrD(-1*Pow(x,p),0))
Next p
Next x
Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre> x p | -x^p -(x)^p (-x)^p -(x^p)
----------------+---------------------------------------
-5 2 | 25 25 25 -25
-5 3 | 125 125 125 125
5 2 | 25 25 25 -25
5 3 | -125 -125 -125 -125</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">PROGRAM "Exponentiation with infix operators in (or operating on) the base"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
PRINT " x p | -x**p -(x)**p (-x)**p -(x**p)"
PRINT "----------------+-----------------------------------------------"
FOR x = -5 TO 5 STEP 10
FOR p = 2 TO 3
PRINT " "; FORMAT$("##",x); " "; FORMAT$("##",p); " | "; FORMAT$("######",(-x**p)); " "; FORMAT$("######",(-(x)**p)); " "; FORMAT$("######",((-x)**p)); " "; FORMAT$("######",(-(x**p)))
NEXT p
NEXT x
END FUNCTION
END PROGRAM</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">print " x p | -x^p -(x)^p (-x)^p -(x^p)"
print "----------------+---------------------------------------------"
for x = -5 to 5 step 10
for p = 2 to 3
print " ", x using "##", " ", p using "##", " | ", (-x^p) using "####", " ", (-(x)^p) using "####", " ", ((-x)^p) using "####", " ", (-(x^p)) using "####"
next p
next x</syntaxhighlight>
{{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#}}==
F# does not support the ** operator for integers but for floats:
<langsyntaxhighlight lang="fsharp">
printfn "-5.0**2.0=%f; -(5.0**2.0)=%f" (-5.0**2.0) (-(5.0**2.0))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 197 ⟶ 341:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: infix locals prettyprint sequences
sequences.generalizations sequences.repeating ;
 
Line 211 ⟶ 355:
5 2 row
5 3 row
5 narray simple-table.</langsyntaxhighlight>
{{out}}
<pre>
Line 222 ⟶ 366:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">print " x p | -x^p -(x)^p (-x)^p -(x^p)"
print "----------------+---------------------------------------------"
for x as integer = -5 to 5 step 10
Line 229 ⟶ 373:
x;p;(-x^p);(-(x)^p);((-x)^p);(-(x^p))
next p
next x</langsyntaxhighlight>
{{out}}<pre>
x p | -x^p -(x)^p (-x)^p -(x^p)
Line 247 ⟶ 391:
 
Note that we can't call the method ''↑'' (or similar) because identifiers in Go must begin with a Unicode letter and using a non-ASCII symbol would be awkward to type on some keyboards in any case.
<langsyntaxhighlight lang="go">package main
 
import (
Line 269 ⟶ 413:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 279 ⟶ 423:
</pre>
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">--https://wiki.haskell.org/Power_function
main = do
print [-5^2,-(5)^2,(-5)^2,-(5^2)] --Integer
Line 286 ⟶ 430:
print [-5^3,-(5)^3,(-5)^3,-(5^3)] --Integer
print [-5^^3,-(5)^^3,(-5)^^3,-(5^^3)] --Fractional
print [-5**3,-(5)**3,(-5)**3,-(5**3)] --Real</langsyntaxhighlight>
{{out}}
<pre>[-25,-25,25,-25]
Line 313 ⟶ 457:
1
</pre>
 
=={{header|Julia}}==
In Julia, the ^ symbol is the power infix operator. The ^ operator has a higher precedence than the - operator,
so -5^2 is -25 and (-5)^2 is 25.
<langsyntaxhighlight lang="julia">
for x in [-5, 5], p in [2, 3]
println("x is", lpad(x, 3), ", p is $p, -x^p is", lpad(-x^p, 4), ", -(x)^p is",
lpad(-(x)^p, 5), ", (-x)^p is", lpad((-x)^p, 5), ", -(x^p) is", lpad(-(x^p), 5))
end
</langsyntaxhighlight>{{out}}
<pre>
x is -5, p is 2, -x^p is -25, -(x)^p is -25, (-x)^p is 25, -(x^p) is -25
Line 327 ⟶ 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>
 
=={{header|Lua}}==
Lua < 5.3 has a single double-precision numeric type. Lua >= 5.3 adds an integer numeric type. "^" is supported as an infix exponentiation operator for both types.
<langsyntaxhighlight lang="lua">mathtype = math.type or type -- polyfill <5.3
function test(xs, ps)
for _,x in ipairs(xs) do
Line 344 ⟶ 499:
if math.type then -- if >=5.3
test( {-5,5}, {2,3} ) -- "integer"
end</langsyntaxhighlight>
{{out}}
<pre> x type(x) p type(p) -x^p -(x)^p (-x)^p -(x^p)
Line 359 ⟶ 514:
=={{header|Maple}}==
 
<langsyntaxhighlight lang="maple">[-5**2,-(5)**2,(-5)**2,-(5**2)];
[-25, -25, 25, -25]
 
[-5**3,-(5)**3,(-5)**3,-(5**3)];
[-125, -125, -125, -125]</langsyntaxhighlight>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">{-5^2, -(5)^2, (-5)^2, -(5^2)}
{-5^3, -(5)^3, (-5)^3, -(5^3)}</langsyntaxhighlight>
{{out}}
<pre>{-25, -25, 25, -25}
Line 376 ⟶ 531:
The infix exponentiation operator is defined in standard module “math”. Its precedence is less than that of unary “-” operator, so -5^2 is 25 and -(5^2) is -25.
 
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
for x in [-5, 5]:
Line 382 ⟶ 537:
echo &"x is {x:2}, ", &"p is {p:1}, ",
&"-x^p is {-x^p:4}, ", &"-(x)^p is {-(x)^p:4}, ",
&"(-x)^p is {(-x)^p:4}, ", &"-(x^p) is {-(x^p):4}"</langsyntaxhighlight>
 
{{out}}
Line 393 ⟶ 548:
{{works with|Extended Pascal}}
Apart from the built-in (prefix) functions <tt>sqr</tt> (exponent&nbsp;=&nbsp;2) and <tt>sqrt</tt> (exponent&nbsp;=&nbsp;0.5) already defined in Standard “Unextended” Pascal (ISO standard 7185), ''Extended Pascal'' (ISO standard 10206) defines following additional (infix) operators:
<langsyntaxhighlight lang="pascal">program exponentiationWithInfixOperatorsInTheBase(output);
 
const
Line 456 ⟶ 611:
testRealPower(-5E0, 3);
testRealPower(+5E0, 3)
end.</langsyntaxhighlight>
{{out}}
=====> testIntegerPower <=====
Line 518 ⟶ 673:
=={{header|Perl}}==
Use the module <tt>Sub::Infix</tt> to define a custom operator. Note that the bracketing punctuation controls the precedence level. Results structured same as in Raku example.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Sub::Infix;
Line 534 ⟶ 689:
}
print "\n";
}</langsyntaxhighlight>
{{out}}
<pre>x: -5 p: 2 | 1 + -$x**$p -24 | 1 + (-$x)**$p 26 | 1 + (-($x)**$p) -24 | (1 + -$x)**$p 36 | 1 + -($x**$p) -24 |
Line 555 ⟶ 710:
Phix has a power() function instead of an infix operator, hence there are only two possible syntaxes, with the obvious outcomes.<br>
(Like Go, Phix does not support operator overloading or definition at all.)
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">for</span> <span style="color: #000000;">x<span style="color: #0000FF;">=<span style="color: #0000FF;">-<span style="color: #000000;">5</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">by</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">p<span style="color: #0000FF;">=<span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span>
Line 561 ⟶ 716:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 571 ⟶ 726:
 
=={{header|QB64}}==
<langsyntaxhighlight lang="qb64">For x = -5 To 5 Step 10
For p = 2 To 3
Print "x = "; x; " p ="; p; " ";
Line 579 ⟶ 734:
Print Using ", -(x^p) is ####"; -(x ^ p)
Next
Next</langsyntaxhighlight>
{{out}}
<pre>
Line 588 ⟶ 743:
</pre>
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">expressions <- alist(-x ^ p, -(x) ^ p, (-x) ^ p, -(x ^ p))
x <- c(-5, -5, 5, 5)
p <- c(2, 3, 2, 3)
Line 595 ⟶ 750:
setNames(lapply(expressions, eval), sapply(expressions, deparse)),
check.names = FALSE)
print(output, row.names = FALSE)</langsyntaxhighlight>
{{out}}
<pre> x p -x^p -(x)^p (-x)^p -(x^p)
Line 613 ⟶ 768:
Also add a different grouping: <code>(1 + -x){exponential operator}p</code>
 
<syntaxhighlight lang="raku" perl6line>sub infix:<↑> is looser(&prefix:<->) { $^a ** $^b }
sub infix:<^> is looser(&infix:<->) { $^a ** $^b }
 
for
use MONKEY;
('Default precedence: infix exponentiation is tighter (higher) precedence than unary negation.',
'1 + -$x**$p', {1 + -$^a**$^b}, '1 + (-$x)**$p', {1 + (-$^a)**$^b}, '1 + (-($x)**$p)', {1 + (-($^a)**$^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";''
}
}</langsyntaxhighlight>
{{out}}
<pre>Default precedence: infix exponentiation is tighter (higher) precedence than unary negation.
Line 647 ⟶ 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}}==
<langsyntaxhighlight lang="rexx">/*REXX program shows exponentition with an infix operator (implied and/or specified).*/
_= '─'; ! = '║'; mJunct= '─╫─'; bJunct= '─╨─' /*define some special glyphs. */
 
Line 677 ⟶ 835:
/*──────────────────────────────────────────────────────────────────────────────────────*/
@: parse arg txt, w, fill; if fill=='' then fill= ' '; return center( txt, w, fill)
</syntaxhighlight>
</lang>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 689 ⟶ 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}}==
<langsyntaxhighlight lang="ruby">nums = [-5, 5]
pows = [2, 3]
nums.product(pows) do |x, p|
puts "x = #{x} p = #{p}\t-x**p #{-x**p}\t-(x)**p #{-(x)**p}\t(-x)**p #{ (-x)**p}\t-(x**p) #{-(x**p)}"
end
</syntaxhighlight>
</lang>
{{out}}
<pre>x = -5 p = 2 -x**p -25 -(x)**p -25 (-x)**p 25 -(x**p) -25
Line 704 ⟶ 893:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import product
 
xx = '-5 +5'.split()
Line 722 ⟶ 911:
for x, p in product(xx, pp):
texts2 = [t.replace(X, x).replace(P, p) for t in texts]
print(' ', '; '.join(f"{t2} =={eval(t2):4}" for t2 in texts2))</langsyntaxhighlight>
 
{{out}}
Line 742 ⟶ 931:
=={{header|Smalltalk}}==
Smalltalk has no prefix operator for negation. To negate, you have to send the number a "negated" message, which has higher precedence than any binary message. Literal constants may have a sign (which is not an operation, but part of the constant).
<langsyntaxhighlight lang="smalltalk">a := 2.
b := 3.
Transcript show:'-5**2 => '; showCR: -5**2.
Line 751 ⟶ 940:
Transcript show:'5**b => '; showCR: 5**b.
" Transcript show:'-(5**b) => '; showCR: -(5**b). -- invalid: syntax error "
" Transcript show:'5**-b => '; showCR: 5**-b. -- invalid: syntax error "</langsyntaxhighlight>
Using the "negated" message:
<langsyntaxhighlight lang="smalltalk">Transcript show:'5 negated**2 => '; showCR: 5 negated**2. "negates 5"
Transcript show:'5 negated**3 => '; showCR: 5 negated**3.
Transcript show:'5**2 negated => '; showCR: 5**2 negated. "negates 2"
Line 766 ⟶ 955:
Transcript show:'(-5**b) negated => '; showCR: (-5**b) negated.
Transcript show:'-5 negated**2 => '; showCR: -5 negated**2. "negates the negative 5"
Transcript show:'-5 negated**3 => '; showCR: -5 negated**3.</langsyntaxhighlight>
{{out}}
<pre>
Line 793 ⟶ 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.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
class Num2 {
Line 826 ⟶ 1,013:
Fmt.print("$s = $4s", ops[3], -(x^p))
}
}</langsyntaxhighlight>
 
{{out}}
1,007

edits