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

Content added Content deleted
(Applesoft BASIC)
m (syntax highlighting fixup automation)
Line 74: Line 74:
</pre>
</pre>
Ada provides an exponentiation operator for integer types and floating point types.
Ada provides an exponentiation operator for integer types and floating point types.
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 122: Line 122:
end Main;
end Main;


</syntaxhighlight>
</lang>
{{output}}
{{output}}
<pre>
<pre>
Line 140: Line 140:
In Algol 68, all unary operators have a higher precedence than any binary operator.
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.
<br>Algol 68 also allows UP and ^ for the exponentiation operator.
<lang algol68>BEGIN
<syntaxhighlight lang="algol68">BEGIN
# show the results of exponentiation and unary minus in various combinations #
# show the results of exponentiation and unary minus in various combinations #
FOR x FROM -5 BY 10 TO 5 DO
FOR x FROM -5 BY 10 TO 5 DO
Line 152: Line 152:
OD
OD
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 162: Line 162:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f EXPONENTIATION_WITH_INFIX_OPERATORS_IN_OR_OPERATING_ON_THE_BASE.AWK
# syntax: GAWK -f EXPONENTIATION_WITH_INFIX_OPERATORS_IN_OR_OPERATING_ON_THE_BASE.AWK
# converted from FreeBASIC
# converted from FreeBASIC
Line 175: Line 175:
exit(0)
exit(0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 188: Line 188:
=={{header|BASIC}}==
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
==={{header|Applesoft BASIC}}===
<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</lang>
<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}}
{{out}}
<pre>
<pre>
Line 200: Line 200:
</pre>
</pre>
==={{header|BASIC256}}===
==={{header|BASIC256}}===
<lang BASIC256>print " x p | -x^p -(x)^p (-x)^p -(x^p)"
<syntaxhighlight lang="basic256">print " x p | -x^p -(x)^p (-x)^p -(x^p)"
print ("-"*15); "+"; ("-"*45)
print ("-"*15); "+"; ("-"*45)
for x = -5 to 5 step 10
for x = -5 to 5 step 10
Line 206: Line 206:
print " "; rjust(x,2); " "; ljust(p,2); " | "; rjust((-x^p),6); " "; rjust((-(x)^p),6); (" "*6); rjust(((-x)^p),6); " "; rjust((-(x^p)),6)
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 p
next x</lang>
next x</syntaxhighlight>
{{out}}
{{out}}
<pre> x p | -x^p -(x)^p (-x)^p -(x^p)
<pre> x p | -x^p -(x)^p (-x)^p -(x^p)
Line 216: Line 216:


==={{header|Gambas}}===
==={{header|Gambas}}===
<lang gambas>Public Sub Main()
<syntaxhighlight lang="gambas">Public Sub Main()


Print " x p | -x^p -(x)^p (-x)^p -(x^p)"
Print " x p | -x^p -(x)^p (-x)^p -(x^p)"
Line 226: Line 226:
Next
Next


End</lang>
End</syntaxhighlight>
{{out}}
{{out}}
<pre>Same as FreeBASIC entry.</pre>
<pre>Same as FreeBASIC entry.</pre>
Line 234: Line 234:
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
{{works with|FreeBASIC}}
{{works with|FreeBASIC}}
<lang qbasic>PRINT " x p | -x^p -(x)^p (-x)^p -(x^p)"
<syntaxhighlight lang="qbasic">PRINT " x p | -x^p -(x)^p (-x)^p -(x^p)"
PRINT "----------------+---------------------------------------------"
PRINT "----------------+---------------------------------------------"
FOR x = -5 TO 5 STEP 10
FOR x = -5 TO 5 STEP 10
Line 240: Line 240:
PRINT USING " ## ## | #### #### #### ####"; x; p; (-x ^ p); (-(x) ^ p); ((-x) ^ p); (-(x ^ p))
PRINT USING " ## ## | #### #### #### ####"; x; p; (-x ^ p); (-(x) ^ p); ((-x) ^ p); (-(x ^ p))
NEXT p
NEXT p
NEXT x</lang>
NEXT x</syntaxhighlight>
{{out}}
{{out}}
<pre>Same as FreeBASIC entry.</pre>
<pre>Same as FreeBASIC entry.</pre>
Line 249: Line 249:
{{works with|QBasic}}
{{works with|QBasic}}
{{works with|True BASIC}}
{{works with|True BASIC}}
<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)"
<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 "----------------------------+-----------------------------------------------------------------"
print "----------------------------+-----------------------------------------------------------------"
for x = -5 to 5 step 10
for x = -5 to 5 step 10
Line 256: Line 256:
next p
next p
next x
next x
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
<pre>Similar to FreeBASIC entry.</pre>


==={{header|True BASIC}}===
==={{header|True BASIC}}===
<lang qbasic>PRINT " x p | -x^p -(x)^p (-x)^p -(x^p)"
<syntaxhighlight lang="qbasic">PRINT " x p | -x^p -(x)^p (-x)^p -(x^p)"
PRINT "----------------+---------------------------------------------"
PRINT "----------------+---------------------------------------------"
FOR x = -5 TO 5 STEP 10
FOR x = -5 TO 5 STEP 10
Line 268: Line 268:
NEXT p
NEXT p
NEXT x
NEXT x
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>Same as FreeBASIC entry.</pre>
<pre>Same as FreeBASIC entry.</pre>


==={{header|PureBasic}}===
==={{header|PureBasic}}===
<lang PureBasic>OpenConsole()
<syntaxhighlight lang="purebasic">OpenConsole()
PrintN(" x p | -x^p -(x)^p (-x)^p -(x^p)")
PrintN(" x p | -x^p -(x)^p (-x)^p -(x^p)")
PrintN("----------------+---------------------------------------")
PrintN("----------------+---------------------------------------")
Line 282: Line 282:
Next x
Next x
Input()
Input()
CloseConsole()</lang>
CloseConsole()</syntaxhighlight>
{{out}}
{{out}}
<pre> x p | -x^p -(x)^p (-x)^p -(x^p)
<pre> x p | -x^p -(x)^p (-x)^p -(x^p)
Line 293: Line 293:
==={{header|XBasic}}===
==={{header|XBasic}}===
{{works with|Windows XBasic}}
{{works with|Windows XBasic}}
<lang xbasic>PROGRAM "Exponentiation with infix operators in (or operating on) the base"
<syntaxhighlight lang="xbasic">PROGRAM "Exponentiation with infix operators in (or operating on) the base"


DECLARE FUNCTION Entry ()
DECLARE FUNCTION Entry ()
Line 306: Line 306:
NEXT x
NEXT x
END FUNCTION
END FUNCTION
END PROGRAM</lang>
END PROGRAM</syntaxhighlight>
{{out}}
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
<pre>Similar to FreeBASIC entry.</pre>


==={{header|Yabasic}}===
==={{header|Yabasic}}===
<lang yabasic>print " x p | -x^p -(x)^p (-x)^p -(x^p)"
<syntaxhighlight lang="yabasic">print " x p | -x^p -(x)^p (-x)^p -(x^p)"
print "----------------+---------------------------------------------"
print "----------------+---------------------------------------------"
for x = -5 to 5 step 10
for x = -5 to 5 step 10
Line 317: Line 317:
print " ", x using "##", " ", p using "##", " | ", (-x^p) using "####", " ", (-(x)^p) using "####", " ", ((-x)^p) using "####", " ", (-(x^p)) using "####"
print " ", x using "##", " ", p using "##", " | ", (-x^p) using "####", " ", (-(x)^p) using "####", " ", ((-x)^p) using "####", " ", (-(x^p)) using "####"
next p
next p
next x</lang>
next x</syntaxhighlight>
{{out}}
{{out}}
<pre>Same as FreeBASIC entry.</pre>
<pre>Same as FreeBASIC entry.</pre>
Line 323: Line 323:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
F# does not support the ** operator for integers but for floats:
F# does not support the ** operator for integers but for floats:
<lang fsharp>
<syntaxhighlight lang="fsharp">
printfn "-5.0**2.0=%f; -(5.0**2.0)=%f" (-5.0**2.0) (-(5.0**2.0))
printfn "-5.0**2.0=%f; -(5.0**2.0)=%f" (-5.0**2.0) (-(5.0**2.0))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 332: Line 332:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: infix locals prettyprint sequences
<syntaxhighlight lang="factor">USING: infix locals prettyprint sequences
sequences.generalizations sequences.repeating ;
sequences.generalizations sequences.repeating ;


Line 346: Line 346:
5 2 row
5 2 row
5 3 row
5 3 row
5 narray simple-table.</lang>
5 narray simple-table.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 357: Line 357:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>print " x p | -x^p -(x)^p (-x)^p -(x^p)"
<syntaxhighlight lang="freebasic">print " x p | -x^p -(x)^p (-x)^p -(x^p)"
print "----------------+---------------------------------------------"
print "----------------+---------------------------------------------"
for x as integer = -5 to 5 step 10
for x as integer = -5 to 5 step 10
Line 364: Line 364:
x;p;(-x^p);(-(x)^p);((-x)^p);(-(x^p))
x;p;(-x^p);(-(x)^p);((-x)^p);(-(x^p))
next p
next p
next x</lang>
next x</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
x p | -x^p -(x)^p (-x)^p -(x^p)
x p | -x^p -(x)^p (-x)^p -(x^p)
Line 382: Line 382:


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.
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.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 404: Line 404:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 414: Line 414:
</pre>
</pre>
=={{header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>--https://wiki.haskell.org/Power_function
<syntaxhighlight lang="haskell">--https://wiki.haskell.org/Power_function
main = do
main = do
print [-5^2,-(5)^2,(-5)^2,-(5^2)] --Integer
print [-5^2,-(5)^2,(-5)^2,-(5^2)] --Integer
Line 421: Line 421:
print [-5^3,-(5)^3,(-5)^3,-(5^3)] --Integer
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)] --Fractional
print [-5**3,-(5)**3,(-5)**3,-(5**3)] --Real</lang>
print [-5**3,-(5)**3,(-5)**3,-(5**3)] --Real</syntaxhighlight>
{{out}}
{{out}}
<pre>[-25,-25,25,-25]
<pre>[-25,-25,25,-25]
Line 451: Line 451:
In Julia, the ^ symbol is the power infix operator. The ^ operator has a higher precedence than the - operator,
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.
so -5^2 is -25 and (-5)^2 is 25.
<lang julia>
<syntaxhighlight lang="julia">
for x in [-5, 5], p in [2, 3]
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",
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))
lpad(-(x)^p, 5), ", (-x)^p is", lpad((-x)^p, 5), ", -(x^p) is", lpad(-(x^p), 5))
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
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 2, -x^p is -25, -(x)^p is -25, (-x)^p is 25, -(x^p) is -25
Line 466: Line 466:
=={{header|Lua}}==
=={{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.
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.
<lang lua>mathtype = math.type or type -- polyfill <5.3
<syntaxhighlight lang="lua">mathtype = math.type or type -- polyfill <5.3
function test(xs, ps)
function test(xs, ps)
for _,x in ipairs(xs) do
for _,x in ipairs(xs) do
Line 479: Line 479:
if math.type then -- if >=5.3
if math.type then -- if >=5.3
test( {-5,5}, {2,3} ) -- "integer"
test( {-5,5}, {2,3} ) -- "integer"
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre> x type(x) p type(p) -x^p -(x)^p (-x)^p -(x^p)
<pre> x type(x) p type(p) -x^p -(x)^p (-x)^p -(x^p)
Line 494: Line 494:
=={{header|Maple}}==
=={{header|Maple}}==


<lang maple>[-5**2,-(5)**2,(-5)**2,-(5**2)];
<syntaxhighlight lang="maple">[-5**2,-(5)**2,(-5)**2,-(5**2)];
[-25, -25, 25, -25]
[-25, -25, 25, -25]


[-5**3,-(5)**3,(-5)**3,-(5**3)];
[-5**3,-(5)**3,(-5)**3,-(5**3)];
[-125, -125, -125, -125]</lang>
[-125, -125, -125, -125]</syntaxhighlight>




=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>{-5^2, -(5)^2, (-5)^2, -(5^2)}
<syntaxhighlight lang="mathematica">{-5^2, -(5)^2, (-5)^2, -(5^2)}
{-5^3, -(5)^3, (-5)^3, -(5^3)}</lang>
{-5^3, -(5)^3, (-5)^3, -(5^3)}</syntaxhighlight>
{{out}}
{{out}}
<pre>{-25, -25, 25, -25}
<pre>{-25, -25, 25, -25}
Line 511: Line 511:
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.
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.


<lang Nim>import math, strformat
<syntaxhighlight lang="nim">import math, strformat


for x in [-5, 5]:
for x in [-5, 5]:
Line 517: Line 517:
echo &"x is {x:2}, ", &"p is {p:1}, ",
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}, ",
&"(-x)^p is {(-x)^p:4}, ", &"-(x^p) is {-(x^p):4}"</lang>
&"(-x)^p is {(-x)^p:4}, ", &"-(x^p) is {-(x^p):4}"</syntaxhighlight>


{{out}}
{{out}}
Line 528: Line 528:
{{works with|Extended Pascal}}
{{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:
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:
<lang pascal>program exponentiationWithInfixOperatorsInTheBase(output);
<syntaxhighlight lang="pascal">program exponentiationWithInfixOperatorsInTheBase(output);


const
const
Line 591: Line 591:
testRealPower(-5E0, 3);
testRealPower(-5E0, 3);
testRealPower(+5E0, 3)
testRealPower(+5E0, 3)
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
=====> testIntegerPower <=====
=====> testIntegerPower <=====
Line 653: Line 653:
=={{header|Perl}}==
=={{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.
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.
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use Sub::Infix;
use Sub::Infix;
Line 669: Line 669:
}
}
print "\n";
print "\n";
}</lang>
}</syntaxhighlight>
{{out}}
{{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 |
<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 690: Line 690:
Phix has a power() function instead of an infix operator, hence there are only two possible syntaxes, with the obvious outcomes.<br>
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.)
(Like Go, Phix does not support operator overloading or definition at all.)
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="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;">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>
<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 696: Line 696:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for
<span style="color: #008080;">end</span> <span style="color: #008080;">for
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 706: Line 706:


=={{header|QB64}}==
=={{header|QB64}}==
<lang qb64>For x = -5 To 5 Step 10
<syntaxhighlight lang="qb64">For x = -5 To 5 Step 10
For p = 2 To 3
For p = 2 To 3
Print "x = "; x; " p ="; p; " ";
Print "x = "; x; " p ="; p; " ";
Line 714: Line 714:
Print Using ", -(x^p) is ####"; -(x ^ p)
Print Using ", -(x^p) is ####"; -(x ^ p)
Next
Next
Next</lang>
Next</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 723: Line 723:
</pre>
</pre>
=={{header|R}}==
=={{header|R}}==
<lang rsplus>expressions <- alist(-x ^ p, -(x) ^ p, (-x) ^ p, -(x ^ p))
<syntaxhighlight lang="rsplus">expressions <- alist(-x ^ p, -(x) ^ p, (-x) ^ p, -(x ^ p))
x <- c(-5, -5, 5, 5)
x <- c(-5, -5, 5, 5)
p <- c(2, 3, 2, 3)
p <- c(2, 3, 2, 3)
Line 730: Line 730:
setNames(lapply(expressions, eval), sapply(expressions, deparse)),
setNames(lapply(expressions, eval), sapply(expressions, deparse)),
check.names = FALSE)
check.names = FALSE)
print(output, row.names = FALSE)</lang>
print(output, row.names = FALSE)</syntaxhighlight>
{{out}}
{{out}}
<pre> x p -x^p -(x)^p (-x)^p -(x^p)
<pre> x p -x^p -(x)^p (-x)^p -(x^p)
Line 748: Line 748:
Also add a different grouping: <code>(1 + -x){exponential operator}p</code>
Also add a different grouping: <code>(1 + -x){exponential operator}p</code>


<lang perl6>sub infix:<↑> is looser(&prefix:<->) { $^a ** $^b }
<syntaxhighlight lang="raku" line>sub infix:<↑> is looser(&prefix:<->) { $^a ** $^b }
sub infix:<^> is looser(&infix:<->) { $^a ** $^b }
sub infix:<^> is looser(&infix:<->) { $^a ** $^b }


Line 768: Line 768:
print "\n";
print "\n";
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Default precedence: infix exponentiation is tighter (higher) precedence than unary negation.
<pre>Default precedence: infix exponentiation is tighter (higher) precedence than unary negation.
Line 789: Line 789:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program shows exponentition with an infix operator (implied and/or specified).*/
<syntaxhighlight lang="rexx">/*REXX program shows exponentition with an infix operator (implied and/or specified).*/
_= '─'; ! = '║'; mJunct= '─╫─'; bJunct= '─╨─' /*define some special glyphs. */
_= '─'; ! = '║'; mJunct= '─╫─'; bJunct= '─╨─' /*define some special glyphs. */


Line 812: Line 812:
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
@: parse arg txt, w, fill; if fill=='' then fill= ' '; return center( txt, w, fill)
@: 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:}}
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
<pre>
Line 825: Line 825:
</pre>
</pre>
=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>nums = [-5, 5]
<syntaxhighlight lang="ruby">nums = [-5, 5]
pows = [2, 3]
pows = [2, 3]
nums.product(pows) do |x, p|
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)}"
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
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>x = -5 p = 2 -x**p -25 -(x)**p -25 (-x)**p 25 -(x**p) -25
<pre>x = -5 p = 2 -x**p -25 -(x)**p -25 (-x)**p 25 -(x**p) -25
Line 839: Line 839:


=={{header|Python}}==
=={{header|Python}}==
<lang python>from itertools import product
<syntaxhighlight lang="python">from itertools import product


xx = '-5 +5'.split()
xx = '-5 +5'.split()
Line 857: Line 857:
for x, p in product(xx, pp):
for x, p in product(xx, pp):
texts2 = [t.replace(X, x).replace(P, p) for t in texts]
texts2 = [t.replace(X, x).replace(P, p) for t in texts]
print(' ', '; '.join(f"{t2} =={eval(t2):4}" for t2 in texts2))</lang>
print(' ', '; '.join(f"{t2} =={eval(t2):4}" for t2 in texts2))</syntaxhighlight>


{{out}}
{{out}}
Line 877: Line 877:
=={{header|Smalltalk}}==
=={{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).
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).
<lang smalltalk>a := 2.
<syntaxhighlight lang="smalltalk">a := 2.
b := 3.
b := 3.
Transcript show:'-5**2 => '; showCR: -5**2.
Transcript show:'-5**2 => '; showCR: -5**2.
Line 886: Line 886:
Transcript show:'5**b => '; showCR: 5**b.
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 "
" Transcript show:'5**-b => '; showCR: 5**-b. -- invalid: syntax error "</lang>
" Transcript show:'5**-b => '; showCR: 5**-b. -- invalid: syntax error "</syntaxhighlight>
Using the "negated" message:
Using the "negated" message:
<lang smalltalk>Transcript show:'5 negated**2 => '; showCR: 5 negated**2. "negates 5"
<syntaxhighlight 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 negated**3 => '; showCR: 5 negated**3.
Transcript show:'5**2 negated => '; showCR: 5**2 negated. "negates 2"
Transcript show:'5**2 negated => '; showCR: 5**2 negated. "negates 2"
Line 901: Line 901:
Transcript show:'(-5**b) negated => '; showCR: (-5**b) negated.
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**2 => '; showCR: -5 negated**2. "negates the negative 5"
Transcript show:'-5 negated**3 => '; showCR: -5 negated**3.</lang>
Transcript show:'-5 negated**3 => '; showCR: -5 negated**3.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 935: Line 935:


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.
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.
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


class Num2 {
class Num2 {
Line 961: Line 961:
Fmt.print("$s = $4s", ops[3], -(x^p))
Fmt.print("$s = $4s", ops[3], -(x^p))
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}