Skip to content

Commit

Permalink
handle div/0
Browse files Browse the repository at this point in the history
  • Loading branch information
kjnilsson committed Feb 1, 2018
1 parent cf00001 commit e606666
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/aten_detect.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ failure_prob_at(At, #state{freshness = F,
factor = A,
samples = Samples}) ->
T = At - F,
{TotNum, SmallNum} = array:foldl(fun(_, undefined, Acc) -> Acc;
{TotNum, SmallNum} = array:foldl(fun(_, undefined, Acc) ->
Acc;
(_, S, {Tot, Smaller}) when S * A =< T ->
{Tot+1, Smaller+1};
(_, _S, {Tot, Smaller}) ->
{Tot+1, Smaller}
end, {0, 0}, Samples),
SmallNum / TotNum.
SmallNum / max(1, TotNum). % avoid div/0

ts() ->
% TODO: should we use erlang monotonic time instead?
Expand All @@ -70,6 +71,7 @@ ts() ->

detect_test() ->
S0 = #state{},
?assert(failure_prob_at(10, S0) =:= 0.0),
S = lists:foldl(fun append/2, S0, [1, 5, 4, 10, 13, 20, 25]),
?assert(failure_prob_at(28, S) < 0.5),
?assert(failure_prob_at(40, S) == 1.0),
Expand Down

0 comments on commit e606666

Please sign in to comment.