Exponentiation order: Difference between revisions

From Rosetta Code
Content added Content deleted
(no need to comment the obvious)
(→‎{{header|Perl 6}}: explain odd forms)
Line 25: Line 25:
<lang perl6>sub demo($x) { say " $x\t───► ", EVAL $x }
<lang perl6>sub demo($x) { say " $x\t───► ", EVAL $x }


demo '5**3**2';
demo '5**3**2'; # show ** is right associative
demo '(5**3)**2';
demo '(5**3)**2';
demo '5**(3**2)';
demo '5**(3**2)';

demo '[**] 5,3,2';
demo '[\**] 5,3,2';</lang>
demo '[**] 5,3,2'; # reduction form, show only final result
demo '[\**] 5,3,2'; # triangle reduction, show growing results</lang>
{{out}}
{{out}}
<pre> 5**3**2 ───► 1953125
<pre> 5**3**2 ───► 1953125

Revision as of 18:46, 18 March 2014

Exponentiation order is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents.

(Most languages usually support one of   **,   ^,   or     or somesuch.)

task requirements

Show the result of a language's evaluation of multiple exponentiation (either as an integer or floating point).

Use whatever exponentiation operator for your language; if it is not one of the usual ones, please comment on what the operator or operators look like in your language.

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



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

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