Skip to content

Commit

Permalink
Protect against potential floating-point overflows when determining b…
Browse files Browse the repository at this point in the history
…eta.

Otherwise, the numerator of the midpoint calculation in the binary
search could overflow, even if the value of the midpoint itself was
fine. Admittedly, this should not be possible as the operands should be
well below the float limits, but better safe than sorry.
  • Loading branch information
LTLA committed Aug 15, 2024
1 parent 2c718cd commit 7d45653
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/qdtsne/gaussian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ void compute_gaussian_perplexity(NeighborList<Index_, Float_>& neighbors, Float_
if (max_beta == max_value) {
beta *= static_cast<Float_>(2);
} else {
beta = (beta + max_beta) / static_cast<Float_>(2);
beta += (max_beta - beta) / static_cast<Float_>(2); // i.e., midpoint that avoids problems with potential overflow.
}
} else {
max_beta = beta;
beta = (beta + min_beta) / static_cast<Float_>(2);
beta += (min_beta - beta) / static_cast<Float_>(2); // i.e., midpoint that avoids problems with potential overflow.
}
}

Expand Down

0 comments on commit 7d45653

Please sign in to comment.