Skip to content

Commit

Permalink
factor in _cumulate should be float
Browse files Browse the repository at this point in the history
  • Loading branch information
wallin committed Dec 30, 2015
1 parent b5bff32 commit d4cd28c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/tdigest/tdigest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ def self.from_json(array)

private


def _add_weight(nearest, x, n)
unless x == nearest.mean
nearest.mean += n * (x - nearest.mean) / (nearest.n + n)
Expand All @@ -206,11 +205,8 @@ def _add_weight(nearest, x, n)
end

def _cumulate(exact = false)
factor = @last_cumulate == 0 ? Float::INFINITY : (@n / @last_cumulate)
if @n == @last_cumulate ||
!exact && @cx && @cx > (factor)
return
end
factor = @last_cumulate == 0 ? Float::INFINITY : (@n.to_f / @last_cumulate)
return if @n == @last_cumulate || (!exact && @cx && @cx > (factor))

cumn = 0
@centroids.each do |_, c|
Expand Down
28 changes: 28 additions & 0 deletions test/tdigest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,32 @@ def test_that_it_has_a_version_number
end
end
end

describe '#push' do
it "calls _cumulate so won't crash because of uninitialized mean_cumn" do
td = TDigest::TDigest.new
td.push [125000000.0,
104166666.66666666,
135416666.66666666,
104166666.66666666,
104166666.66666666,
93750000.0,
125000000.0,
62500000.0,
114583333.33333333,
156250000.0,
124909090.90909092,
104090909.0909091,
135318181.81818184,
104090909.0909091,
104090909.0909091,
93681818.18181819,
124909090.90909092,
62454545.45454546,
114500000.00000001,
156136363.63636366,
123567567.56756756,
102972972.97297296]
end
end
end

0 comments on commit d4cd28c

Please sign in to comment.