Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions cc/algorithms/distributions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,11 @@ int64_t GeometricDistribution::Sample() { return Sample(1.0); }

const std::vector<double>& GeometricDistribution::GetProbs(double lambda) {
if (probs_.empty() || lambda != cached_lambda_) {
probs_.clear();
probs_.reserve(63);
probs_.resize(63);
for (int i = 0; i < 63; ++i) {
double c = lambda * (1LL << i);
if (c > 100) {
// If c is large, exp(c) is huge and 1 / (exp(c) + 1) is close to 0, no
// need to compute them.
break;
} else {
probs_.push_back(1.0 / (std::exp(c) + 1));
}
// If c is large, exp(c) is huge and 1 / (exp(c) + 1) is close to 0.
probs_[i] = c < 100 ? 1.0 / (std::exp(c) + 1) : 0.0;
}
cached_lambda_ = lambda;
}
Expand Down Expand Up @@ -235,7 +229,7 @@ int64_t GeometricDistribution::Sample(double scale) {
const std::vector<double>& probs = GetProbs(lambda);

int64_t result = 0;
for (int i = probs.size() - 1; i >= 0; --i) {
for (int i = 62; i >= 0; --i) {
if (GetUniformDouble() < probs[i]) {
result |= (1LL << i);
}
Expand Down
Loading