From 7d456536214a9de50169645e16072a7886db5004 Mon Sep 17 00:00:00 2001 From: LTLA Date: Thu, 15 Aug 2024 10:58:18 -0700 Subject: [PATCH] Protect against potential floating-point overflows when determining beta. 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. --- include/qdtsne/gaussian.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/qdtsne/gaussian.hpp b/include/qdtsne/gaussian.hpp index edd6608..3218635 100644 --- a/include/qdtsne/gaussian.hpp +++ b/include/qdtsne/gaussian.hpp @@ -117,11 +117,11 @@ void compute_gaussian_perplexity(NeighborList& neighbors, Float_ if (max_beta == max_value) { beta *= static_cast(2); } else { - beta = (beta + max_beta) / static_cast(2); + beta += (max_beta - beta) / static_cast(2); // i.e., midpoint that avoids problems with potential overflow. } } else { max_beta = beta; - beta = (beta + min_beta) / static_cast(2); + beta += (min_beta - beta) / static_cast(2); // i.e., midpoint that avoids problems with potential overflow. } }