This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,3 @@
: pow ( f n -- f' )
dup 0 < [ abs pow recip ]
[ [ 1 ] 2dip swap [ * ] curry times ] if ;

View file

@ -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 ;

View file

@ -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 ;

View file

@ -0,0 +1,6 @@
: (pow) ( f n -- f' )
[ 1 ] 2dip
[ dup 1 = ] [
dup even? [ [ sq ] [ 2 /i ] bi* ] [ [ [ * ] keep ] dip 1 - ] if
] until
drop * ;

View 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 ]

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,7 @@
exp =: */@:#~
10 exp 3
1000
10 exp 0
1

View file

@ -0,0 +1,4 @@
exp =: *@:] %: */@:(#~|)
10 exp _3
0.001

View file

@ -0,0 +1,6 @@
exp =: dyad def 'x *^:y 1'
10 exp 3
1000
10 exp _3
0.001

View file

@ -0,0 +1,4 @@
exp =: ^.^:_1
81 exp 0.5
9

View file

@ -0,0 +1,4 @@
exp =: %:^:_1~
81 exp 0.5
9

View file

@ -0,0 +1,6 @@
pi =: 3.14159265358979323846
e =: 2.71828182845904523536
i =: 2 %: _1 NB. Square root of -1
e^(pi*i)
_1

View file

@ -0,0 +1,4 @@
exp =: %:^:_1~
e exp (pi*i)
_1

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,2 @@
define(`power',`ifelse($2,0,1,`eval($1*$0($1,decr($2)))')')
power(2,10)

View file

@ -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]

View file

@ -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

View file

@ -0,0 +1,6 @@
1.86087
1
0.16
1.86087
1
0.16

View file

@ -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

View file

@ -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.

View file

@ -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.