Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Exponentiation_order

View file

@ -0,0 +1,32 @@
This task will demonstrate the order of [[wp:Exponentiation|exponentiation]] &nbsp; <big>('''x<sup>y</sup>''') </big> &nbsp; when there are multiple exponents.
(Many programming languages, &nbsp; especially those with extended─precision integer arithmetic, &nbsp; usually support one of <big> <code>**</code>, <code>^</code>, <code>↑</code> </big> 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):
<big>
::::* &nbsp; 5**3**2
::::* &nbsp; (5**3)**2
::::* &nbsp; 5**(3**2)
</big>
<br>
If there are other methods (or formats) of multiple exponentiations, show them as well.
;See also:
* MathWorld entry: &nbsp; [http://mathworld.wolfram.com/Exponentiation.html exponentiation]
;Related tasks:
* &nbsp; [[Exponentiation_operator|exponentiation operator]]
* &nbsp; [[Arbitrary-precision_integers_(included)|arbitrary-precision integers (included)]]
* &nbsp; [[Exponentiation with infix operators in (or operating on) the base]]
<br><br>

View file

@ -0,0 +1,3 @@
print(5 ^ 3 ^ 2)
print((5 ^ 3) ^ 2)
print(5 ^ (3 ^ 2))

View file

@ -0,0 +1,3 @@
print( ( "5**3**2: ", 5**3**2, newline ) );
print( ( "(5**3)**2: ", (5**3)**2, newline ) );
print( ( "5**(3**2): ", 5**(3**2), newline ) )

View file

@ -0,0 +1,5 @@
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.

View file

@ -0,0 +1,7 @@
# 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)
}

View file

@ -0,0 +1,24 @@
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
PROC Main()
REAL r2,r3,r5,tmp1,tmp2
Put(125) PutE() ;clear screen
IntToReal(2,r2)
IntToReal(3,r3)
IntToReal(5,r5)
PrintE("There is no power operator in Action!")
PrintE("Power function for REAL type is used.")
PrintE("But the precision is insufficient.")
Power(r5,r3,tmp1)
Power(tmp1,r2,tmp2)
Print("(5^3)^2=")
PrintRE(tmp2)
Power(r3,r2,tmp1)
Power(r5,tmp1,tmp2)
Print("5^(3^2)=")
PrintRE(tmp2)
RETURN

View file

@ -0,0 +1,9 @@
with Ada.Text_IO;
procedure Exponentation_Order is
use Ada.Text_IO;
begin
-- Put_Line ("5**3**2 : " & Natural'(5**3**2)'Image);
Put_Line ("(5**3)**2 : " & Natural'((5**3)**2)'Image);
Put_Line ("5**(3**2) : " & Natural'(5**(3**2))'Image);
end Exponentation_Order;

View file

@ -0,0 +1,7 @@
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)
return "5 ^ 3 ^ 2 = " & r1 & "
(5 ^ 3) ^ 2 = " & r2 & "
5 ^ (3 ^ 2) = " & r3

View file

@ -0,0 +1 @@
?"5^3^2 = "5 ^ 3 ^ 2 CHR$ (13)"(5^3)^2 = "(5 ^ 3) ^ 2 CHR$ (13)"5^(3^2) = "5 ^ (3 ^ 2);

View file

@ -0,0 +1,3 @@
print 5^3^2
print (5^3)^2
print 5^(3^2)

View file

@ -0,0 +1,4 @@
print "5^3^2 = "; 5^3^2
print "(5^3)^2 = "; (5^3)^2
print "5^(3^2) = "; 5^(3^2)
end

View file

@ -0,0 +1,3 @@
PRINT "5^3^2 = "; 5^3^2
PRINT "(5^3)^2 = "; (5^3)^2
PRINT "5^(3^2) = "; 5^(3^2)

View file

@ -0,0 +1 @@
put$str$("5^3^2: " 5^3^2 "\n(5^3)^2: " (5^3)^2 "\n5^(3^2): " 5^(3^2) \n)

View file

@ -0,0 +1,9 @@
#include <iostream>
#include <cmath>
int main() {
std::cout << "(5 ^ 3) ^ 2 = " << (uint) pow(pow(5,3), 2) << std::endl;
std::cout << "5 ^ (3 ^ 2) = "<< (uint) pow(5, (pow(3, 2)));
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,13 @@
#include <iostream>
#include <cmath>
enum my_int {};
inline my_int operator^(my_int a, my_int b) { return static_cast<my_int>(pow(a,b)); }
int main() {
my_int x = 5, y = 3, z = 2;
std::cout << "(5 ^ 3) ^ 2 = " << ((x^y)^z) << std::endl;
std::cout << "5 ^ (3 ^ 2) = "<< (x^(y^z));
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,20 @@
using System;
namespace exponents
{
class Program
{
static void Main(string[] args)
{
/*
* Like C, C# does not have an exponent operator.
* Exponentiation is done via Math.Pow, which
* only takes two arguments
*/
Console.WriteLine(Math.Pow(Math.Pow(5, 3), 2));
Console.WriteLine(Math.Pow(5, Math.Pow(3, 2)));
Console.Read();
}
}
}

View file

@ -0,0 +1,10 @@
#include<stdio.h>
#include<math.h>
int main()
{
printf("(5 ^ 3) ^ 2 = %.0f",pow(pow(5,3),2));
printf("\n5 ^ (3 ^ 2) = %.0f",pow(5,pow(3,2)));
return 0;
}

View file

@ -0,0 +1,7 @@
start_up = proc ()
po: stream := stream$primary_output()
stream$putl(po, "5**3**2 = " || int$unparse(5**3**2))
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

View file

@ -0,0 +1,3 @@
10 print "5^3^2 = "5^3^2
20 print "(5^3)^2 = "(5^3)^2
30 print "5^(3^2) = "5^(3^2)

View file

@ -0,0 +1,13 @@
(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

View file

@ -0,0 +1,2 @@
(expt (expt 5 3) 2)
(expt 5 (expt 3 2))

View file

@ -0,0 +1,8 @@
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);
}

View file

@ -0,0 +1,6 @@
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)))}');
}

View file

@ -0,0 +1,6 @@
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;

View file

@ -0,0 +1,16 @@
;; 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

View file

@ -0,0 +1,7 @@
USING: formatting math.functions ;
5 3 2 ^ ^
"5 3 2 ^ ^ %d\n" printf
5 3 ^ 2 ^
"5 3 ^ 2 ^ %d\n" printf

View file

@ -0,0 +1,10 @@
USING: formatting infix ;
[infix 5**3**2 infix]
"5**3**2 = %d\n" printf
[infix (5**3)**2 infix]
"(5**3)**2 = %d\n" printf
[infix 5**(3**2) infix]
"5**(3**2) = %d\n" printf

View file

@ -0,0 +1,3 @@
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)

View file

@ -0,0 +1,11 @@
' FB 1.05.0
' The exponentation operator in FB is ^ rather than **.
' In the absence of parenthesis this operator is
' left-associative. So the first example
' will have the same value as the second example.
Print "5^3^2 =>"; 5^3^2
Print "(5^3)^2 =>"; (5^3)^2
Print "5^(3^2) =>"; 5^(3^2)
Sleep

View file

@ -0,0 +1,3 @@
println["5^3^2 = " + 5^3^2]
println["(5^3)^2 = " + (5^3)^2]
println["5^(3^2) = " + 5^(3^2)]

View file

@ -0,0 +1,7 @@
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

View file

@ -0,0 +1,3 @@
10 PRINT "5^3^2 =" 5^3^2
20 PRINT "(5^3)^2 =" (5^3)^2
30 PRINT "5^(3^2) =" 5^(3^2)

View file

@ -0,0 +1,14 @@
package main
import "fmt"
import "math"
func main() {
var a, b, c float64
a = math.Pow(5, math.Pow(3, 2))
b = math.Pow(math.Pow(5, 3), 2)
c = math.Pow(5, math.Pow(3, 2))
fmt.Printf("5^3^2 = %.0f\n", a)
fmt.Printf("(5^3)^2 = %.0f\n", b)
fmt.Printf("5^(3^2) = %.0f\n", c)
}

View file

@ -0,0 +1,3 @@
println(" 5 ** 3 ** 2 == " + 5**3**2)
println("(5 ** 3)** 2 == " + (5**3)**2)
println(" 5 **(3 ** 2)== " + 5**(3**2))

View file

@ -0,0 +1,3 @@
100 PRINT "5^3^2 =";5^3^2
110 PRINT "(5^3)^2 =";(5^3)^2
120 PRINT "5^(3^2) =";5^(3^2)

View file

@ -0,0 +1,6 @@
5^3^2
1.95312e6
(5^3)^2
15625
5^(3^2)
1.95312e6

View file

@ -0,0 +1,2 @@
jq -n 'pow(pow(5;3);2)'
15625

View file

@ -0,0 +1,3 @@
def pow: reduce .[1:] as $i (.[0]; pow(.;$i))
[5,3,2] | pow

View file

@ -0,0 +1,6 @@
@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

View file

@ -0,0 +1,23 @@
// version 1.0.5-2
infix fun Int.ipow(exp: Int): Int = when {
exp < 0 -> throw IllegalArgumentException("negative exponents not allowed")
exp == 0 -> 1
else -> {
var ans = 1
var base = this
var e = exp
while(e != 0) {
if (e and 1 == 1) ans *= base
e = e shr 1
base *= base
}
ans
}
}
fun main(args: Array<String>) {
println("5**3**2 = ${5 ipow 3 ipow 2}")
println("(5**3)**2 = ${(5 ipow 3) ipow 2}")
println("5**(3**2) = ${5 ipow (3 ipow 2)}")
}

View file

@ -0,0 +1,4 @@
'{pow {pow 5 3} 2}
-> {pow {pow 5 3} 2}
'{pow 5 {pow 3 2}}
-> {pow 5 {pow 3 2}}

View file

@ -0,0 +1,3 @@
writeln " 5^3^2: ", 5^3^2
writeln "(5^3)^2: ", (5^3)^2
writeln "5^(3^2): ", 5^(3^2)

View file

@ -0,0 +1,3 @@
5 ^ 3 ^ 2. ;; 1953125
(5 ^ 3) ^ 2. ;; 15625
5 ^ (3 ^ 2). ;; 1953125

View file

@ -0,0 +1,3 @@
print("5^3^2 = " .. 5^3^2)
print("(5^3)^2 = " .. (5^3)^2)
print("5^(3^2) = " .. 5^(3^2))

View file

@ -0,0 +1,3 @@
10 PRINT "5^3^2 =" 5^3^2
20 PRINT "(5^3)^2 =" (5^3)^2
30 PRINT "5^(3^2) =" 5^(3^2)

View file

@ -0,0 +1,3 @@
5^3^2;
(5^3)^2;
5^(3^2);

View file

@ -0,0 +1,6 @@
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]]]

View file

@ -0,0 +1,5 @@
5 3 2 pow pow
"5 3 2 ^ ^ " print! puts!
5 3 pow 2 pow
"5 3 ^ 2 ^ " print! puts!

View file

@ -0,0 +1,6 @@
% println 5^3^2
15625
% println (5^3)^2
15625
% println 5^(3^2)
1953125

View file

@ -0,0 +1,7 @@
import math, sequtils
echo "5^3^2 = ", 5^3^2
echo "(5^3)^2 = ", (5^3)^2
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)

View file

@ -0,0 +1,3 @@
# 5. ** 3. ** 2. ;;
# 5. **( 3. ** 2.) ;;
#(5. ** 3. ) **2. ;;

View file

@ -0,0 +1,2 @@
f(s)=print(s" = "eval(s));
apply(f, ["5^3^2", "(5^3)^2", "5^(3^2)"]);

View file

@ -0,0 +1,5 @@
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;

View file

@ -0,0 +1 @@
say "$_ = " . eval($_) for qw/5**3**2 (5**3)**2 5**(3**2)/;

View file

@ -0,0 +1,4 @@
-->
<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;">)
<!--

View file

@ -0,0 +1,5 @@
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).

View file

@ -0,0 +1,5 @@
: (** (** 5 3) 2)
-> 15625
: (** 5 (** 3 2))
-> 1953125

View file

@ -0,0 +1,4 @@
OpenConsole()
PrintN("(5^3)^2 = " + Str(Pow(Pow(5, 3), 2)))
PrintN("5^(3^2) = " + Str(Pow(5, (Pow(3, 2)))))
CloseConsole()

View file

@ -0,0 +1,13 @@
>>> 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
>>>

View file

@ -0,0 +1,4 @@
PRINT "5^3^2 ="; 5^3^2
PRINT "(5^3)^2 ="; (5^3)^2
PRINT "5^(3^2) ="; 5^(3^2)
END

View file

@ -0,0 +1,2 @@
print(quote(5**3))
print(quote(5^3))

View file

@ -0,0 +1 @@
'^'('^'(5, 3), 2)

View file

@ -0,0 +1 @@
'^'(5, '^'(3, 2))

View file

@ -0,0 +1,2 @@
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")))

View file

@ -0,0 +1,2 @@
print(matrix(sapply(inputs, eval), dimnames = list(inputs, "Outputs")))
print(data.frame(Inputs = sapply(inputs, deparse), Outputs = sapply(inputs, eval))))

View file

@ -0,0 +1,11 @@
/*REXX program demonstrates various ways of multiple exponentiations. */
/*┌────────────────────────────────────────────────────────────────────┐
The REXX language uses ** for exponentiation.
Also, * * can be used.
| and even */*power of*/* |
*/
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.*/

View file

@ -0,0 +1,22 @@
#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))

View file

@ -0,0 +1,14 @@
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
# Unicode postfix exponents are supported as well:
demo '(5³)²';
demo '5³²';

View file

@ -0,0 +1,16 @@
Red["Exponentiation order"]
exprs: [
[5 ** 3 ** 2]
[(5 ** 3) ** 2]
[5 ** (3 ** 2)]
[power power 5 3 2] ;-- functions too
[power 5 power 3 2]
]
foreach expr exprs [
print [mold/only expr "=" do expr]
if find expr '** [
print [mold/only expr "=" math expr "using math"]
]
]

View file

@ -0,0 +1,2 @@
see "(5^3)^2 =>" + pow(pow(5,3),2) + nl
see "5^(3^2) =>" + pow(5,pow(3,2)) + nl

View file

@ -0,0 +1,2 @@
ar = ["5**3**2", "(5**3)**2", "5**(3**2)", "[5,3,2].inject(:**)"]
ar.each{|exp| puts "#{exp}:\t#{eval exp}"}

View file

@ -0,0 +1,4 @@
print "5^3^2 = "; 5^3^2
print "(5^3)^2 = "; (5^3)^2
print "5^(3^2) = "; 5^(3^2)
end

View file

@ -0,0 +1,5 @@
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)));
}

View file

@ -0,0 +1,8 @@
$ include "seed7_05.s7i";
const proc: main is func
begin
writeln("5**3**2 = " <& 5**3**2);
writeln("(5**3)**2 = " <& (5**3)**2);
writeln("5**(3**2) = " <& 5**(3**2));
end func;

View file

@ -0,0 +1,13 @@
var a = [
'5**3**2',
'(5**3)**2',
'5**(3**2)',
'5 ** 3 ** 2',
'5 ** 3**2',
'5**3 ** 2',
'[5,3,2]«**»',
]
a.each {|e|
"%-12s == %s\n".printf(e, eval(e))
}

View file

@ -0,0 +1,3 @@
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

View file

@ -0,0 +1,3 @@
10 PRINT "5**3**2 = ";5**3**2
20 PRINT "(5**3)**2 = ";(5**3)**2
30 PRINT "5**(3**2) = ";5**(3**2)

View file

@ -0,0 +1,3 @@
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).

View file

@ -0,0 +1,8 @@
. di (5^3^2)
15625
. di ((5^3)^2)
15625
. di (5^(3^2))
1953125

View file

@ -0,0 +1,8 @@
. mata (5^3^2)
15625
. mata ((5^3)^2)
15625
. mata (5^(3^2))
1953125

View file

@ -0,0 +1,37 @@
precedencegroup ExponentiationPrecedence {
associativity: left
higherThan: MultiplicationPrecedence
}
infix operator ** : ExponentiationPrecedence
@inlinable
public func ** <T: BinaryInteger>(lhs: T, rhs: T) -> T {
guard lhs != 0 else {
return 1
}
var x = lhs
var n = rhs
var y = T(1)
while n > 1 {
switch n & 1 {
case 0:
n /= 2
case 1:
y *= x
n = (n - 1) / 2
case _:
fatalError()
}
x *= x
}
return x * y
}
print(5 ** 3 ** 2)
print((5 ** 3) ** 2)
print(5 ** (3 ** 2))

View file

@ -0,0 +1,3 @@
foreach expression {5**3**2 (5**3)**2 5**(3**2)} {
puts "${expression}:\t[expr $expression]"
}

View file

@ -0,0 +1,4 @@
PRINT "5^3^2 ="; 5^3^2
PRINT "(5^3)^2 ="; (5^3)^2
PRINT "5^(3^2) ="; 5^(3^2)
END

View file

@ -0,0 +1,5 @@
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

View file

@ -0,0 +1,3 @@
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)

View file

@ -0,0 +1,11 @@
// Exponentiation order example:
@SAY "5**3**2 = " ( 5**3**2 );
@SAY "(5**3)**2 = " ( (5**3)**2 );
@SAY "5**(3**2) = " ( 5**(3**2) );
/] Output:
5**3**2 = 1953125
(5**3)**2 = 15625
5**(3**2) = 1953125

View file

@ -0,0 +1,7 @@
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) {
Fmt.print("$-9s -> $d", ops[i], results[i])
}

View file

@ -0,0 +1,11 @@
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

View file

@ -0,0 +1,5 @@
[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);
]

View file

@ -0,0 +1,4 @@
print "5^3^2 = ", 5^3^2
print "(5^3)^2 = ", (5^3)^2
print "5^(3^2) = ", 5^(3^2)
end

View file

@ -0,0 +1,3 @@
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))));