Exponentiation order: Difference between revisions
(+ D entry) |
m (tidy up task description) |
||
Line 1: | Line 1: | ||
{{draft task|exponentiation order}} |
{{draft task|exponentiation order}} |
||
This task will demonstrate the order of exponentiation |
This task will demonstrate the order of [[wp:Exponentiation|exponentiation]] (<math>x^y</math>) when there are multiple exponents. |
||
(Many programming languages, especially those with extended-precision integer arithmetic, usually support one of <code>**</code>, <code>^</code>, <code>↑</code> or some such for this.) |
|||
(Most programming languages usually support one of '''**''', '''^''', or '''↑''' or somesuch.) |
|||
⚫ | |||
⚫ | |||
Show the result of a language's evaluation of multiple exponentiation (either as an integer or floating point). |
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. |
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): |
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) |
|||
:::::* 5**(3**2) |
|||
If there are other methods (or formats) of multiple exponentiations, show them as well. |
If there are other methods (or formats) of multiple exponentiations, show them as well. |
||
;See also |
;See also |
||
* Wiki entry [http://en.wikipedia.org/wiki/Exponentiation exponentiation] |
|||
* Mathworld entry [http://mathworld.wolfram.com/Exponentiation.html exponentiation] |
* Mathworld entry [http://mathworld.wolfram.com/Exponentiation.html exponentiation] |
||
<br><br> |
|||
=={{header|D}}== |
=={{header|D}}== |
Revision as of 10:25, 20 March 2014
This task will demonstrate the order of exponentiation () when there are multiple exponents.
(Many programming languages, especially those with extended-precision integer arithmetic, usually support one of **
, ^
, ↑
or some such for this.)
- 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
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
Perl 6
<lang perl6>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³² ───► 15625
(Not to mention the fact that the form without parentheses looks like you're trying to raise something 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>
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