This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,19 @@
% -spec attribute is for documentation and dialyzer static analysis purposes only.
% It does not constrain the function like a guard (when ... ).
-spec exp_int(X :: integer(), N :: non_neg_integer()) -> integer().
% X ^ 0
exp_int(X, 0) when is_integer(X) -> % is_integer guard is required, otherwise X would match anything.
1;
% X ^ odd
exp_int(X, N) when is_integer(X), N >= 1, N rem 2 == 1 ->
Part = exp_int(X, (N-1) div 2),
X * Part * Part;
% X ^ even
exp_int(X, N) when is_integer(X), N >= 2, N rem 2 == 0 ->
Part = exp_int(X, N div 2),
Part * Part.
% X ^ negative is excluded because it would return float.

View file

@ -0,0 +1,21 @@
% -spec attribute is for documentation and dialyzer static analysis purposes only.
% It does not constrain the function like a guard (when ... ).
-spec exp_float(X :: float(), N :: integer()) -> float().
% X ^ 0
exp_float(X, 0) when is_float(X) -> % is_float guard is required, otherwise X would match anything.
1.0;
% X ^ negative
exp_float(X, N) when is_float(X), N < 0 ->
1.0 / exp_float(X, -N);
% X ^ even
exp_float(X, N) when is_float(X), N >= 1, N rem 2 == 1 ->
Part = exp_float(X, (N-1) div 2),
X * Part * Part;
% X ^ odd
exp_float(X, N) when is_float(X), N >= 2, N rem 2 == 0 ->
Part = exp_float(X, N div 2),
Part * Part.