Exponentiation order: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Exponentiation order in various BASIC dialents (QBasic, BASIC256, Run BASIC, True BASIC and Yabasic))
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(20 intermediate revisions by 13 users not shown)
Line 34:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">print(5 ^ 3 ^ 2)
print((5 ^ 3) ^ 2)
print(5 ^ (3 ^ 2))</langsyntaxhighlight>
{{out}}
<pre>
Line 47:
There is no power operator in Action! Power function for REAL type is used. But the precision is insufficient.
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
PROC Main()
Line 70:
Print("5^(3^2)=")
PrintRE(tmp2)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Exponentiation_order.png Screenshot from Atari 8-bit computer]
Line 84:
5**3**2 is not a valid Ada expression. Parenthesis are mandatory.
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Exponentation_Order is
Line 92:
Put_Line ("(5**3)**2 : " & Natural'((5**3)**2)'Image);
Put_Line ("5**(3**2) : " & Natural'(5**(3**2))'Image);
end Exponentation_Order;</langsyntaxhighlight>
 
{{out}}
Line 100:
=={{header|ALGOL 68}}==
Algol 68 provides various alternative symbols for the exponentiation operator generally, "**", "^" and "UP" can be used.
<langsyntaxhighlight lang="algol68">print( ( "5**3**2: ", 5**3**2, newline ) );
print( ( "(5**3)**2: ", (5**3)**2, newline ) );
print( ( "5**(3**2): ", 5**(3**2), newline ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 108:
(5**3)**2: +15625
5**(3**2): +1953125
</pre>
 
=={{header|ALGOL-M}}==
The eponentiation operator ** in ALGOL-M works only on integer operands.
<syntaxhighlight lang = "ALGOL">
begin
 
write("5**3**2 = ", 5**3**2);
write("(5**3)**2 = ", (5**3)**2);
write("5**(3**2) = ", 5**(3**2));
 
end
</syntaxhighlight>
{{out}}
The third expression results in a value that exceeds the maximum integer value of 16383. Sadly, ALGOL-M emits no warning or error message when this occurs but simply gives the wrong answer.
<pre>
5**3**2 = 15625
(5**3)**2 = 15625
5**(3**2) = -12955
</pre>
 
=={{header|ALGOL W}}==
The Algol W exponentiation operator always produces a real result and requires an integer right operand, hence the round functions in the following.
<langsyntaxhighlight lang="algolw">begin
write( "5**3**2: ", round( 5 ** 3 ** 2 ) );
write( "(5**3)**2: ", round( ( 5 ** 3 ) ** 2 ) );
write( "5**(3**2): ", round( 5 ** round( 3 ** 2 ) ) )
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 139 ⟶ 158:
AppleScript's compiler inserts its own parentheses with 5 ^ 3 ^ 2.
 
<langsyntaxhighlight lang="applescript">set r1 to 5 ^ 3 ^ 2 -- Changes to 5 ^ (3 ^ 2) when compiled.
set r2 to (5 ^ 3) ^ 2
set r3 to 5 ^ (3 ^ 2)
Line 145 ⟶ 164:
return "5 ^ 3 ^ 2 = " & r1 & "
(5 ^ 3) ^ 2 = " & r2 & "
5 ^ (3 ^ 2) = " & r3</langsyntaxhighlight>
 
{{output}}
Line 154 ⟶ 173:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">print 5^3^2
print (5^3)^2
print 5^(3^2)</langsyntaxhighlight>
 
{{out}}
Line 165 ⟶ 184:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f EXPONENTIATION_ORDER.AWK
BEGIN {
Line 173 ⟶ 192:
exit(0)
}
</syntaxhighlight>
</lang>
<p>output:</p>
<pre>
Line 182 ⟶ 201:
 
=={{header|BASIC}}==
==={{header|Sinclair ZX81Applesoft BASIC}}===
<syntaxhighlight lang="gwbasic">?"5^3^2 = "5 ^ 3 ^ 2 CHR$ (13)"(5^3)^2 = "(5 ^ 3) ^ 2 CHR$ (13)"5^(3^2) = "5 ^ (3 ^ 2);</syntaxhighlight>
<lang basic>10 PRINT "5**3**2 = ";5**3**2
20 PRINT "(5**3)**2 = ";(5**3)**2
30 PRINT "5**(3**2) = ";5**(3**2)</lang>
{{out}}
<pre>5**^3**^2 = 15625
(5**^3)**^2 = 15625
5**^(3**^2) = 1953125.01</pre>
==={{header|BASIC256}}===
{{works with|QBasic}}
{{works with|FreeBASIC}}
{{works with|True BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="freebasic">print "5^3^2 = "; 5^3^2
print "(5^3)^2 = "; (5^3)^2
print "5^(3^2) = "; 5^(3^2)
end</syntaxhighlight>
{{out}}
<pre>5^3^2 = 15625
(5^3)^2 = 15625
5^(3^2) = 1953125</pre>
 
==={{header|BBC BASIC}}===
<langsyntaxhighlight lang="bbcbasic">PRINT "5^3^2 = "; 5^3^2
PRINT "(5^3)^2 = "; (5^3)^2
PRINT "5^(3^2) = "; 5^(3^2)</langsyntaxhighlight>
{{out}}
<pre>5^3^2 = 15625
(5^3)^2 = 15625
5^(3^2) = 1953125</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 print "5^3^2 = "5^3^2
20 print "(5^3)^2 = "(5^3)^2
30 print "5^(3^2) = "5^(3^2)</syntaxhighlight>
{{out}}
<pre>5^3^2 = 15625
(5^3)^2 = 15625
5^(3^2) = 1953125</pre>
 
==={{header|GW-BASIC}}===
{{works with|Applesoft BASIC}}
{{works with|Chipmunk Basic}}
{{works with|MSX_BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 PRINT "5^3^2 =" 5^3^2
20 PRINT "(5^3)^2 =" (5^3)^2
30 PRINT "5^(3^2) =" 5^(3^2)</syntaxhighlight>
{{out}}
<pre>5^3^2 = 15625
Line 201 ⟶ 255:
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 PRINT "5^3^2 =";5^3^2
110 PRINT "(5^3)^2 =";(5^3)^2
120 PRINT "5^(3^2) =";5^(3^2)</langsyntaxhighlight>
{{out}}
<pre>5^3^2 = 15625
Line 209 ⟶ 263:
5^(3^2) = 1953125</pre>
 
==={{header|MSX Basic}}===
<syntaxhighlight lang="qbasic">10 PRINT "5^3^2 =" 5^3^2
20 PRINT "(5^3)^2 =" (5^3)^2
30 PRINT "5^(3^2) =" 5^(3^2)</syntaxhighlight>
{{out}}
<pre>5^3^2 = 15625
(5^3)^2 = 15625
5^(3^2) = 1953125</pre>
 
==={{header|PureBasic}}===
In the PureBasic it is impossible to show the result of: 5^3^2
<syntaxhighlight lang="vb">OpenConsole()
PrintN("(5^3)^2 = " + Str(Pow(Pow(5, 3), 2)))
PrintN("5^(3^2) = " + Str(Pow(5, (Pow(3, 2)))))
CloseConsole()</syntaxhighlight>
{{out}}
<pre>(5^3)^2 = 15625
5^(3^2) = 1953125</pre>
 
==={{header|QBasic}}===
Line 217 ⟶ 289:
{{works with|BASIC256}}
{{works with|Run BASIC}}
<langsyntaxhighlight lang="qbasic">PRINT "5^3^2 ="; 5^3^2
PRINT "(5^3)^2 ="; (5^3)^2
PRINT "5^(3^2) ="; 5^(3^2)
END</langsyntaxhighlight>
{{out}}
<pre>5^3^2 = 15625
(5^3)^2 = 15625
5^(3^2) = 1953125</pre>
 
==={{header|BASIC256}}===
{{works with|QBasic}}
{{works with|FreeBASIC}}
{{works with|True BASIC}}
{{works with|Run BASIC}}
<lang freebasic>print "5^3^2 = "; 5^3^2
print "(5^3)^2 = "; (5^3)^2
print "5^(3^2) = "; 5^(3^2)
end</lang>
{{out}}
<pre>5^3^2 = 15625
Line 245 ⟶ 303:
{{works with|True BASIC}}
{{works with|BASIC256}}
<langsyntaxhighlight lang="freebasic">print "5^3^2 = "; 5^3^2
print "(5^3)^2 = "; (5^3)^2
print "5^(3^2) = "; 5^(3^2)
end</langsyntaxhighlight>
{{out}}
<pre>5^3^2 = 15625
Line 259 ⟶ 317:
{{works with|BASIC256}}
{{works with|Run BASIC}}
<langsyntaxhighlight lang="qbasic">PRINT "5^3^2 ="; 5^3^2
PRINT "(5^3)^2 ="; (5^3)^2
PRINT "5^(3^2) ="; 5^(3^2)
END</langsyntaxhighlight>
{{out}}
<pre>5^3^2 = 15625
(5^3)^2 = 15625
5^(3^2) = 1953125</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Exponentiation order"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
PRINT "5^3^2 ="; 5**3**2
PRINT "(5^3)^2 ="; (5**3)**2
PRINT "5^(3^2) ="; 5**(3**2)
END FUNCTION
END PROGRAM</syntaxhighlight>
{{out}}
<pre>5^3^2 = 15625
Line 273 ⟶ 349:
{{works with|True BASIC}}
{{works with|BASIC256}}
<langsyntaxhighlight lang="freebasic">print "5^3^2 = ", 5^3^2
print "(5^3)^2 = ", (5^3)^2
print "5^(3^2) = ", 5^(3^2)
end</langsyntaxhighlight>
{{out}}
<pre>5^3^2 = 15625
Line 282 ⟶ 358:
5^(3^2) = 1953125</pre>
 
==={{header|Sinclair ZX81 BASIC}}===
<syntaxhighlight lang="basic">10 PRINT "5**3**2 = ";5**3**2
20 PRINT "(5**3)**2 = ";(5**3)**2
30 PRINT "5**(3**2) = ";5**(3**2)</syntaxhighlight>
{{out}}
<pre>5**3**2 = 15625
(5**3)**2 = 15625
5**(3**2) = 1953125</pre>
 
=={{header|Bracmat}}==
<langsyntaxhighlight Bracmatlang="bracmat">put$str$("5^3^2: " 5^3^2 "\n(5^3)^2: " (5^3)^2 "\n5^(3^2): " 5^(3^2) \n) </langsyntaxhighlight>
{{out}}
<pre>5^3^2: 1953125
Line 293 ⟶ 377:
C does not have an exponentiation operator. The caret operator '^' performs xor bitwise operation in C. The function pow in the standard C Math library takes two arguments.
 
<langsyntaxhighlight Clang="c">#include<stdio.h>
#include<math.h>
 
Line 302 ⟶ 386:
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 311 ⟶ 395:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
 
Line 319 ⟶ 403:
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
With permissive flag:
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
 
Line 334 ⟶ 418:
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 343 ⟶ 427:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace exponents
Line 363 ⟶ 447:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 373 ⟶ 457:
Clojure uses prefix notation and expt only takes 2 arguments for exponentiation, so "5**3**2" isn't represented.
 
<langsyntaxhighlight lang="clojure">(use 'clojure.math.numeric-tower)
;; (5**3)**2
(expt (expt 5 3) 2) ; => 15625
Line 385 ⟶ 469:
;; 5**(3**2) alternative: evaluating right-to-left with reduce requires a small modification
(defn rreduce [f coll] (reduce #(f %2 %) (reverse coll)))
(rreduce expt [5 3 2]) ; => 1953125</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">start_up = proc ()
po: stream := stream$primary_output()
Line 394 ⟶ 478:
stream$putl(po, "(5**3)**2 = " || int$unparse((5**3)**2))
stream$putl(po, "5**(3**2) = " || int$unparse(5**(3**2)))
end start_up</langsyntaxhighlight>
{{out}}
<pre>5**3**2 = 1953125
Line 402 ⟶ 486:
=={{header|Common Lisp}}==
Because Common Lisp uses prefix notation and <code>expt</code> accepts only two arguments, it doesn't have an expression for <code>5**3**2</code>. Just showing expressions for the latter two.
<langsyntaxhighlight lang="lisp">(expt (expt 5 3) 2)
(expt 5 (expt 3 2))</langsyntaxhighlight>
{{out}}
<pre>15625
Line 409 ⟶ 493:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.math, std.algorithm;
 
Line 416 ⟶ 500:
writefln("5 ^^ (3 ^^ 2) = %7d", 5 ^^ (3 ^^ 2));
writefln("[5, 3, 2].reduce!pow = %7d", [5, 3, 2].reduce!pow);
}</langsyntaxhighlight>
{{out}}
<pre>5 ^^ 3 ^^ 2 = 1953125
Line 422 ⟶ 506:
5 ^^ (3 ^^ 2) = 1953125
[5, 3, 2].reduce!pow = 15625</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Math,SysUtils,StdCtrls}}
Delphi doesn't have exponentiation but it does have the "Power" function in the math library
 
<syntaxhighlight lang="Delphi">
procedure ExponentDemo(Memo: TMemo);
begin
Memo.Lines.Add('5^3^2 = '+FloatToStrF(Power(5,Power(3,2)),ffNumber,18,0));
Memo.Lines.Add('(5^3)^2 = '+FloatToStrF(Power(Power(5,3),2),ffNumber,18,0));
Memo.Lines.Add('5^(3^2) = '+FloatToStrF(Power(5,Power(3,2)),ffNumber,18,0));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
5^3^2 = 1,953,125
(5^3)^2 = 15,625
5^(3^2) = 1,953,125
 
</pre>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">import 'dart:math' show pow;
 
void main() {
print('(5 ^ 3) ^ 2 = ${pow(pow(5, 3), 2)}');
print('5 ^ (3 ^ 2) = ${pow(5, (pow(3, 2)))}');
}</syntaxhighlight>
{{out}}
<pre>(5 ^ 3) ^ 2 = 15625
5 ^ (3 ^ 2) = 1953125</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
print "(5 ^ 3) ^ 2 = " & pow (pow 5 3) 2
print "5 ^ (3 ^ 2) = " & pow 5 pow 3 2
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; the standard and secure way is to use the (expt a b) function
(expt 5 (expt 3 2)) ;; 5 ** ( 3 ** 2)
Line 441 ⟶ 565:
(5 ** (3 ** 2))
→ 1953125
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
Factor is a stack language where expressions take the form of reverse Polish notation, so there is no ambiguity here. It is up to you, the programmer, to perform operations in the order you intend.
<langsyntaxhighlight lang="factor">USING: formatting math.functions ;
 
5 3 2 ^ ^
Line 451 ⟶ 575:
 
5 3 ^ 2 ^
"5 3 ^ 2 ^ %d\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 460 ⟶ 584:
Factor also has syntax for infix arithmetic via the the <code>infix</code> vocabulary.
 
<langsyntaxhighlight lang="factor">USING: formatting infix ;
 
[infix 5**3**2 infix]
Line 469 ⟶ 593:
 
[infix 5**(3**2) infix]
"5**(3**2) = %d\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 478 ⟶ 602:
 
=={{header|Fortran}}==
<langsyntaxhighlight Fortranlang="fortran">write(*, "(a, i0)") "5**3**2 = ", 5**3**2
write(*, "(a, i0)") "(5**3)**2 = ", (5**3)**2
write(*, "(a, i0)") "5**(3**2) = ", 5**(3**2)</langsyntaxhighlight>
{{out}}
<pre>5**3**2 = 1953125
Line 487 ⟶ 611:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0
 
' The exponentation operator in FB is ^ rather than **.
Line 497 ⟶ 621:
Print "(5^3)^2 =>"; (5^3)^2
Print "5^(3^2) =>"; 5^(3^2)
Sleep</langsyntaxhighlight>
 
{{out}}
Line 509 ⟶ 633:
Frink correctly follows standard mathematical notation that exponent towers are performed from "top to bottom" or "right to left."
 
<langsyntaxhighlight Frinklang="frink">println["5^3^2 = " + 5^3^2]
println["(5^3)^2 = " + (5^3)^2]
println["5^(3^2) = " + 5^(3^2)]
</syntaxhighlight>
</lang>
 
{{out}}
Line 519 ⟶ 643:
(5^3)^2 = 15625
5^(3^2) = 1953125
</pre>
 
=={{header|FutureBasic}}==
FB is translated into C which does not have an exponentiation operator. The caret operator '^' performs xor bitwise operation. FB also has an fn pow function, translated from the the standard C Math library, which takes two arguments.
<syntaxhighlight lang="futurebasic">
print "(5^3)^2 = "; (5^3)^2
print "5^(3^2) = "; 5^(3^2)
print
print "fn pow( fn pow(5,3), 2 ) = "; fn pow( fn pow(5,3), 2 )
print "fn pow( 5, fn pow(3,2 ) ) = "; fn pow( 5, fn pow(3,2 ) )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
(5^3)^2 = 15625
5^(3^2) = 1953125
 
fn pow( fn pow(5,3), 2 ) = 15625
fn pow( 5, fn pow(3,2 ) ) = 1953125
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight Golang="go">package main
 
import "fmt"
Line 535 ⟶ 679:
fmt.Printf("(5^3)^2 = %.0f\n", b)
fmt.Printf("5^(3^2) = %.0f\n", c)
}</langsyntaxhighlight>
 
{{out}}
Line 547 ⟶ 691:
 
Solution:
<langsyntaxhighlight lang="groovy">println(" 5 ** 3 ** 2 == " + 5**3**2)
println("(5 ** 3)** 2 == " + (5**3)**2)
println(" 5 **(3 ** 2)== " + 5**(3**2))</langsyntaxhighlight>
 
Output:
Line 610 ⟶ 754:
J uses the same evaluation order for exponentiation as it does for assignment. That is to say: the bottom up view is right-to-left and the top-down view is left-to-right.
 
<langsyntaxhighlight Jlang="j"> 5^3^2
1.95312e6
(5^3)^2
15625
5^(3^2)
1.95312e6</langsyntaxhighlight>
 
----
Line 627 ⟶ 771:
jq's built-in for exponentiation is an arity-two function and thus no ambiguity arising from infix-notation is possible. Here's an example:
 
<langsyntaxhighlight lang="jq">jq -n 'pow(pow(5;3);2)'
15625</langsyntaxhighlight>
 
For chaining, one could use `reduce`:
 
<langsyntaxhighlight lang="jq"> def pow: reduce .[1:] as $i (.[0]; pow(.;$i))
 
[5,3,2] | pow</langsyntaxhighlight>
 
Result: 15625
Line 641 ⟶ 785:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">@show 5 ^ 3 ^ 2 # default: power operator is read right-to-left
@show (5 ^ 3) ^ 2
@show 5 ^ (3 ^ 2)
@show reduce(^, [5, 3, 2])
@show foldl(^, [5, 3, 2]) # guarantees left associativity
@show foldr(^, [5, 3, 2]) # guarantees right associativity</langsyntaxhighlight>
 
{{out}}
Line 658 ⟶ 802:
=={{header|Kotlin}}==
Kotlin does not have a dedicated exponentiation operator and we would normally use Java's Math.pow function instead. However, it's possible to define an infix function which would look like an operator and here we do so for integer base and exponent. For simplicity we disallow negative exponents altogether and consider 0 ** 0 == 1. Associativity would, of course, be the same as for a normal function call.
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
infix fun Int.ipow(exp: Int): Int = when {
Line 680 ⟶ 824:
println("(5**3)**2 = ${(5 ipow 3) ipow 2}")
println("5**(3**2) = ${5 ipow (3 ipow 2)}")
}</langsyntaxhighlight>
 
{{out}}
Line 693 ⟶ 837:
Because lambdatalk uses prefix notation and {pow a b} accepts only two arguments, it doesn't have an expression for 5**3**2. Just showing expressions for the latter two.
 
<langsyntaxhighlight lang="scheme">
'{pow {pow 5 3} 2}
-> {pow {pow 5 3} 2}
'{pow 5 {pow 3 2}}
-> {pow 5 {pow 3 2}}
</syntaxhighlight>
</lang>
 
=={{header|langur}}==
<syntaxhighlight lang="langur">writeln " 5^3^2: ", 5^3^2
writeln "(5^3)^2: ", (5^3)^2
writeln "5^(3^2): ", 5^(3^2)
</syntaxhighlight>
 
{{out}}
<pre> 5^3^2: 1953125
(5^3)^2: 15625
5^(3^2): 1953125
</pre>
 
=={{header|Latitude}}==
<langsyntaxhighlight lang="latitude">5 ^ 3 ^ 2. ;; 1953125
(5 ^ 3) ^ 2. ;; 15625
5 ^ (3 ^ 2). ;; 1953125</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">print("5^3^2 = " .. 5^3^2)
print("(5^3)^2 = " .. (5^3)^2)
print("5^(3^2) = " .. 5^(3^2))</langsyntaxhighlight>
{{out}}
<pre>5^3^2 = 1953125
Line 716 ⟶ 872:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">5^3^2;
(5^3)^2;
5^(3^2);</langsyntaxhighlight>
{{Out|Output}}
<pre>Error, ambiguous use of `^`, please use parentheses
Line 725 ⟶ 881:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">a = "5^3^2";
Print[a <> " = " <> ToString[ToExpression[a]]]
b = "(5^3)^2";
Print[b <> " = " <> ToString[ToExpression[b]]]
c = "5^(3^2)";
Print[c <> " = " <> ToString[ToExpression[c]]]</langsyntaxhighlight>
{{out}}
<pre>5^3^2 = 1953125
Line 739 ⟶ 895:
As with other postfix languages, there is no ambiguity because all operators have the same precedence.
{{works with|min|0.19.6}}
<langsyntaxhighlight lang="min">5 3 2 pow pow
"5 3 2 ^ ^ " print! puts!
 
5 3 pow 2 pow
"5 3 ^ 2 ^ " print! puts!</langsyntaxhighlight>
{{out}}
<pre>
5 3 2 ^ ^ 1953125.0
5 3 ^ 2 ^ 15625.0
</pre>
 
=={{header|MiniScript}}==
REPL output.
{{out}}
<pre>
]5^3^2
15625
](5^3)^2
15625
]5^(3^2)
1953125
</pre>
 
=={{header|Nanoquery}}==
Nanoquery uses the '^' operator, which performs exponentiation in order like multiplication. Parenthesis are often needed to perform operations like 5^3^2 correctly.
<langsyntaxhighlight Nanoquerylang="nanoquery">% println 5^3^2
15625
% println (5^3)^2
15625
% println 5^(3^2)
1953125</langsyntaxhighlight>
 
=={{header|Nim}}==
<Langsyntaxhighlight Nimlang="nim">import math, sequtils
 
echo "5^3^2 = ", 5^3^2
Line 766 ⟶ 934:
echo "5^(3^2) = ", 5^(3^2)
echo "foldl([5, 3, 2], a^b) = ", foldl([5, 3, 2], a^b)
echo "foldr([5, 3, 2], a^b) = ", foldr([5, 3, 2], a^b)</langsyntaxhighlight>
 
{{out}}
Line 777 ⟶ 945:
=={{header|OCaml}}==
OCaml language has '**' as an exponentiation symbol for floating point integers
<syntaxhighlight lang="ocaml">
<OCaml code>
# 5. ** 3. ** 2. ;;
# 5. **( 3. ** 2.) ;;
#(5. ** 3. ) **2. ;;
</syntaxhighlight>
 
{{out}}
<pre>
Line 792 ⟶ 960:
=={{header|PARI/GP}}==
Exponentiation is right-associative in GP.
<langsyntaxhighlight lang="parigp">f(s)=print(s" = "eval(s));
apply(f, ["5^3^2", "(5^3)^2", "5^(3^2)"]);</langsyntaxhighlight>
{{out}}
<pre>5^3^2 = 1953125
Line 800 ⟶ 968:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">say "$_ = " . eval($_) for qw/5**3**2 (5**3)**2 5**(3**2)/;</langsyntaxhighlight>
{{out}}
<pre>
Line 811 ⟶ 979:
{{libheader|Phix/basics}}
Phix has a power function rather than an infix power operator, hence there is no possible confusion.
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #0000FF;">?<span style="color: #7060A8;">power<span style="color: #0000FF;">(<span style="color: #7060A8;">power<span style="color: #0000FF;">(<span style="color: #000000;">5<span style="color: #0000FF;">,<span style="color: #000000;">3<span style="color: #0000FF;">)<span style="color: #0000FF;">,<span style="color: #000000;">2<span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?<span style="color: #7060A8;">power<span style="color: #0000FF;">(<span style="color: #000000;">5<span style="color: #0000FF;">,<span style="color: #7060A8;">power<span style="color: #0000FF;">(<span style="color: #000000;">3<span style="color: #0000FF;">,<span style="color: #000000;">2<span style="color: #0000FF;">)<span style="color: #0000FF;">)
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
15625
1953125
</pre>
 
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">main =>
X = 5**3**2, Y = (5**3)**2, Z = 5**(3**2),
print("5**3**2 = "), println(X),
print("(5**3)**2 = "), println(Y),
print("5**(3**2) = "), print(Z).
</syntaxhighlight>
 
{{out}}
<pre>
5**3**2 = 1953125
(5**3)**2 = 15625
5**(3**2) = 1953125
</pre>
 
=={{header|PicoLisp}}==
The PicoLisp '**' exponentiation function takes 2 arguments
<langsyntaxhighlight PicoLisplang="picolisp">: (** (** 5 3) 2)
-> 15625
 
: (** 5 (** 3 2))
-> 1953125</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">exponentiation: procedure options(main);
put skip edit('5**3**2 = ', 5**3**2) (A,F(7));
put skip edit('(5**3)**2 = ', (5**3)**2) (A,F(7));
put skip edit('5**(3**2) = ', 5**(3**2)) (A,F(7));
end exponentiation;</langsyntaxhighlight>
{{out}}
<pre>5**3**2 = 15625
Line 841 ⟶ 1,025:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> 5**3**2
1953125
>>> (5**3)**2
Line 853 ⟶ 1,037:
>>> reduce(pow, (5, 3, 2))
15625
>>> </langsyntaxhighlight>
 
=={{header|Quackery}}==
Line 876 ⟶ 1,060:
 
It turns out that the parser is so blind to "**" that we cannot even quote it. The following are identical:
<langsyntaxhighlight lang="rsplus">print(quote(5**3))
print(quote(5^3))</langsyntaxhighlight>
 
Another method is to use "^" as if it is an ordinary function of two arguments. It appears that "**" does not support this. As there is no potential for ambiguity in the operator precedence, we will not print this result below. For example:
<langsyntaxhighlight lang="rsplus">'^'('^'(5, 3), 2)</langsyntaxhighlight>
is clearly (5^3)^2 i.e. 15625, whereas
<langsyntaxhighlight lang="rsplus">'^'(5, '^'(3, 2))</langsyntaxhighlight>
is clearly 5^(3^2) i.e. 1953125.
 
As for actually solving the task, the requirement that each output be on a new line causes us a surprising amount of difficulty. To avoid repeating ourselves, we must almost resort to metaprogramming:
<langsyntaxhighlight lang="rsplus">inputs <- alist(5^3^2, (5^3)^2, 5^(3^2), 5**3**2, (5**3)**2, 5**(3**2))
invisible(sapply(inputs, function(x) cat(deparse(x), "returns: ", eval(x), "\n")))</langsyntaxhighlight>
 
Alternatively, we could print out a matrix or data frame:
<langsyntaxhighlight lang="rsplus">print(matrix(sapply(inputs, eval), dimnames = list(inputs, "Outputs")))
print(data.frame(Inputs = sapply(inputs, deparse), Outputs = sapply(inputs, eval))))</langsyntaxhighlight>
{{out}}
<pre>> print(quote(5**3))
Line 922 ⟶ 1,106:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
;; 5**3**2 depends on associativity of ** : Racket's (scheme's) prefix function
;; calling syntax only allows for pairs of arguments for expt.
Line 943 ⟶ 1,127:
(require (only-in srfi/1 reduce reduce-right))
(reduce expt 1 '(5 3 2))
(reduce-right expt 1 '(5 3 2))</langsyntaxhighlight>
{{out}}
<pre>prefix
Line 961 ⟶ 1,145:
Note that the reduction forms automatically go right-to-left because the base operator is right-associative. Most other operators are left-associative and would automatically reduce left-to-right instead.
 
<syntaxhighlight lang="raku" perl6line>use MONKEY-SEE-NO-EVAL;
sub demo($x) { say " $x\t───► ", EVAL $x }
 
Line 975 ⟶ 1,159:
demo '(5³)²';
demo '5³²';
</syntaxhighlight>
</lang>
 
{{out}}
Line 992 ⟶ 1,176:
=={{header|Red}}==
In Red, operators simply evaluate left to right. As this differs from mathematical order of operations, Red provides the <code>math</code> function which evaluates a block using math rules instead of Red's default evaluation. One could also use the <code>power</code> function, sidestepping the issue of evaluation order entirely. All three approaches are shown.
<langsyntaxhighlight lang="rebol">Red["Exponentiation order"]
 
exprs: [
Line 1,007 ⟶ 1,191:
print [mold/only expr "=" math expr "using math"]
]
]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,021 ⟶ 1,205:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates various ways of multiple exponentiations. */
/*┌────────────────────────────────────────────────────────────────────┐
│ The REXX language uses ** for exponentiation. │
Line 1,031 ⟶ 1,215:
say ' (5**3)**2 ───► ' (5**3)**2
say ' 5**(3**2) ───► ' 5**(3**2)
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
'''output'''
<pre>
Line 1,043 ⟶ 1,227:
In the Ring it is impossible to show the result of: 5^3^2
 
<langsyntaxhighlight lang="ring">
see "(5^3)^2 =>" + pow(pow(5,3),2) + nl
see "5^(3^2) =>" + pow(5,pow(3,2)) + nl
</syntaxhighlight>
</lang>
Output:
<pre>
(5^3)^2 =>15625
5^(3^2) =>1953125
</pre>
 
=={{header|RPL}}==
When using reverse Polish notation, there is no parenthesis: the user must decide the exponentiation order.
When using algebraic notation:
'5^3^2' →NUM
'(5^3)^2' →NUM
'5^(3^2)' →NUM
{{out}}
<pre>
3: 15625
2: 15625
1: 1953125
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">ar = ["5**3**2", "(5**3)**2", "5**(3**2)", "[5,3,2].inject(:**)"]
ar.each{|exp| puts "#{exp}:\t#{eval exp}"}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,067 ⟶ 1,264:
=={{header|Rust}}==
 
<langsyntaxhighlight lang="rust">fn main() {
println!("5**3**2 = {:7}", 5u32.pow(3).pow(2));
println!("(5**3)**2 = {:7}", (5u32.pow(3)).pow(2));
println!("5**(3**2) = {:7}", 5u32.pow(3u32.pow(2)));
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,077 ⟶ 1,274:
(5**3)**2 = 15625
5**(3**2) = 1953125
</pre>
 
=={{header|S-BASIC}}==
The exponentiation operator ^ works on both integer and real operands. Numeric constants in expressions are taken to be of type real, which is useful here, because the third result exceeds S-BASIC's manximum integer value of 32767.
<syntaxhighlight lang = "BASIC">
print "5^3^2 : "; 5 ^ 3 ^ 2
print "(5^3)^2 : "; (5 ^ 3) ^ 2
print "5^(3^2) : "; 5 ^ (3 ^ 2)
 
end
</syntaxhighlight>
{{out}}
<pre>
5^3^2 : 15625
(5^3)^2 : 15625
5^(3^2) : 1.95312E+6
</pre>
 
Line 1,083 ⟶ 1,296:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 1,090 ⟶ 1,303:
writeln("(5**3)**2 = " <& (5**3)**2);
writeln("5**(3**2) = " <& 5**(3**2));
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,101 ⟶ 1,314:
=={{header|Sidef}}==
In Sidef, the whitespace between the operands and the operator controls the precedence of the operation.
<langsyntaxhighlight lang="ruby">var a = [
'5**3**2',
'(5**3)**2',
Line 1,113 ⟶ 1,326:
a.each {|e|
"%-12s == %s\n".printf(e, eval(e))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,126 ⟶ 1,339:
 
=={{header|Simula}}==
<langsyntaxhighlight Simulalang="simula">OutText("5** 3 **2: "); OutInt(5** 3 **2, 0); Outimage;
OutText("(5**3)**2: "); OutInt((5**3)**2, 0); Outimage;
OutText("5**(3**2): "); OutInt(5**(3**2), 0); Outimage</langsyntaxhighlight>
{{out}}
<pre>5** 3 **2: 15625
Line 1,137 ⟶ 1,350:
Works in Smalltalk/X &sup1;
<p>Smalltalk strictly evaluates left to right; operators are not known to the language/parser, but instead message sends to the receiver on the left side (aka: virtual function calls) .
<langsyntaxhighlight lang="smalltalk">Transcript show:'5**3**2 => '; showCR: 5**3**2.
Transcript show:'(5**3)**2 => '; showCR: (5**3)**2.
Transcript show:'5**(3**2) => '; showCR: 5**(3**2).</langsyntaxhighlight>
{{out}}
<pre>
Line 1,150 ⟶ 1,363:
=={{header|Stata}}==
 
<langsyntaxhighlight lang="stata">. di (5^3^2)
15625
 
Line 1,157 ⟶ 1,370:
 
. di (5^(3^2))
1953125</langsyntaxhighlight>
 
Likewise in Mata:
 
<langsyntaxhighlight lang="stata">. mata (5^3^2)
15625
 
Line 1,168 ⟶ 1,381:
 
. mata (5^(3^2))
1953125</langsyntaxhighlight>
 
 
Line 1,175 ⟶ 1,388:
Swift doesn't have an exponentiation operator, however it's possible to define one, including the precedence and associativity.
 
<langsyntaxhighlight lang="swift">precedencegroup ExponentiationPrecedence {
associativity: left
higherThan: MultiplicationPrecedence
Line 1,211 ⟶ 1,424:
print(5 ** 3 ** 2)
print((5 ** 3) ** 2)
print(5 ** (3 ** 2))</langsyntaxhighlight>
 
{{out}}
Line 1,220 ⟶ 1,433:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">foreach expression {5**3**2 (5**3)**2 5**(3**2)} {
puts "${expression}:\t[expr $expression]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,232 ⟶ 1,445:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Public Sub exp()
Debug.Print "5^3^2", 5 ^ 3 ^ 2
Debug.Print "(5^3)^2", (5 ^ 3) ^ 2
Debug.Print "5^(3^2)", 5 ^ (3 ^ 2)
End Sub</langsyntaxhighlight>{{out}}
<pre>5^3^2 15625
(5^3)^2 15625
Line 1,243 ⟶ 1,456:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
WScript.StdOut.WriteLine "5^3^2 => " & 5^3^2
WScript.StdOut.WriteLine "(5^3)^2 => " & (5^3)^2
WScript.StdOut.WriteLine "5^(3^2) => " & 5^(3^2)
</syntaxhighlight>
</lang>
 
{{Out}}
Line 1,257 ⟶ 1,470:
 
=={{header|Verbexx}}==
<langsyntaxhighlight lang="verbexx">// Exponentiation order example:
 
@SAY "5**3**2 = " ( 5**3**2 );
Line 1,267 ⟶ 1,480:
5**3**2 = 1953125
(5**3)**2 = 15625
5**(3**2) = 1953125</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
Wren doesn't have an exponentiation operator as such but the Num class has a ''pow'' method which does the same thing.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var ops = [ "5**3**2", "(5**3)**2", "5**(3**2)" ]
var results = [ 5.pow(3).pow(2), (5.pow(3)).pow(2), 5.pow(3.pow(2)) ]
for (i in 0...ops.count) {
SystemFmt.print("%(Fmt.s($-99s -> $d", ops[i])), -> %(results[i])")
}</langsyntaxhighlight>
 
{{out}}
Line 1,285 ⟶ 1,498:
(5**3)**2 -> 15625
5**(3**2) -> 1953125
</pre>
 
=={{header|XPL0}}==
XPL0 doesn't have an exponentiation operator, but it does have a Pow intrinsic (in the 32-bit versions).
<syntaxhighlight lang "XPL0">[Format(1, 0);
Text(0, "5**3**2 = "); RlOut(0, Pow(5., Pow(3., 2.))); CrLf(0); \right associative
Text(0, "(5**3)**2 = "); RlOut(0, Pow(Pow(5., 3.), 2.)); CrLf(0);
Text(0, "5**(3**2) = "); RlOut(0, Pow(5., Pow(3., 2.))); CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
5**3**2 = 1953125
(5**3)**2 = 15625
5**(3**2) = 1953125
</pre>
 
Line 1,290 ⟶ 1,517:
{{trans|C}}
zkl does not have an exponentiation operator but floats have a pow method.
<langsyntaxhighlight lang="zkl">println("5 ^ 3 ^ 2 = %,d".fmt((5.0).pow((3.0).pow(2))));
println("(5 ^ 3) ^ 2 = %,d".fmt((5.0).pow(3).pow(2)));
println("5 ^ (3 ^ 2) = %,d".fmt((5.0).pow((3.0).pow(2))));</langsyntaxhighlight>
{{out}}
<pre>
9,479

edits