Exponentiation order: Difference between revisions
(Added EchoLisp) |
(Added Algol 68) |
||
Line 16: | Line 16: | ||
* Mathworld entry [http://mathworld.wolfram.com/Exponentiation.html exponentiation] |
* Mathworld entry [http://mathworld.wolfram.com/Exponentiation.html exponentiation] |
||
* [[Arbitrary-precision integers (included)]] |
* [[Arbitrary-precision integers (included)]] |
||
=={{header|ALGOL 68}}== |
|||
Algol 68 provides various alternative symbols for the exponentiation operator generally, "**", "^" and "UP" can be used. |
|||
<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 ) )</lang> |
|||
{{out}} |
|||
<pre> |
|||
5**3**2: +15625 |
|||
(5**3)**2: +15625 |
|||
5**(3**2): +1953125 |
|||
</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 33: | Line 45: | ||
5^(3^2) = 1953125 |
5^(3^2) = 1953125 |
||
</pre> |
</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
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 2 arguments. |
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 2 arguments. |
Revision as of 19:57, 11 February 2016
This task will demonstrate the order of exponentiation (xy) when there are multiple exponents.
(Many programming languages, especially those with extended-precision integer arithmetic, usually support one of **
, ^
, ↑
or some such for exponentiation.)
- Task requirements
Show the result of a language's evaluation of multiple exponentiation (either as an integer or floating point). If your language's exponentiation operator is not one of the usual ones, please comment on how to recognize it.
Using whatever operator or syntax your language supports (if any), show the results in three lines (with identification):
- 5**3**2
- (5**3)**2
- 5**(3**2)
If there are other methods (or formats) of multiple exponentiations, show them as well.
- See also
- Mathworld entry exponentiation
- Arbitrary-precision integers (included)
ALGOL 68
Algol 68 provides various alternative symbols for the exponentiation operator generally, "**", "^" and "UP" can be used. <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 ) )</lang>
- Output:
5**3**2: +15625 (5**3)**2: +15625 5**(3**2): +1953125
AWK
<lang AWK>
- syntax: GAWK -f EXPONENTIATION_ORDER.AWK
BEGIN {
printf("5^3^2 = %d\n",5^3^2) printf("(5^3)^2 = %d\n",(5^3)^2) printf("5^(3^2) = %d\n",5^(3^2)) exit(0)
} </lang>
output:
5^3^2 = 1953125 (5^3)^2 = 15625 5^(3^2) = 1953125
C
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 2 arguments.
Expressions in C are evaluated by RPN. The RPNs of 5^3^2 and 5^(3^2) are the same and thus also their pow expressions. <lang C> /*Abhishek Ghosh, 20th March 2014, Rotterdam*/
include<stdio.h>
- include<math.h>
int main() { printf("\n5 ^ 3 ^ 2 = %.0f",pow(5,pow(3,2))); /*.0f suppresses decimal output*/ printf("\n(5 ^ 3) ^ 2 = %.0f",pow(pow(5,3),2)); printf("\n5 ^ (3 ^ 2) = %.0f",pow(5,pow(3,2)));
return 0; } </lang>
- Output:
5 ^ 3 ^ 2 = 1953125 (5 ^ 3) ^ 2 = 15625 5 ^ (3 ^ 2) = 1953125
Clojure
Clojure uses prefix notation and expt only takes 2 arguments for exponentiation, so "5**3**2" isn't represented.
<lang clojure>(use 'clojure.math.numeric-tower)
- (5**3)**2
(expt (expt 5 3) 2) ; => 15625
- 5**(3**2)
(expt 5 (expt 3 2)) ; => 1953125
- (5**3)**2 alternative
- use reduce
(reduce expt [5 3 2]) ; => 15625
- 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</lang>
D
<lang d>void main() {
import std.stdio, std.math, std.algorithm;
writefln("5 ^^ 3 ^^ 2 = %7d", 5 ^^ 3 ^^ 2); writefln("(5 ^^ 3) ^^ 2 = %7d", (5 ^^ 3) ^^ 2); writefln("5 ^^ (3 ^^ 2) = %7d", 5 ^^ (3 ^^ 2)); writefln("[5, 3, 2].reduce!pow = %7d", [5, 3, 2].reduce!pow);
}</lang>
- Output:
5 ^^ 3 ^^ 2 = 1953125 (5 ^^ 3) ^^ 2 = 15625 5 ^^ (3 ^^ 2) = 1953125 [5, 3, 2].reduce!pow = 15625
EchoLisp
<lang scheme>
- the standard and secure way is to use the (expt a b) function
(expt 5 (expt 3 2)) ;; 5 ** ( 3 ** 2)
→ 1953125
(expt (expt 5 3) 2) ;; (5 ** 3) ** 2
→ 15625
- infix EchoLisp may use the ** operator, which right associates
(lib 'match) (load 'infix.glisp)
(5 ** 3 ** 2)
→ 1953125
((5 ** 3) ** 2)
→ 15625
(5 ** (3 ** 2))
→ 1953125
</lang>
Fortran
<lang 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)</lang>
- Output:
5**3**2 = 1953125 (5**3)**2 = 15625 5**(3**2) = 1953125
J
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.
<lang J> 5^3^2 1.95312e6
(5^3)^2
15625
5^(3^2)
1.95312e6</lang>
Mathematica / Wolfram Language
<lang 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]]]</lang>
- Output:
5^3^2 = 1953125 (5^3)^2 = 15625 5^(3^2) = 1953125
PARI/GP
Exponentiation is right-associative in GP. <lang parigp>f(s)=print(s" = "eval(s)); apply(f, ["5^3^2", "(5^3)^2", "5^(3^2)"]);</lang>
- Output:
5^3^2 = 1953125 (5^3)^2 = 15625 5^(3^2) = 1953125
Perl
<lang perl>say "$_ = " . eval($_) for qw/5**3**2 (5**3)**2 5**(3**2)/;</lang>
- Output:
5**3**2 = 1953125 (5**3)**2 = 15625 5**(3**2) = 1953125
Perl 6
<lang perl6>use MONKEY-SEE-NO-EVAL; sub demo($x) { say " $x\t───► ", EVAL $x }
demo '5**3**2'; # show ** is right associative demo '(5**3)**2'; demo '5**(3**2)';
demo '[**] 5,3,2'; # reduction form, show only final result demo '[\**] 5,3,2'; # triangle reduction, show growing results</lang>
- Output:
5**3**2 ───► 1953125 (5**3)**2 ───► 15625 5**(3**2) ───► 1953125 [**] 5,3,2 ───► 1953125 [\**] 5,3,2 ───► 2 9 1953125
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.
While it is possible to define your own postfix operators to do exponentiation, Unicode does not have multilevel subscripts, and postfixes are always evaluated from inside out, so you can't stack them and expect right associativity: <lang perl6>sub postfix:<²>($n) { $n * $n } sub postfix:<³>($n) { $n * $n * $n }
demo '(5³)²'; demo '5³²';</lang>
- Output:
(5³)² ───► 15625 5³² ───► 23283064365386962890625
The form without parentheses ends up raising to the 32nd power. Nor are you even allowed to parenthesize it the other way: 5(³²) would be a syntax error. Despite all that, for programs that do a lot of squaring or cubing, the postfix forms can enhance both readability and concision.)
Python
<lang python>>>> 5**3**2 1953125 >>> (5**3)**2 15625 >>> 5**(3**2) 1953125 >>> # The following is not normally done >>> try: from functools import reduce # Py3K except: pass
>>> reduce(pow, (5, 3, 2)) 15625 >>> </lang>
Racket
<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.
- So no can do for 5**3**2
- (5**3)**2
(displayln "prefix") (expt (expt 5 3) 2)
- (5**3)**2
(expt 5 (expt 3 2))
- There is also a less-used infix operation (for all functions, not just expt)... which I suppose
- might do with an airing. But fundamentally nothing changes.
(displayln "\"in\"fix") ((5 . expt . 3) . expt . 2) (5 . expt . (3 . expt . 2))
- everyone's doing a reduction, it seems
(displayln "reduction") (require (only-in srfi/1 reduce reduce-right)) (reduce expt 1 '(5 3 2)) (reduce-right expt 1 '(5 3 2))</lang>
- Output:
prefix 15625 1953125 "in"fix 15625 1953125 reduction 14134776518227074636666380005943348126619871175004951664972849610340958208 1953125
REXX
<lang rexx>/*REXX program demonstrates various ways of multiple exponentiations. */ /*┌────────────────────────────────────────────────────────────────────┐
│ The REXX language uses ** for exponention. │ │ Also, * * can be used. │ └────────────────────────────────────────────────────────────────────┘*/
say ' 5**3**2 ───► ' 5**3**2 say ' (5**3)**2 ───► ' (5**3)**2 say ' 5**(3**2) ───► ' 5**(3**2)
/*stick a fork in it, we're done.*/</lang>
output
5**3**2 ───► 15625 (5**3)**2 ───► 15625 5**(3**2) ───► 1953125
Ruby
<lang ruby>ar = ["5**3**2", "(5**3)**2", "5**(3**2)", "[5,3,2].inject(:**)"] ar.each{|exp| puts "#{exp}:\t#{eval exp}"} </lang>
- Output:
5**3**2: 1953125 (5**3)**2: 15625 5**(3**2): 1953125 [5,3,2].inject(:**): 15625
Tcl
<lang tcl>foreach expression {5**3**2 (5**3)**2 5**(3**2)} {
puts "${expression}:\t[expr $expression]"
}</lang>
- Output:
5**3**2: 1953125 (5**3)**2: 15625 5**(3**2): 1953125
There's also a binary pow()
expression function that always converts its arguments to floating point numbers and then applies the exponentiation operation; it's now largely obsolete because of the **
operator, but is retained for backward compatibility with older programs.
VBScript
<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) </lang>
- Output:
5^3^2 => 15625 (5^3)^2 => 15625 5^(3^2) => 1953125
zkl
zkl does not have an exponentiation operator but floats have a pow method. <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))));</lang>
- Output:
5 ^ 3 ^ 2 = 1,953,125 (5 ^ 3) ^ 2 = 15,625 5 ^ (3 ^ 2) = 1,953,125