RosettaCodeData/Task/Exponentiation-operator/ALGOL-68/exponentiation-operator-2.alg

51 lines
1.8 KiB
Plaintext

main:(
INT two=2, thirty=30; # test constants #
PROC VOID undefined;
# First implement exponentiation using a rather slow but sure FOR loop #
PROC int pow = (INT base, exponent)INT: ( # PROC cannot be over loaded #
IF exponent<0 THEN undefined FI;
INT out:=( exponent=0 | 1 | base );
FROM 2 TO exponent DO out*:=base OD;
out
);
printf(($" One Gibi-unit is: int pow("g(0)","g(0)")="g(0)" - (cost: "g(0)
" INT multiplications)"l$,two, thirty, int pow(two,thirty),thirty-1));
# implement exponentiation using a faster binary technique and WHILE LOOP #
OP ** = (INT base, exponent)INT:
IF base = 0 THEN 0 ELIF base = 1 THEN 1
ELIF exponent = 0 THEN 1 ELIF exponent = 1 THEN base
ELIF ODD exponent THEN
(base*base) ** (exponent OVER 2) * base
ELSE
(base*base) ** (exponent OVER 2)
FI;
printf(($" One Gibi-unit is: "g(0)"**"g(0)"="g(0)" - (cost: "g(0)
" INT multiplications)"l$,two, thirty, two ** thirty,8));
OP ** = (REAL in base, INT in exponent)REAL: ( # ** INT Operator can be overloaded #
REAL base := ( in exponent<0 | 1/in base | in base);
INT exponent := ABS in exponent;
IF base = 0 THEN 0 ELIF base = 1 THEN 1
ELIF exponent = 0 THEN 1 ELIF exponent = 1 THEN base
ELIF ODD exponent THEN
(base*base) ** (exponent OVER 2) * base
ELSE
(base*base) ** (exponent OVER 2)
FI
);
printf(($" One Gibi-unit is: "g(0,1)"**"g(0)"="g(0,1)" - (cost: "g(0)
" REAL multiplications)"l$, 2.0, thirty, 2.0 ** thirty,8));
OP ** = (REAL base, REAL exponent)REAL: ( # ** REAL Operator can be overloaded #
exp(ln(base)*exponent)
);
printf(($" One Gibi-unit is: "g(0,1)"**"g(0,1)"="g(0,1)" - (cost: "
"depends on precision)"l$, 2.0, 30.0, 2.0 ** 30.0))
)