Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Exponentiation-operator/00-META.yaml
Normal file
5
Task/Exponentiation-operator/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Arithmetic
|
||||
from: http://rosettacode.org/wiki/Exponentiation_operator
|
||||
note: Arithmetic operations
|
||||
15
Task/Exponentiation-operator/00-TASK.txt
Normal file
15
Task/Exponentiation-operator/00-TASK.txt
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
Most programming languages have a built-in implementation of exponentiation.
|
||||
|
||||
|
||||
;Task:
|
||||
Re-implement integer exponentiation for both <big>int<sup>int</sup></big> and <big>float<sup>int</sup></big> as both a procedure, and an operator (if your language supports operator definition).
|
||||
|
||||
If the language supports operator (or procedure) overloading, then an overloaded form should be provided for both <big>int<sup>int</sup></big> and <big>float<sup>int</sup></big> variants.
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Exponentiation order]]
|
||||
* [[Arbitrary-precision_integers_(included)|arbitrary-precision integers (included)]]
|
||||
* [[Exponentiation with infix operators in (or operating on) the base]]
|
||||
<br><br>
|
||||
|
||||
17
Task/Exponentiation-operator/11l/exponentiation-operator.11l
Normal file
17
Task/Exponentiation-operator/11l/exponentiation-operator.11l
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
F my_pow(base, exp) -> Float
|
||||
I exp < 0
|
||||
R 1 / my_pow(base, -exp)
|
||||
I exp == 0
|
||||
R 1
|
||||
V ans = base
|
||||
L 0 .< exp - 1
|
||||
ans *= base
|
||||
R ans
|
||||
|
||||
print(‘2 ^ 3 = ’my_pow(2, 3))
|
||||
print(‘1 ^ -10 = ’my_pow(1, -10))
|
||||
print(‘-1 ^ -3 = ’my_pow(-1, -3))
|
||||
print()
|
||||
print(‘2.0 ^ -3 = ’my_pow(2.0, -3))
|
||||
print(‘1.5 ^ 0 = ’my_pow(1.5, 0))
|
||||
print(‘4.5 ^ 2 = ’my_pow(4.5, 2))
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
ExponentUnsigned:
|
||||
;input: D0.W = BASE
|
||||
; D1.W = EXPONENT
|
||||
; OUTPUTS TO D0
|
||||
; NO OVERFLOW PROTECTION - USE AT YOUR OWN RISK!
|
||||
;HIGH WORDS OF D0 AND D1 ARE CLEARED.
|
||||
;clobbers D1
|
||||
|
||||
MOVE.L D2,-(SP)
|
||||
|
||||
;using DBRAs lets us simultaneously subtract and compare
|
||||
DBRA D1,.test_if_one
|
||||
MOVEQ.L #1,D0 ;executes only if D1 was 0 to start with
|
||||
.test_if_one:
|
||||
DBRA D1,.go
|
||||
bra .done ;executes only if D1 was 1 to start with
|
||||
.go:
|
||||
;else, multiply D0 by its ORIGINAL self repeatedly.
|
||||
MOVE.L D0,D2
|
||||
.loop:
|
||||
MULU D0,D2
|
||||
DBRA D1,.loop
|
||||
|
||||
MOVE.L D2,D0
|
||||
.done:
|
||||
MOVE.L (SP)+,D2
|
||||
RTS
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
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: (
|
||||
BITS binary exponent:=BIN exponent ; # do exponent arithmetic in binary #
|
||||
INT out := IF bits width ELEM binary exponent THEN base ELSE 1 FI;
|
||||
INT sq := IF exponent < 0 THEN undefined; ~ ELSE base FI;
|
||||
|
||||
WHILE
|
||||
binary exponent := binary exponent SHR 1;
|
||||
binary exponent /= BIN 0
|
||||
DO
|
||||
sq *:= sq;
|
||||
IF bits width ELEM binary exponent THEN out *:= sq FI
|
||||
OD;
|
||||
out
|
||||
);
|
||||
|
||||
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;
|
||||
BITS binary exponent:=BIN exponent ; # do exponent arithmetic in binary #
|
||||
REAL out := IF bits width ELEM binary exponent THEN base ELSE 1 FI;
|
||||
REAL sq := base;
|
||||
|
||||
WHILE
|
||||
binary exponent := binary exponent SHR 1;
|
||||
binary exponent /= BIN 0
|
||||
DO
|
||||
sq *:= sq;
|
||||
IF bits width ELEM binary exponent THEN out *:= sq FI
|
||||
OD;
|
||||
out
|
||||
);
|
||||
|
||||
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))
|
||||
)
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
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))
|
||||
)
|
||||
|
|
@ -0,0 +1 @@
|
|||
$ awk 'function pow(x,n){r=1;for(i=0;i<n;i++)r=r*x;return r}{print pow($1,$2)}'
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
If you want to use arbitrary precision number with (more recent) awk, you have to use -M option :
|
||||
$ gawk -M '{ printf("%f\n",$1^$2) }'
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
And if you want to use locales for decimal separator, you have tu use -N option :
|
||||
$ gawk -N '{ printf("%f\n",$1^$2) }'
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
|
||||
|
||||
INT FUNC PowerI(INT base,exp)
|
||||
INT res,i
|
||||
|
||||
IF exp<0 THEN Break() FI
|
||||
|
||||
res=1
|
||||
FOR i=1 TO exp
|
||||
DO
|
||||
res==*base
|
||||
OD
|
||||
RETURN (res)
|
||||
|
||||
PROC PowerR(REAL POINTER base INT exp
|
||||
REAL POINTER res)
|
||||
INT i
|
||||
REAL tmp
|
||||
|
||||
IF exp<0 THEN Break() FI
|
||||
|
||||
IntToReal(1,res)
|
||||
FOR i=1 TO exp
|
||||
DO
|
||||
RealMult(res,base,tmp)
|
||||
RealAssign(tmp,res)
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC TestI(INT base,exp)
|
||||
INT res
|
||||
|
||||
res=PowerI(base,exp)
|
||||
PrintF("%I^%I=%I%E",base,exp,res)
|
||||
RETURN
|
||||
|
||||
PROC TestR(REAL POINTER base INT exp)
|
||||
REAL res
|
||||
|
||||
PowerR(base,exp,res)
|
||||
PrintR(base) PrintF("^%I=",exp)
|
||||
PrintRE(res)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
REAL base
|
||||
|
||||
Put(125) PutE() ;clear screen
|
||||
|
||||
TestI(27,3)
|
||||
TestI(2,12)
|
||||
TestI(-3,9)
|
||||
TestI(1,1000)
|
||||
TestI(20000,0)
|
||||
|
||||
ValR("3.141592654",base)
|
||||
TestR(base,10)
|
||||
ValR("-1.11",base)
|
||||
TestR(base,99)
|
||||
ValR("0.123456789",base)
|
||||
TestR(base,1)
|
||||
ValR("987654.321",base)
|
||||
TestR(base,0)
|
||||
RETURN
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package Integer_Exponentiation is
|
||||
-- int^int
|
||||
procedure Exponentiate (Argument : in Integer;
|
||||
Exponent : in Natural;
|
||||
Result : out Integer);
|
||||
function "**" (Left : Integer;
|
||||
Right : Natural) return Integer;
|
||||
|
||||
-- real^int
|
||||
procedure Exponentiate (Argument : in Float;
|
||||
Exponent : in Integer;
|
||||
Result : out Float);
|
||||
function "**" (Left : Float;
|
||||
Right : Integer) return Float;
|
||||
end Integer_Exponentiation;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
with Ada.Float_Text_IO, Ada.Integer_Text_IO, Ada.Text_IO;
|
||||
with Integer_Exponentiation;
|
||||
|
||||
procedure Test_Integer_Exponentiation is
|
||||
use Ada.Float_Text_IO, Ada.Integer_Text_IO, Ada.Text_IO;
|
||||
use Integer_Exponentiation;
|
||||
R : Float;
|
||||
I : Integer;
|
||||
begin
|
||||
Exponentiate (Argument => 2.5, Exponent => 3, Result => R);
|
||||
Put ("2.5 ^ 3 = ");
|
||||
Put (R, Fore => 2, Aft => 4, Exp => 0);
|
||||
New_Line;
|
||||
|
||||
Exponentiate (Argument => -12, Exponent => 3, Result => I);
|
||||
Put ("-12 ^ 3 = ");
|
||||
Put (I, Width => 7);
|
||||
New_Line;
|
||||
end Test_Integer_Exponentiation;
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package body Integer_Exponentiation is
|
||||
-- int^int
|
||||
procedure Exponentiate (Argument : in Integer;
|
||||
Exponent : in Natural;
|
||||
Result : out Integer) is
|
||||
begin
|
||||
Result := 1;
|
||||
for Counter in 1 .. Exponent loop
|
||||
Result := Result * Argument;
|
||||
end loop;
|
||||
end Exponentiate;
|
||||
|
||||
function "**" (Left : Integer;
|
||||
Right : Natural) return Integer is
|
||||
Result : Integer;
|
||||
begin
|
||||
Exponentiate (Argument => Left,
|
||||
Exponent => Right,
|
||||
Result => Result);
|
||||
return Result;
|
||||
end "**";
|
||||
|
||||
-- real^int
|
||||
procedure Exponentiate (Argument : in Float;
|
||||
Exponent : in Integer;
|
||||
Result : out Float) is
|
||||
begin
|
||||
Result := 1.0;
|
||||
if Exponent < 0 then
|
||||
for Counter in Exponent .. -1 loop
|
||||
Result := Result / Argument;
|
||||
end loop;
|
||||
else
|
||||
for Counter in 1 .. Exponent loop
|
||||
Result := Result * Argument;
|
||||
end loop;
|
||||
end if;
|
||||
end Exponentiate;
|
||||
|
||||
function "**" (Left : Float;
|
||||
Right : Integer) return Float is
|
||||
Result : Float;
|
||||
begin
|
||||
Exponentiate (Argument => Left,
|
||||
Exponent => Right,
|
||||
Result => Result);
|
||||
return Result;
|
||||
end "**";
|
||||
end Integer_Exponentiation;
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
on exponentiationOperatorTask(n, power)
|
||||
set power to power as integer
|
||||
set operatorResult to (n ^ power)
|
||||
set handlerResult to exponentiate(n, power)
|
||||
|
||||
return {operator:operatorResult, |handler|:handlerResult}
|
||||
end exponentiationOperatorTask
|
||||
|
||||
on exponentiate(n, power)
|
||||
-- AppleScript's ^ operator returns a real (ie. float) result. This handler does the same.
|
||||
set n to n as real
|
||||
set out to 1.0
|
||||
if (power < 0) then
|
||||
repeat -power times
|
||||
set out to out / n
|
||||
end repeat
|
||||
else
|
||||
repeat power times
|
||||
set out to out * n
|
||||
end repeat
|
||||
end if
|
||||
|
||||
return out
|
||||
end exponentiate
|
||||
|
||||
exponentiationOperatorTask(3, 3) --> {operator:27.0, |handler|:27.0}
|
||||
exponentiationOperatorTask(2, 16) --> {operator:6.5536E+4, |handler|:6.5536E+4}
|
||||
exponentiationOperatorTask(2.5, 10) --> {operator:9536.7431640625, |handler|:9536.7431640625}
|
||||
exponentiationOperatorTask(2.5, -10) --> {operator:1.048576E-4, |handler|:1.048576E-4}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
myPow: function [base,xp][
|
||||
if xp < 0 [
|
||||
(floating? base)? -> return 1 // myPow base neg xp
|
||||
-> return 1 / myPow base neg xp
|
||||
]
|
||||
|
||||
if xp = 0 ->
|
||||
return 1
|
||||
|
||||
ans: 1
|
||||
while [xp > 0][
|
||||
ans: ans * base
|
||||
xp: xp - 1
|
||||
]
|
||||
return ans
|
||||
]
|
||||
|
||||
print ["2 ^ 3 =" myPow 2 3]
|
||||
print ["1 ^ -10 =" myPow 1 neg 10]
|
||||
print ["-1 ^ -3 =" myPow neg 1 neg 3]
|
||||
print ""
|
||||
print ["2.0 ^ -3 =" myPow 2.0 neg 3]
|
||||
print ["1.5 ^ 0 =" myPow 1.5 0]
|
||||
print ["4.5 ^ 2 =" myPow 4.5 2]
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
MsgBox % Pow(5,3)
|
||||
MsgBox % Pow(2.5,4)
|
||||
|
||||
Pow(x, n){
|
||||
r:=1
|
||||
loop %n%
|
||||
r *= x
|
||||
return r
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
DECLARE FUNCTION powL& (x AS INTEGER, y AS INTEGER)
|
||||
DECLARE FUNCTION powS# (x AS SINGLE, y AS INTEGER)
|
||||
|
||||
DIM x AS INTEGER, y AS INTEGER
|
||||
DIM a AS SINGLE
|
||||
|
||||
RANDOMIZE TIMER
|
||||
a = RND * 10
|
||||
x = INT(RND * 10)
|
||||
y = INT(RND * 10)
|
||||
PRINT x, y, powL&(x, y)
|
||||
PRINT a, y, powS#(a, y)
|
||||
|
||||
FUNCTION powL& (x AS INTEGER, y AS INTEGER)
|
||||
DIM n AS INTEGER, m AS LONG
|
||||
IF x <> 0 THEN
|
||||
m = 1
|
||||
IF SGN(y) > 0 THEN
|
||||
FOR n = 1 TO y
|
||||
m = m * x
|
||||
NEXT
|
||||
END IF
|
||||
END IF
|
||||
powL& = m
|
||||
END FUNCTION
|
||||
|
||||
FUNCTION powS# (x AS SINGLE, y AS INTEGER)
|
||||
DIM n AS INTEGER, m AS DOUBLE
|
||||
IF x <> 0 THEN
|
||||
m = 1
|
||||
IF y <> 0 THEN
|
||||
FOR n = 1 TO y
|
||||
m = m * x
|
||||
NEXT
|
||||
IF y < 0 THEN m = 1# / m
|
||||
END IF
|
||||
END IF
|
||||
powS# = m
|
||||
END FUNCTION
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
ni = int(rand * 10)
|
||||
nf = round(rand * 10, 4)
|
||||
ex = int(rand * 10)
|
||||
print " "; ni; " ^ "; ex; " = "; iPow (ni, ex)
|
||||
print nf; " ^ "; ex; " = "; fPow (nf, ex)
|
||||
end
|
||||
|
||||
function iPow (base, exponent)
|
||||
if exponent = 0 then return 1
|
||||
if exponent = 1 then return base
|
||||
if exponent < 0 then return 1 / iPow(base, -exponent)
|
||||
power = base
|
||||
for i = 2 to exponent
|
||||
power *= base
|
||||
next
|
||||
return power
|
||||
end function
|
||||
|
||||
function fPow (base, exponent)
|
||||
if exponent = 0.0 then return 1.0
|
||||
if exponent = 1.0 then return base
|
||||
if exponent < 0.0 then return 1.0 / fPow(base, -exponent)
|
||||
power = base
|
||||
for i = 2 to exponent
|
||||
power *= base
|
||||
next
|
||||
return power
|
||||
end function
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
PRINT "11^5 = " ; FNipow(11, 5)
|
||||
PRINT "PI^3 = " ; FNfpow(PI, 3)
|
||||
END
|
||||
|
||||
DEF FNipow(A%, B%)
|
||||
LOCAL I%, P%
|
||||
P% = 1
|
||||
FOR I% = 1 TO 32
|
||||
P% *= P%
|
||||
IF B% < 0 THEN P% *= A%
|
||||
B% = B% << 1
|
||||
NEXT
|
||||
= P%
|
||||
|
||||
DEF FNfpow(A, B%)
|
||||
LOCAL I%, P
|
||||
P = 1
|
||||
FOR I% = 1 TO 32
|
||||
P *= P
|
||||
IF B% < 0 THEN P *= A
|
||||
B% = B% << 1
|
||||
NEXT
|
||||
= P
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
v v \<
|
||||
>&:32p&1-\>32g*\1-:|
|
||||
$
|
||||
.
|
||||
@
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
#Procedure
|
||||
exp = { base, exp |
|
||||
1.to(exp).reduce 1, { m, n | m = m * base }
|
||||
}
|
||||
|
||||
#Numbers are weird
|
||||
1.parent.^ = { rhs |
|
||||
num = my
|
||||
1.to(rhs).reduce 1 { m, n | m = m * num }
|
||||
}
|
||||
|
||||
p exp 2 5 #Prints 32
|
||||
p 2 ^ 5 #Prints 32
|
||||
32
Task/Exponentiation-operator/C++/exponentiation-operator.cpp
Normal file
32
Task/Exponentiation-operator/C++/exponentiation-operator.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
template<typename Number>
|
||||
Number power(Number base, int exponent)
|
||||
{
|
||||
int zerodir;
|
||||
Number factor;
|
||||
if (exponent < 0)
|
||||
{
|
||||
zerodir = 1;
|
||||
factor = Number(1)/base;
|
||||
}
|
||||
else
|
||||
{
|
||||
zerodir = -1;
|
||||
factor = base;
|
||||
}
|
||||
|
||||
Number result(1);
|
||||
while (exponent != 0)
|
||||
{
|
||||
if (exponent % 2 != 0)
|
||||
{
|
||||
result *= factor;
|
||||
exponent += zerodir;
|
||||
}
|
||||
else
|
||||
{
|
||||
factor *= factor;
|
||||
exponent /= 2;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("5^5 = " + Expon(5, 5));
|
||||
Console.WriteLine("5.5^5 = " + Expon(5.5, 5));
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
static double Expon(int Val, int Pow)
|
||||
{
|
||||
return Math.Pow(Val, Pow);
|
||||
}
|
||||
static double Expon(double Val, int Pow)
|
||||
{
|
||||
return Math.Pow(Val, Pow);
|
||||
}
|
||||
43
Task/Exponentiation-operator/C/exponentiation-operator-1.c
Normal file
43
Task/Exponentiation-operator/C/exponentiation-operator-1.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
int ipow(int base, int exp)
|
||||
{
|
||||
int pow = base;
|
||||
int v = 1;
|
||||
if (exp < 0) {
|
||||
assert (base != 0); /* divide by zero */
|
||||
return (base*base != 1)? 0: (exp&1)? base : 1;
|
||||
}
|
||||
|
||||
while(exp > 0 )
|
||||
{
|
||||
if (exp & 1) v *= pow;
|
||||
pow *= pow;
|
||||
exp >>= 1;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
double dpow(double base, int exp)
|
||||
{
|
||||
double v=1.0;
|
||||
double pow = (exp <0)? 1.0/base : base;
|
||||
if (exp < 0) exp = - exp;
|
||||
|
||||
while(exp > 0 )
|
||||
{
|
||||
if (exp & 1) v *= pow;
|
||||
pow *= pow;
|
||||
exp >>= 1;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("2^6 = %d\n", ipow(2,6));
|
||||
printf("2^-6 = %d\n", ipow(2,-6));
|
||||
printf("2.71^6 = %lf\n", dpow(2.71,6));
|
||||
printf("2.71^-6 = %lf\n", dpow(2.71,-6));
|
||||
}
|
||||
13
Task/Exponentiation-operator/C/exponentiation-operator-2.c
Normal file
13
Task/Exponentiation-operator/C/exponentiation-operator-2.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#define generic_pow(base, exp)\
|
||||
_Generic((base),\
|
||||
double: dpow,\
|
||||
int: ipow)\
|
||||
(base, exp)
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("2^6 = %d\n", generic_pow(2,6));
|
||||
printf("2^-6 = %d\n", generic_pow(2,-6));
|
||||
printf("2.71^6 = %lf\n", generic_pow(2.71,6));
|
||||
printf("2.71^-6 = %lf\n", generic_pow(2.71,-6));
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
(defn ** [x n] (reduce * (repeat n x)))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(defun my-expt-do (a b)
|
||||
(do ((x 1 (* x a))
|
||||
(y 0 (+ y 1)))
|
||||
((= y b) x)))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(defun my-expt-rec (a b)
|
||||
(cond
|
||||
((= b 0) 1)
|
||||
(t (* a (my-expt-rec a (- b 1))))))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(defun ^ (a b)
|
||||
(do ((x 1 (* x a))
|
||||
(y 0 (+ y 1)))
|
||||
((= y b) x)))
|
||||
47
Task/Exponentiation-operator/D/exponentiation-operator.d
Normal file
47
Task/Exponentiation-operator/D/exponentiation-operator.d
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import std.stdio, std.conv;
|
||||
|
||||
struct Number(T) {
|
||||
T x; // base
|
||||
alias x this;
|
||||
string toString() const { return text(x); }
|
||||
|
||||
Number opBinary(string op)(in int exponent)
|
||||
const pure nothrow @nogc if (op == "^^") in {
|
||||
if (exponent < 0)
|
||||
assert (x != 0, "Division by zero");
|
||||
} body {
|
||||
debug puts("opBinary ^^");
|
||||
|
||||
int zerodir;
|
||||
T factor;
|
||||
if (exponent < 0) {
|
||||
zerodir = +1;
|
||||
factor = T(1) / x;
|
||||
} else {
|
||||
zerodir = -1;
|
||||
factor = x;
|
||||
}
|
||||
|
||||
T result = 1;
|
||||
int e = exponent;
|
||||
while (e != 0)
|
||||
if (e % 2 != 0) {
|
||||
result *= factor;
|
||||
e += zerodir;
|
||||
} else {
|
||||
factor *= factor;
|
||||
e /= 2;
|
||||
}
|
||||
|
||||
return Number(result);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
alias Double = Number!double;
|
||||
writeln(Double(2.5) ^^ 5);
|
||||
|
||||
alias Int = Number!int;
|
||||
writeln(Int(3) ^^ 3);
|
||||
writeln(Int(0) ^^ -2); // Division by zero.
|
||||
}
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
program Exponentiation_operator;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
System.SysUtils;
|
||||
|
||||
type
|
||||
TDouble = record
|
||||
Value: Double;
|
||||
class operator Implicit(a: TDouble): Double;
|
||||
class operator Implicit(a: Double): TDouble;
|
||||
class operator Implicit(a: TDouble): string;
|
||||
class operator LogicalXor(a: TDouble; b: Integer): TDouble;
|
||||
end;
|
||||
|
||||
TInteger = record
|
||||
Value: Integer;
|
||||
class operator Implicit(a: TInteger): Integer;
|
||||
class operator Implicit(a: Integer): TInteger;
|
||||
class operator Implicit(a: TInteger): string;
|
||||
class operator LogicalXor(a: TInteger; b: Integer): TInteger;
|
||||
end;
|
||||
|
||||
{ TDouble }
|
||||
|
||||
class operator TDouble.Implicit(a: TDouble): Double;
|
||||
begin
|
||||
Result := a.Value;
|
||||
end;
|
||||
|
||||
class operator TDouble.Implicit(a: Double): TDouble;
|
||||
begin
|
||||
Result.Value := a;
|
||||
end;
|
||||
|
||||
class operator TDouble.Implicit(a: TDouble): string;
|
||||
begin
|
||||
Result := a.Value.ToString;
|
||||
end;
|
||||
|
||||
class operator TDouble.LogicalXor(a: TDouble; b: Integer): TDouble;
|
||||
var
|
||||
i: Integer;
|
||||
val: Double;
|
||||
begin
|
||||
val := 1;
|
||||
for i := 1 to b do
|
||||
val := val * a.Value;
|
||||
Result.Value := val;
|
||||
end;
|
||||
|
||||
{ TInteger }
|
||||
|
||||
class operator TInteger.Implicit(a: TInteger): Integer;
|
||||
begin
|
||||
Result := a.Value;
|
||||
end;
|
||||
|
||||
class operator TInteger.Implicit(a: Integer): TInteger;
|
||||
begin
|
||||
Result.Value := a;
|
||||
end;
|
||||
|
||||
class operator TInteger.Implicit(a: TInteger): string;
|
||||
begin
|
||||
Result := a.Value.ToString;
|
||||
end;
|
||||
|
||||
class operator TInteger.LogicalXor(a: TInteger; b: Integer): TInteger;
|
||||
var
|
||||
val, i: Integer;
|
||||
begin
|
||||
if b < 0 then
|
||||
raise Exception.Create('Expoent must be greater or equal zero');
|
||||
|
||||
val := 1;
|
||||
for i := 1 to b do
|
||||
val := val * a.Value;
|
||||
Result.Value := val;
|
||||
end;
|
||||
|
||||
procedure Print(s: string);
|
||||
begin
|
||||
Write(s);
|
||||
end;
|
||||
|
||||
var
|
||||
valF: TDouble;
|
||||
valI: TInteger;
|
||||
|
||||
begin
|
||||
valF := 5.5;
|
||||
valI := 5;
|
||||
|
||||
// Delphi don't have "**" or "^" operator for overload,
|
||||
// "xor" operator has used instead
|
||||
Print('5^5 = ');
|
||||
Print(valI xor 5);
|
||||
print(#10);
|
||||
|
||||
Print('5.5^5 = ');
|
||||
Print(valF xor 5);
|
||||
print(#10);
|
||||
|
||||
readln;
|
||||
end.
|
||||
11
Task/Exponentiation-operator/E/exponentiation-operator.e
Normal file
11
Task/Exponentiation-operator/E/exponentiation-operator.e
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def power(base, exponent :int) {
|
||||
var r := base
|
||||
if (exponent < 0) {
|
||||
for _ in exponent..0 { r /= base }
|
||||
} else if (exponent <=> 0) {
|
||||
return 1
|
||||
} else {
|
||||
for _ in 2..exponent { r *= base }
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
PROGRAM POWER
|
||||
|
||||
PROCEDURE POWER(A,B->POW) ! this routine handles only *INTEGER* powers
|
||||
LOCAL FLAG%
|
||||
IF B<0 THEN B=-B FLAG%=TRUE
|
||||
POW=1
|
||||
FOR X=1 TO B DO
|
||||
POW=POW*A
|
||||
END FOR
|
||||
IF FLAG% THEN POW=1/POW
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
POWER(11,-2->POW) PRINT(POW)
|
||||
POWER(π,3->POW) PRINT(POW)
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
;; this exponentiation function handles integer, rational or float x.
|
||||
;; n is a positive or negative integer.
|
||||
|
||||
(define (** x n) (cond
|
||||
((zero? n) 1)
|
||||
((< n 0) (/ (** x (- n)))) ;; x**-n = 1 / x**n
|
||||
((= n 1) x)
|
||||
((= n 0) 1)
|
||||
((odd? n) (* x (** x (1- n)))) ;; x**(2p+1) = x * x**2p
|
||||
(else (let ((m (** x (/ n 2)))) (* m m))))) ;; x**2p = (x**p) * (x**p)
|
||||
|
||||
(** 3 0) → 1
|
||||
(** 3 4) → 81
|
||||
(** 3 5) → 243
|
||||
(** 10 10) → 10000000000
|
||||
(** 1.3 10) → 13.785849184900007
|
||||
|
||||
(** -3 5) → -243
|
||||
(** 3 -4) → 1/81
|
||||
(** 3.7 -4) → 0.005335720890574502
|
||||
(** 2/3 7) → 128/2187
|
||||
|
||||
(lib 'bigint)
|
||||
(** 666 42) →
|
||||
38540524895511613165266748863173814985473295063157418576769816295283207864908351682948692085553606681763707358759878656
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
open number
|
||||
|
||||
_ ^ 0 = 1
|
||||
x ^ n | n > 0 = f x (n - 1) x
|
||||
|else = fail "Negative exponent"
|
||||
where f _ 0 y = y
|
||||
f a d y = g a d
|
||||
where g b i | even i = g (b * b) (i `quot` 2)
|
||||
| else = f b (i - 1) (b * y)
|
||||
|
||||
(12 ^ 4, 12 ** 4)
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
open number
|
||||
|
||||
//Function quot from number module is defined only for
|
||||
//integral numbers. We can use this as an universal quot.
|
||||
uquot x y | x is Integral = x `quot` y
|
||||
| else = x / y
|
||||
|
||||
//Changing implementation by using generic numeric literals
|
||||
//(e.g. 2u) and elimitating all comparisons with 0.
|
||||
!x ^ n | n ~= 0u = 1u
|
||||
| n > 0u = f x (n - 1u) x
|
||||
| else = fail "Negative exponent"
|
||||
where f a d y
|
||||
| d ~= 0u = y
|
||||
| else = g a d
|
||||
where g b i | even i = g (b * b) (i `uquot` 2u)
|
||||
| else = f b (i - 1u) (b * y)
|
||||
|
||||
|
||||
(12 ^ 4, 12.34 ^ 4.04)
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
open number
|
||||
|
||||
//A class that defines our overloadable function
|
||||
class Exponent a where
|
||||
(^) a->a->_
|
||||
|
||||
//Implementation for integers
|
||||
instance Exponent Int where
|
||||
_ ^ 0 = 1
|
||||
x ^ n | n > 0 = f x (n - 1) x
|
||||
|else = fail "Negative exponent"
|
||||
where f _ 0 y = y
|
||||
f a d y = g a d
|
||||
where g b i | even i = g (b * b) (i `quot` 2)
|
||||
| else = f b (i - 1) (b * y)
|
||||
|
||||
//Implementation for floats
|
||||
instance Exponent Single where
|
||||
x ^ n | n < 0.001 = 1
|
||||
| n > 0 = f x (n - 1) x
|
||||
| else = fail "Negative exponent"
|
||||
where f a d y
|
||||
| d < 0.001 = y
|
||||
| else = g a d
|
||||
where g b i | even i = g (b * b) (i / 2)
|
||||
| else = f b (i - 1) (b * y)
|
||||
|
||||
(12 ^ 4, 12.34 ^ 4.04)
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
defmodule My do
|
||||
def exp(x,y) when is_integer(x) and is_integer(y) and y>=0 do
|
||||
IO.write("int> ") # debug test
|
||||
exp_int(x,y)
|
||||
end
|
||||
def exp(x,y) when is_integer(y) do
|
||||
IO.write("float> ") # debug test
|
||||
exp_float(x,y)
|
||||
end
|
||||
def exp(x,y), do: (IO.write(" "); :math.pow(x,y))
|
||||
|
||||
defp exp_int(_,0), do: 1
|
||||
defp exp_int(x,y), do: Enum.reduce(1..y, 1, fn _,acc -> x * acc end)
|
||||
|
||||
defp exp_float(_,y) when y==0, do: 1.0
|
||||
defp exp_float(x,y) when y<0, do: 1/exp_float(x,-y)
|
||||
defp exp_float(x,y), do: Enum.reduce(1..y, 1, fn _,acc -> x * acc end)
|
||||
end
|
||||
|
||||
list = [{2,0}, {2,3}, {2,-2},
|
||||
{2.0,0}, {2.0,3}, {2.0,-2},
|
||||
{0.5,0}, {0.5,3}, {0.5,-2},
|
||||
{-2,2}, {-2,3}, {-2.0,2}, {-2.0,3},
|
||||
]
|
||||
IO.puts " ___My.exp___ __:math.pow_"
|
||||
Enum.each(list, fn {x,y} ->
|
||||
sxy = "#{x} ** #{y}"
|
||||
sexp = inspect My.exp(x,y)
|
||||
spow = inspect :math.pow(x,y) # For the comparison
|
||||
:io.fwrite("~10s = ~12s, ~12s~n", [sxy, sexp, spow])
|
||||
end)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
pow(X, Y) when Y < 0 ->
|
||||
1/pow(X, -Y);
|
||||
pow(X, Y) when is_integer(Y) ->
|
||||
pow(X, Y, 1).
|
||||
|
||||
pow(_, 0, B) ->
|
||||
B;
|
||||
pow(X, Y, B) ->
|
||||
B2 = if Y rem 2 =:= 0 -> B; true -> X * B end,
|
||||
pow(X * X, Y div 2, B2).
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
//Integer Exponentiation, more interesting anyway than repeated multiplication. Nigel Galloway, October 12th., 2018
|
||||
let rec myExp n g=match g with
|
||||
|0 ->1
|
||||
|g when g%2=1 ->n*(myExp n (g-1))
|
||||
|_ ->let p=myExp n (g/2) in p*p
|
||||
|
||||
printfn "%d" (myExp 3 15)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
: pow ( f n -- f' )
|
||||
dup 0 < [ abs pow recip ]
|
||||
[ [ 1 ] 2dip swap [ * ] curry times ] if ;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
: pow ( f n -- f' )
|
||||
{
|
||||
{ [ dup 0 < ] [ abs pow recip ] }
|
||||
{ [ dup 0 = ] [ 2drop 1 ] }
|
||||
[ [ 2 mod 1 = swap 1 ? ] [ [ sq ] [ 2 /i ] bi* pow ] 2bi * ]
|
||||
} cond ;
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
USING: combinators kernel math ;
|
||||
IN: test
|
||||
|
||||
: (pow) ( f n -- f' )
|
||||
[ dup even? ] [ [ sq ] [ 2 /i ] bi* ] while
|
||||
dup 1 = [ drop ] [ dupd 1 - (pow) * ] if ;
|
||||
|
||||
: pow ( f n -- f' )
|
||||
{
|
||||
{ [ dup 0 < ] [ abs (pow) recip ] }
|
||||
{ [ dup 0 = ] [ 2drop 1 ] }
|
||||
[ (pow) ]
|
||||
} cond ;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
: (pow) ( f n -- f' )
|
||||
[ 1 ] 2dip
|
||||
[ dup 1 = ] [
|
||||
dup even? [ [ sq ] [ 2 /i ] bi* ] [ [ [ * ] keep ] dip 1 - ] if
|
||||
] until
|
||||
drop * ;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
: ** ( n m -- n^m )
|
||||
1 swap 0 ?do over * loop nip ;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
: f**n ( f n -- f^n )
|
||||
dup 0= if
|
||||
drop fdrop 1e
|
||||
else dup 1 and if
|
||||
1- fdup recurse f*
|
||||
else
|
||||
2/ fdup f* recurse
|
||||
then then ;
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
MODULE Exp_Mod
|
||||
IMPLICIT NONE
|
||||
|
||||
INTERFACE OPERATOR (.pow.) ! Using ** instead would overload the standard exponentiation operator
|
||||
MODULE PROCEDURE Intexp, Realexp
|
||||
END INTERFACE
|
||||
|
||||
CONTAINS
|
||||
|
||||
FUNCTION Intexp (base, exponent)
|
||||
INTEGER :: Intexp
|
||||
INTEGER, INTENT(IN) :: base, exponent
|
||||
INTEGER :: i
|
||||
|
||||
IF (exponent < 0) THEN
|
||||
IF (base == 1) THEN
|
||||
Intexp = 1
|
||||
ELSE
|
||||
Intexp = 0
|
||||
END IF
|
||||
RETURN
|
||||
END IF
|
||||
Intexp = 1
|
||||
DO i = 1, exponent
|
||||
Intexp = Intexp * base
|
||||
END DO
|
||||
END FUNCTION IntExp
|
||||
|
||||
FUNCTION Realexp (base, exponent)
|
||||
REAL :: Realexp
|
||||
REAL, INTENT(IN) :: base
|
||||
INTEGER, INTENT(IN) :: exponent
|
||||
INTEGER :: i
|
||||
|
||||
Realexp = 1.0
|
||||
IF (exponent < 0) THEN
|
||||
DO i = exponent, -1
|
||||
Realexp = Realexp / base
|
||||
END DO
|
||||
ELSE
|
||||
DO i = 1, exponent
|
||||
Realexp = Realexp * base
|
||||
END DO
|
||||
END IF
|
||||
END FUNCTION RealExp
|
||||
END MODULE Exp_Mod
|
||||
|
||||
PROGRAM EXAMPLE
|
||||
USE Exp_Mod
|
||||
WRITE(*,*) 2.pow.30, 2.0.pow.30
|
||||
END PROGRAM EXAMPLE
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
' FB 1.05.0
|
||||
|
||||
' Note that 'base' is a keyword in FB, so we use 'base_' instead as a parameter
|
||||
|
||||
Function Pow Overload (base_ As Double, exponent As Integer) As Double
|
||||
If exponent = 0.0 Then Return 1.0
|
||||
If exponent = 1.0 Then Return base_
|
||||
If exponent < 0.0 Then Return 1.0 / Pow(base_, -exponent)
|
||||
Dim power As Double = base_
|
||||
For i As Integer = 2 To exponent
|
||||
power *= base_
|
||||
Next
|
||||
Return power
|
||||
End Function
|
||||
|
||||
Function Pow Overload(base_ As Integer, exponent As Integer) As Double
|
||||
Return Pow(CDbl(base_), exponent)
|
||||
End Function
|
||||
|
||||
' check results of these functions using FB's built in '^' operator
|
||||
Print "Pow(2, 2) = "; Pow(2, 2)
|
||||
Print "Pow(2.5, 2) = "; Pow(2.5, 2)
|
||||
Print "Pow(2, -3) = "; Pow(2, -3)
|
||||
Print "Pow(1.78, 3) = "; Pow(1.78, 3)
|
||||
Print
|
||||
Print "2 ^ 2 = "; 2 ^ 2
|
||||
Print "2.5 ^ 2 = "; 2.5 ^ 2
|
||||
Print "2 ^ -3 = "; 2 ^ -3
|
||||
Print "1.78 ^ 3 = "; 1.78 ^ 3
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
local fn CustomPOW( base as double, exponent as NSInteger ) as double
|
||||
double power = base, result
|
||||
NSUInteger i
|
||||
|
||||
if exponent = 0.0 then result = 1.0 : exit fn
|
||||
if exponent = 1.0 then result = base : exit fn
|
||||
if exponent < 0.0
|
||||
for i = 2 to -exponent
|
||||
power = power * base
|
||||
next
|
||||
result = 1.0/power : exit fn
|
||||
end if
|
||||
for i = 2 to exponent
|
||||
power = power * base
|
||||
next
|
||||
result = power
|
||||
end fn = result
|
||||
|
||||
print "Custom POW function:"
|
||||
print "fn CustomPOW( 2, 2 ) = "; fn CustomPOW( 2, 2 )
|
||||
print "fn CustomPOW( 2.5, 2 ) = "; fn CustomPOW( 2.5, 2 )
|
||||
print "fn CustomPOW( 2, -3 ) = "; fn CustomPOW( 2, -3 )
|
||||
print "fn CustomPOW( 1.78, 3 ) = "; fn CustomPOW( 1.78, 3 )
|
||||
print "fn CustomPOW( 5.5, 5 ) = "; fn CustomPOW( 5.5, 5 )
|
||||
print "fn CustomPOW( 4.5, 2 ) = "; fn CustomPOW( 4.5, 2 )
|
||||
print "fn CustomPOW( -1, -3 ) = "; fn CustomPOW( -1, -3 )
|
||||
print
|
||||
print "Native FB ^ operator:"
|
||||
print "2^2 = "; 2^2
|
||||
print "2.5^2 = "; 2.5^2
|
||||
print "2^-3 = "; 2^-3
|
||||
print "1.78^3 = "; 1.78^3
|
||||
print "5.5^5 = "; 5.5^5
|
||||
print "4.5^2 = "; 4.5^2
|
||||
print "-1^=3 = "; -1^-3
|
||||
print
|
||||
print "Native FB fn POW function:"
|
||||
print "fn POW( 2, 2 ) = "; fn POW( 2, 2 )
|
||||
print "fn POW( 2.5, 2 ) = "; fn POW( 2.5, 2 )
|
||||
print "fn POW( 2, -3 ) = "; fn POW( 2, -3 )
|
||||
print "fn POW( 1.78, 3 ) = "; fn POW( 1.78, 3 )
|
||||
print "fn POW( 5.5, 5 ) = "; fn POW( 5.5, 5 )
|
||||
print "fn POW( 4.5, 2 ) = "; fn POW( 4.5, 2 )
|
||||
print "fn POW( -1, -3 ) = "; fn POW( -1, -3 )
|
||||
print
|
||||
|
||||
HandleEvents
|
||||
20
Task/Exponentiation-operator/GAP/exponentiation-operator.gap
Normal file
20
Task/Exponentiation-operator/GAP/exponentiation-operator.gap
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
expon := function(a, n, one, mul)
|
||||
local p;
|
||||
p := one;
|
||||
while n > 0 do
|
||||
if IsOddInt(n) then
|
||||
p := mul(a, p);
|
||||
fi;
|
||||
a := mul(a, a);
|
||||
n := QuoInt(n, 2);
|
||||
od;
|
||||
return p;
|
||||
end;
|
||||
|
||||
expon(2, 10, 1, \*);
|
||||
# 1024
|
||||
|
||||
# a more creative use of exponentiation
|
||||
List([0 .. 31], n -> (1 - expon(0, n, 1, \-))/2);
|
||||
# [ 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
||||
# 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 ]
|
||||
77
Task/Exponentiation-operator/Go/exponentiation-operator.go
Normal file
77
Task/Exponentiation-operator/Go/exponentiation-operator.go
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func expI(b, p int) (int, error) {
|
||||
if p < 0 {
|
||||
return 0, errors.New("negative power not allowed")
|
||||
}
|
||||
r := 1
|
||||
for i := 1; i <= p; i++ {
|
||||
r *= b
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func expF(b float32, p int) float32 {
|
||||
var neg bool
|
||||
if p < 0 {
|
||||
neg = true
|
||||
p = -p
|
||||
}
|
||||
r := float32(1)
|
||||
for pow := b; p > 0; pow *= pow {
|
||||
if p&1 == 1 {
|
||||
r *= pow
|
||||
}
|
||||
p >>= 1
|
||||
}
|
||||
if neg {
|
||||
r = 1 / r
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func main() {
|
||||
ti := func(b, p int) {
|
||||
fmt.Printf("%d^%d: ", b, p)
|
||||
e, err := expI(b, p)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(e)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("expI tests")
|
||||
ti(2, 10)
|
||||
ti(2, -10)
|
||||
ti(-2, 10)
|
||||
ti(-2, 11)
|
||||
ti(11, 0)
|
||||
|
||||
fmt.Println("overflow undetected")
|
||||
ti(10, 10)
|
||||
|
||||
tf := func(b float32, p int) {
|
||||
fmt.Printf("%g^%d: %g\n", b, p, expF(b, p))
|
||||
}
|
||||
|
||||
fmt.Println("\nexpF tests:")
|
||||
tf(2, 10)
|
||||
tf(2, -10)
|
||||
tf(-2, 10)
|
||||
tf(-2, 11)
|
||||
tf(11, 0)
|
||||
|
||||
fmt.Println("disallowed in expI, allowed here")
|
||||
tf(0, -1)
|
||||
|
||||
fmt.Println("other interesting cases for 32 bit float type")
|
||||
tf(10, 39)
|
||||
tf(10, -39)
|
||||
tf(-10, 39)
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(^) :: (Num a, Integral b) => a -> b -> a
|
||||
_ ^ 0 = 1
|
||||
x ^ n | n > 0 = f x (n-1) x where
|
||||
f _ 0 y = y
|
||||
f a d y = g a d where
|
||||
g b i | even i = g (b*b) (i `quot` 2)
|
||||
| otherwise = f b (i-1) (b*y)
|
||||
_ ^ _ = error "Prelude.^: negative exponent"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(^^) :: (Fractional a, Integral b) => a -> b -> a
|
||||
x ^^ n = if n >= 0 then x^n else recip (x^(negate n))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(**) :: Floating a => a -> a -> a
|
||||
x ** y = exp (log x * y)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
WRITE(Clipboard) pow(5, 3) ! 125
|
||||
WRITE(ClipBoard) pow(5.5, 7) ! 152243.5234
|
||||
|
||||
FUNCTION pow(x, n)
|
||||
pow = 1
|
||||
DO i = 1, n
|
||||
pow = pow * x
|
||||
ENDDO
|
||||
END
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
100 DEF POW(X,Y)
|
||||
110 IF X=0 THEN LET POW=0:EXIT DEF
|
||||
120 LET POW=EXP(Y*LOG(X))
|
||||
130 END DEF
|
||||
140 PRINT POW(PI,3)
|
||||
150 PRINT PI^3
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
procedure main()
|
||||
bases := [5,5.]
|
||||
numbers := [0,2,2.,-1,3]
|
||||
every write("expon(",b := !bases,", ",x := !numbers,")=",(expon(b,x) | "failed") \ 1)
|
||||
end
|
||||
|
||||
procedure expon(base,power)
|
||||
local op,res
|
||||
|
||||
base := numeric(base) | runerror(102,base)
|
||||
power := power = integer(power) | runerr(101,power)
|
||||
|
||||
if power = 0 then return 1
|
||||
else op := if power < 1 then
|
||||
(base := real(base)) & "/" # force real base
|
||||
else "*"
|
||||
|
||||
res := 1
|
||||
every 1 to abs(power) do
|
||||
res := op(res,base)
|
||||
return res
|
||||
end
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
exp =: */@:#~
|
||||
|
||||
10 exp 3
|
||||
1000
|
||||
|
||||
10 exp 0
|
||||
1
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
exp =: *@:] %: */@:(#~|)
|
||||
|
||||
10 exp _3
|
||||
0.001
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
exp =: dyad def 'x *^:y 1'
|
||||
|
||||
10 exp 3
|
||||
1000
|
||||
10 exp _3
|
||||
0.001
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
exp =: ^.^:_1
|
||||
|
||||
81 exp 0.5
|
||||
9
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
exp =: %:^:_1~
|
||||
|
||||
81 exp 0.5
|
||||
9
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
pi =: 3.14159265358979323846
|
||||
e =: 2.71828182845904523536
|
||||
i =: 2 %: _1 NB. Square root of -1
|
||||
|
||||
e^(pi*i)
|
||||
_1
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
exp =: %:^:_1~
|
||||
|
||||
e exp (pi*i)
|
||||
_1
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
public class Exp{
|
||||
public static void main(String[] args){
|
||||
System.out.println(pow(2,30));
|
||||
System.out.println(pow(2.0,30)); //tests
|
||||
System.out.println(pow(2.0,-2));
|
||||
}
|
||||
|
||||
public static double pow(double base, int exp){
|
||||
if(exp < 0) return 1 / pow(base, -exp);
|
||||
double ans = 1.0;
|
||||
for(;exp > 0;--exp) ans *= base;
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
function pow(base, exp) {
|
||||
if (exp != Math.floor(exp))
|
||||
throw "exponent must be an integer";
|
||||
if (exp < 0)
|
||||
return 1 / pow(base, -exp);
|
||||
var ans = 1;
|
||||
while (exp > 0) {
|
||||
ans *= base;
|
||||
exp--;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
15
Task/Exponentiation-operator/Jq/exponentiation-operator-1.jq
Normal file
15
Task/Exponentiation-operator/Jq/exponentiation-operator-1.jq
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# 0^0 => 1
|
||||
# NOTE: jq converts very large integers to floats.
|
||||
# This implementation uses reduce to avoid deep recursion
|
||||
def power_int(n):
|
||||
if n == 0 then 1
|
||||
elif . == 0 then 0
|
||||
elif n < 0 then 1/power_int(-n)
|
||||
elif ((n | floor) == n) then
|
||||
( (n % 2) | if . == 0 then 1 else -1 end ) as $sign
|
||||
| if (. == -1) then $sign
|
||||
elif . < 0 then (( -(.) | power_int(n) ) * $sign)
|
||||
else . as $in | reduce range(1;n) as $i ($in; . * $in)
|
||||
end
|
||||
else error("This is a toy implementation that requires n be integral")
|
||||
end ;
|
||||
13
Task/Exponentiation-operator/Jq/exponentiation-operator-2.jq
Normal file
13
Task/Exponentiation-operator/Jq/exponentiation-operator-2.jq
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
def demo(x;y):
|
||||
x | [ power_int(y), (log*y|exp) ] ;
|
||||
|
||||
demo(2; 3),
|
||||
demo(2; 64),
|
||||
demo(1.1; 1024),
|
||||
demo(1.1; -1024)
|
||||
|
||||
# Output:
|
||||
[8, 7.999999999999998]
|
||||
[18446744073709552000, 18446744073709525000]
|
||||
[2.4328178969536854e+42, 2.4328178969536693e+42]
|
||||
[4.1104597317052596e-43, 4.1104597317052874e-43]
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function pow(base::Number, exp::Integer)
|
||||
r = one(base)
|
||||
for i = 1:exp
|
||||
r *= base
|
||||
end
|
||||
return r
|
||||
end
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
// version 1.0.6
|
||||
|
||||
infix fun Int.ipow(exp: Int): Int =
|
||||
when {
|
||||
this == 1 -> 1
|
||||
this == -1 -> if (exp and 1 == 0) 1 else -1
|
||||
exp < 0 -> throw IllegalArgumentException("invalid exponent")
|
||||
exp == 0 -> 1
|
||||
else -> {
|
||||
var ans = 1
|
||||
var base = this
|
||||
var e = exp
|
||||
while (e > 1) {
|
||||
if (e and 1 == 1) ans *= base
|
||||
e = e shr 1
|
||||
base *= base
|
||||
}
|
||||
ans * base
|
||||
}
|
||||
}
|
||||
|
||||
infix fun Double.dpow(exp: Int): Double {
|
||||
var ans = 1.0
|
||||
var e = exp
|
||||
var base = if (e < 0) 1.0 / this else this
|
||||
if (e < 0) e = -e
|
||||
while (e > 0) {
|
||||
if (e and 1 == 1) ans *= base
|
||||
e = e shr 1
|
||||
base *= base
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println("2 ^ 3 = ${2 ipow 3}")
|
||||
println("1 ^ -10 = ${1 ipow -10}")
|
||||
println("-1 ^ -3 = ${-1 ipow -3}")
|
||||
println()
|
||||
println("2.0 ^ -3 = ${2.0 dpow -3}")
|
||||
println("1.5 ^ 0 = ${1.5 dpow 0}")
|
||||
println("4.5 ^ 2 = ${4.5 dpow 2}")
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
{def ^
|
||||
{def *^
|
||||
{lambda {:base :exponent :acc}
|
||||
{if {= :exponent 0}
|
||||
then :acc
|
||||
else {*^ :base {- :exponent 1} {* :acc :base}}}}}
|
||||
{lambda {:base :exponent}
|
||||
{*^ :base :exponent 1}}}
|
||||
-> ^
|
||||
|
||||
{^ 2 3}
|
||||
-> 8
|
||||
{^ {/ 1 2} 3}
|
||||
-> 0.125 // No rational type as primitives
|
||||
{^ 0.5 3}
|
||||
-> 0.125
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
print " 11^5 = ", floatPow( 11, 5 )
|
||||
print " (-11)^5 = ", floatPow( -11, 5 )
|
||||
print " 11^( -5) = ", floatPow( 11, -5 )
|
||||
print " 3.1416^3 = ", floatPow( 3.1416, 3 )
|
||||
print " 0^2 = ", floatPow( 0, 2 )
|
||||
print " 2^0 = ", floatPow( 2, 0 )
|
||||
print " -2^0 = ", floatPow( -2, 0 )
|
||||
|
||||
end
|
||||
|
||||
function floatPow( a, b)
|
||||
if a <>0 then
|
||||
m =1
|
||||
if b =abs( b) then
|
||||
for n =1 to b
|
||||
m =m *a
|
||||
next n
|
||||
else
|
||||
m =1 /floatPow( a, 0 - b) ' LB has no unitary minus operator.
|
||||
end if
|
||||
else
|
||||
m =0
|
||||
end if
|
||||
floatPow =m
|
||||
end function
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
-- As for built-in power() function:
|
||||
-- base can be either integer or float; returns float.
|
||||
on pow (base, exp)
|
||||
if exp=0 then return 1.0
|
||||
else if exp<0 then
|
||||
exp = -exp
|
||||
base = 1.0/base
|
||||
end if
|
||||
res = float(base)
|
||||
repeat with i = 2 to exp
|
||||
res = res*base
|
||||
end repeat
|
||||
return res
|
||||
end
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
to int_power :n :m
|
||||
if equal? 0 :m [output 1]
|
||||
if equal? 0 modulo :m 2 [output int_power :n*:n :m/2]
|
||||
output :n * int_power :n :m-1
|
||||
end
|
||||
26
Task/Exponentiation-operator/Lua/exponentiation-operator.lua
Normal file
26
Task/Exponentiation-operator/Lua/exponentiation-operator.lua
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
number = {}
|
||||
|
||||
function number.pow( a, b )
|
||||
local ret = 1
|
||||
if b >= 0 then
|
||||
for i = 1, b do
|
||||
ret = ret * a.val
|
||||
end
|
||||
else
|
||||
for i = b, -1 do
|
||||
ret = ret / a.val
|
||||
end
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
function number.New( v )
|
||||
local num = { val = v }
|
||||
local mt = { __pow = number.pow }
|
||||
setmetatable( num, mt )
|
||||
return num
|
||||
end
|
||||
|
||||
x = number.New( 5 )
|
||||
print( x^2 ) --> 25
|
||||
print( number.pow( x, -4 ) ) --> 0.016
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
pow(n,x)
|
||||
k = n fby k div 2;
|
||||
p = x fby p*p;
|
||||
y =1 fby if even(k) then y else y*p;
|
||||
result y asa k eq 0;
|
||||
end
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
Module Exponentiation {
|
||||
\\ a variable can be any type except a string (no $ in name)
|
||||
\\ variable b is long type.
|
||||
\\ by default we pass by value arguments to a function
|
||||
\\ to pass by reference we have to use & before name,
|
||||
\\ in the signature and in the call
|
||||
function pow(a, b as long) {
|
||||
p=a-a ' make p same type as a
|
||||
p++
|
||||
if b>0 then for i=1& to b {p*=a}
|
||||
=p
|
||||
}
|
||||
const fst$="{0::-32} {1}"
|
||||
Document exp$
|
||||
k= pow(11&, 5)
|
||||
exp$=format$(fst$, k, type$(k)="Long")+{
|
||||
}
|
||||
l=pow(11, 5)
|
||||
exp$=format$(fst$, l, type$(l)="Double")+{
|
||||
}
|
||||
m=pow(pi, 3)
|
||||
exp$=format$(fst$, m, type$(m)="Decimal")+{
|
||||
}
|
||||
\\ send to clipboard
|
||||
clipboard exp$
|
||||
\\ send monospaced type text to console using cr char to change lines
|
||||
Print #-2, exp$
|
||||
Rem Report exp$ ' send to console using proportional spacing and justification
|
||||
}
|
||||
Exponentiation
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
define(`power',`ifelse($2,0,1,`eval($1*$0($1,decr($2)))')')
|
||||
power(2,10)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
exponentiation[x_,y_Integer]:=Which[y>0,Times@@ConstantArray[x,y],y==0,1,y<0,1/exponentiation[x,-y]]
|
||||
CirclePlus[x_,y_Integer]:=exponentiation[x,y]
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
exponentiation[1.23,3]
|
||||
exponentiation[4,0]
|
||||
exponentiation[2.5,-2]
|
||||
1.23\[CirclePlus]3
|
||||
4\[CirclePlus]0
|
||||
2.5\[CirclePlus]-2
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
1.86087
|
||||
1
|
||||
0.16
|
||||
1.86087
|
||||
1
|
||||
0.16
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
"^^^"(a, n) := block(
|
||||
[p: 1],
|
||||
while n > 0 do (
|
||||
if oddp(n) then p: p * a,
|
||||
a: a * a,
|
||||
n: quotient(n, 2)
|
||||
),
|
||||
p
|
||||
)$
|
||||
|
||||
infix("^^^")$
|
||||
|
||||
2 ^^^ 10;
|
||||
1024
|
||||
|
||||
2.5 ^^^ 10;
|
||||
9536.7431640625
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
(* Library Interface *)
|
||||
DEFINITION MODULE Exponentiation;
|
||||
|
||||
PROCEDURE IntExp(base, exp : INTEGER) : INTEGER;
|
||||
(* Raises base to the power of exp and returns the result
|
||||
both base and exp must be of type INTEGER *)
|
||||
|
||||
PROCEDURE RealExp(base : REAL; exp : INTEGER) : REAL;
|
||||
(* Raises base to the power of exp and returns the result
|
||||
base must be of type REAL, exp of type INTEGER *)
|
||||
|
||||
END Exponentiation.
|
||||
|
||||
(* Library Implementation *)
|
||||
IMPLEMENTATION MODULE Exponentiation;
|
||||
|
||||
PROCEDURE IntExp(base, exp : INTEGER) : INTEGER;
|
||||
VAR
|
||||
i, res : INTEGER;
|
||||
BEGIN
|
||||
res := 1;
|
||||
FOR i := 1 TO exp DO
|
||||
res := res * base;
|
||||
END;
|
||||
RETURN res;
|
||||
END IntExp;
|
||||
|
||||
PROCEDURE RealExp(base: REAL; exp: INTEGER) : REAL;
|
||||
VAR
|
||||
i : INTEGER;
|
||||
res : REAL;
|
||||
BEGIN
|
||||
res := 1.0;
|
||||
IF exp < 0 THEN
|
||||
FOR i := exp TO -1 DO
|
||||
res := res / base;
|
||||
END;
|
||||
ELSE (* exp >= 0 *)
|
||||
FOR i := 1 TO exp DO
|
||||
res := res * base;
|
||||
END;
|
||||
END;
|
||||
RETURN res;
|
||||
END RealExp;
|
||||
|
||||
END Exponentiation.
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
MODULE Expt EXPORTS Main;
|
||||
|
||||
IMPORT IO, Fmt;
|
||||
|
||||
PROCEDURE IntExpt(arg, exp: INTEGER): INTEGER =
|
||||
VAR result := 1;
|
||||
BEGIN
|
||||
FOR i := 1 TO exp DO
|
||||
result := result * arg;
|
||||
END;
|
||||
RETURN result;
|
||||
END IntExpt;
|
||||
|
||||
PROCEDURE RealExpt(arg: REAL; exp: INTEGER): REAL =
|
||||
VAR result := 1.0;
|
||||
BEGIN
|
||||
IF exp < 0 THEN
|
||||
FOR i := exp TO -1 DO
|
||||
result := result / arg;
|
||||
END;
|
||||
ELSE
|
||||
FOR i := 1 TO exp DO
|
||||
result := result * arg;
|
||||
END;
|
||||
END;
|
||||
RETURN result;
|
||||
END RealExpt;
|
||||
|
||||
BEGIN
|
||||
IO.Put("2 ^ 4 = " & Fmt.Int(IntExpt(2, 4)) & "\n");
|
||||
IO.Put("2.5 ^ 4 = " & Fmt.Real(RealExpt(2.5, 4)) & "\n");
|
||||
END Expt.
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
using System;
|
||||
|
||||
macro @^ (val, pow : int)
|
||||
{
|
||||
<[ Math.Pow($val, $pow) ]>
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
using Nemerle.Assertions;
|
||||
|
||||
module Expon
|
||||
{
|
||||
Expon(val : int, pow : int) : int // demonstrates simple/naive method
|
||||
requires pow > 0 otherwise throw ArgumentOutOfRangeException("Negative powers not allowed, will not return int.")
|
||||
{
|
||||
mutable result = 1;
|
||||
repeat(pow) {
|
||||
result *= val
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
Expon(val : double, pow : int) : double // demonstrates shift and square method
|
||||
{
|
||||
mutable neg = false;
|
||||
mutable p = pow;
|
||||
when (pow < 0) {neg = true; p = -pow};
|
||||
mutable v = val;
|
||||
mutable result = 1d;
|
||||
|
||||
while (p > 0) {
|
||||
when (p & 1 == 1) result *= v;
|
||||
v *= v;
|
||||
p >>= 1;
|
||||
}
|
||||
if (neg) 1d/result else result
|
||||
}
|
||||
|
||||
Main() : void
|
||||
{
|
||||
def eight = 2^3;
|
||||
// def oops = 2^1.5; // compilation error as operator is defined for integer exponentiation
|
||||
def four = Expon(2, 2);
|
||||
def four_d = Expon(2.0, 2);
|
||||
|
||||
WriteLine($"$eight, $four, $four_d");
|
||||
}
|
||||
}
|
||||
23
Task/Exponentiation-operator/Nim/exponentiation-operator.nim
Normal file
23
Task/Exponentiation-operator/Nim/exponentiation-operator.nim
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
proc `^`[T: float|int](base: T; exp: int): T =
|
||||
var (base, exp) = (base, exp)
|
||||
result = 1
|
||||
|
||||
if exp < 0:
|
||||
when T is int:
|
||||
if base * base != 1: return 0
|
||||
elif (exp and 1) == 0: return 1
|
||||
else: return base
|
||||
else:
|
||||
base = 1.0 / base
|
||||
exp = -exp
|
||||
|
||||
while exp != 0:
|
||||
if (exp and 1) != 0:
|
||||
result *= base
|
||||
exp = exp shr 1
|
||||
base *= base
|
||||
|
||||
echo "2^6 = ", 2^6
|
||||
echo "2^-6 = ", 2 ^ -6
|
||||
echo "2.71^6 = ", 2.71^6
|
||||
echo "2.71^-6 = ", 2.71 ^ -6
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
let pow one mul a n =
|
||||
let rec g p x = function
|
||||
| 0 -> x
|
||||
| i ->
|
||||
g (mul p p) (if i mod 2 = 1 then mul p x else x) (i/2)
|
||||
in
|
||||
g a one n
|
||||
;;
|
||||
|
||||
pow 1 ( * ) 2 16;; (* 65536 *)
|
||||
pow 1.0 ( *. ) 2.0 16;; (* 65536. *)
|
||||
|
||||
(* pow is not limited to exponentiation *)
|
||||
pow 0 ( + ) 2 16;; (* 32 *)
|
||||
pow "" ( ^ ) "abc " 10;; (* "abc abc abc abc abc abc abc abc abc abc " *)
|
||||
pow [ ] ( @ ) [ 1; 2 ] 10;; (* [1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2] *)
|
||||
|
||||
(* Thue-Morse sequence *)
|
||||
Array.init 32 (fun n -> (1 - pow 1 ( - ) 0 n) lsr 1);;
|
||||
|
||||
(* [|0; 1; 1; 0; 1; 0; 0; 1; 1; 0; 0; 1; 0; 1; 1; 0;
|
||||
1; 0; 0; 1; 0; 1; 1; 0; 0; 1; 1; 0; 1; 0; 0; 1|]
|
||||
|
||||
See http://en.wikipedia.org/wiki/Thue-Morse_sequence
|
||||
*)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
class Exp {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
Pow(2,30)->PrintLine();
|
||||
Pow(2.0,30)->PrintLine();
|
||||
Pow(2.0,-2)->PrintLine();
|
||||
}
|
||||
|
||||
function : native : Pow(base : Float, exp : Int) ~ Float {
|
||||
if(exp < 0) {
|
||||
return 1 / base->Power(exp * -1.0);
|
||||
};
|
||||
|
||||
ans := 1.0;
|
||||
while(exp > 0) {
|
||||
ans *= base;
|
||||
exp -= 1;
|
||||
};
|
||||
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
: powint(r, n)
|
||||
| i |
|
||||
1 n abs loop: i [ r * ]
|
||||
n isNegative ifTrue: [ inv ] ;
|
||||
|
||||
2 3 powint println
|
||||
2 powint(3) println
|
||||
1.2 4 powint println
|
||||
1.2 powint(4) println
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
ex(a, b)={
|
||||
my(c = 1);
|
||||
while(b > 1,
|
||||
if(b % 2, c *= a);
|
||||
a = a^2;
|
||||
b >>= 1
|
||||
);
|
||||
a * c
|
||||
};
|
||||
|
|
@ -0,0 +1 @@
|
|||
ex2(a, b) = a ^ b;
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
declare exp generic
|
||||
(iexp when (fixed, fixed),
|
||||
fexp when (float, fixed) );
|
||||
iexp: procedure (m, n) returns (fixed binary (31));
|
||||
declare (m, n) fixed binary (31) nonassignable;
|
||||
declare exp fixed binary (31) initial (m), i fixed binary;
|
||||
if m = 0 & n = 0 then signal error;
|
||||
if n = 0 then return (1);
|
||||
do i = 2 to n;
|
||||
exp = exp * m;
|
||||
end;
|
||||
return (exp);
|
||||
end iexp;
|
||||
fexp: procedure (a, n) returns (float (15));
|
||||
declare (a float, n fixed binary (31)) nonassignable;
|
||||
declare exp float initial (a), i fixed binary;
|
||||
if a = 0 & n = 0 then signal error;
|
||||
if n = 0 then return (1);
|
||||
do i = 2 to n;
|
||||
exp = exp * a;
|
||||
end;
|
||||
return (exp);
|
||||
end fexp;
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
Program ExponentiationOperator(output);
|
||||
|
||||
function intexp (base, exponent: integer): longint;
|
||||
var
|
||||
i: integer;
|
||||
|
||||
begin
|
||||
if (exponent < 0) then
|
||||
if (base = 1) then
|
||||
intexp := 1
|
||||
else
|
||||
intexp := 0
|
||||
else
|
||||
begin
|
||||
intexp := 1;
|
||||
for i := 1 to exponent do
|
||||
intexp := intexp * base;
|
||||
end;
|
||||
end;
|
||||
|
||||
function realexp (base: real; exponent: integer): real;
|
||||
var
|
||||
i: integer;
|
||||
|
||||
begin
|
||||
realexp := 1.0;
|
||||
if (exponent < 0) then
|
||||
for i := exponent to -1 do
|
||||
realexp := realexp / base
|
||||
else
|
||||
for i := 1 to exponent do
|
||||
realexp := realexp * base;
|
||||
end;
|
||||
|
||||
begin
|
||||
writeln('2^30: ', intexp(2, 30));
|
||||
writeln('2.0^30: ', realexp(2.0, 30));
|
||||
end.
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#!/usr/bin/perl -w
|
||||
use strict ;
|
||||
|
||||
sub expon {
|
||||
my ( $base , $expo ) = @_ ;
|
||||
if ( $expo == 0 ) {
|
||||
return 1 ;
|
||||
}
|
||||
elsif ( $expo == 1 ) {
|
||||
return $base ;
|
||||
}
|
||||
elsif ( $expo > 1 ) {
|
||||
my $prod = 1 ;
|
||||
foreach my $n ( 0..($expo - 1) ) {
|
||||
$prod *= $base ;
|
||||
}
|
||||
return $prod ;
|
||||
}
|
||||
elsif ( $expo < 0 ) {
|
||||
return 1 / ( expon ( $base , -$expo ) ) ;
|
||||
}
|
||||
}
|
||||
print "3 to the power of 10 as a function is " . expon( 3 , 10 ) . " !\n" ;
|
||||
print "3 to the power of 10 as a builtin is " . 3**10 . " !\n" ;
|
||||
print "5.5 to the power of -3 as a function is " . expon( 5.5 , -3 ) . " !\n" ;
|
||||
print "5.5 to the power of -3 as a builtin is " . 5.5**-3 . " !\n" ;
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
sub ex {
|
||||
my($base,$exp) = @_;
|
||||
die "Exponent '$exp' must be an integer!" if $exp != int($exp);
|
||||
return 1 if $exp == 0;
|
||||
($base, $exp) = (1/$base, -$exp) if $exp < 0;
|
||||
my $c = 1;
|
||||
while ($exp > 1) {
|
||||
$c *= $base if $exp % 2;
|
||||
$base *= $base;
|
||||
$exp >>= 1;
|
||||
}
|
||||
$base * $c;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">powir</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">i</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">b</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">b</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">b</span>
|
||||
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">powir</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
(de ** (X N) # N th power of X
|
||||
(if (ge0 N)
|
||||
(let Y 1
|
||||
(loop
|
||||
(when (bit? 1 N)
|
||||
(setq Y (* Y X)) )
|
||||
(T (=0 (setq N (>> 1 N)))
|
||||
Y )
|
||||
(setq X (* X X)) ) )
|
||||
0 ) )
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
function pow($a, [int]$b) {
|
||||
if ($b -eq -1) { return 1/$a }
|
||||
if ($b -eq 0) { return 1 }
|
||||
if ($b -eq 1) { return $a }
|
||||
if ($b -lt 0) {
|
||||
$rec = $true # reciprocal needed
|
||||
$b = -$b
|
||||
}
|
||||
|
||||
$result = $a
|
||||
2..$b | ForEach-Object {
|
||||
$result *= $a
|
||||
}
|
||||
|
||||
if ($rec) {
|
||||
return 1/$result
|
||||
} else {
|
||||
return $result
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
:- arithmetic_function((^^)/2).
|
||||
:- op(200, xfy, user:(^^)).
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue