Skip to content

Commit de9b539

Browse files
committed
Fix floating point quality comparison
1 parent ed46820 commit de9b539

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

crypto/block/block.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,17 +1713,18 @@ bool MtCarloComputeShare::compute() {
17131713
}
17141714

17151715
void MtCarloComputeShare::gen_vset() {
1716-
double total_wt = 1.;
1716+
constexpr int precision = 1000000;
1717+
int total_wt = precision;
17171718
int hc = 0;
17181719
for (int i = 0; i < K; i++) {
17191720
CHECK(total_wt > 0);
1720-
double inv_wt = 1. / total_wt;
1721+
double inv_wt = (double)precision / total_wt;
17211722
R0 += inv_wt;
17221723
for (int j = 0; j < i; j++) {
17231724
RW[A[j]] -= inv_wt;
17241725
}
17251726
// double p = drand48() * total_wt;
1726-
double p = (double)td::Random::fast_uint64() * total_wt / (1. * (1LL << 32) * (1LL << 32));
1727+
double p = (double)td::Random::fast_uint64() / precision * total_wt / (1. * (1LL << 32) * (1LL << 32));
17271728
for (int h = 0; h < hc; h++) {
17281729
if (p < H[h].first) {
17291730
break;
@@ -1740,8 +1741,9 @@ void MtCarloComputeShare::gen_vset() {
17401741
}
17411742
}
17421743
CHECK(a >= 0 && a < N);
1743-
CHECK(total_wt >= W[a]);
1744-
total_wt -= W[a];
1744+
const int Wa = static_cast<int>(W[a] * precision);
1745+
CHECK(total_wt >= Wa);
1746+
total_wt -= Wa;
17451747
double x = CW[a];
17461748
c = hc++;
17471749
while (c > 0 && H[c - 1].first > x) {

0 commit comments

Comments
 (0)