You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by ti...@apache.org on 2026/02/10 13:17:11 UTC
(datasketches-rust) 01/01: chore(tdigest): better handle Centroid adds
This is an automated email from the ASF dual-hosted git repository.
tison pushed a commit to branch floating-cal
in repository https://gitbox.apache.org/repos/asf/datasketches-rust.git
commit d77af8aca98e639965daa24dd54f3220b0f0d599
Author: tison <wa...@gmail.com>
AuthorDate: Tue Feb 10 21:17:00 2026 +0800
chore(tdigest): better handle Centroid adds
Signed-off-by: tison <wa...@gmail.com>
---
datasketches/src/tdigest/sketch.rs | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/datasketches/src/tdigest/sketch.rs b/datasketches/src/tdigest/sketch.rs
index a790892..a9ef093 100644
--- a/datasketches/src/tdigest/sketch.rs
+++ b/datasketches/src/tdigest/sketch.rs
@@ -1251,15 +1251,24 @@ impl Centroid {
fn add(&mut self, other: Centroid) {
let (self_weight, other_weight) = (self.weight(), other.weight());
let total_weight = self_weight + other_weight;
- self.weight = self.weight.saturating_add(other.weight.get());
+ self.weight = self
+ .weight
+ .checked_add(other.weight.get())
+ .expect("weight overflow");
let (self_mean, other_mean) = (self.mean, other.mean);
- let ratio_self = self_weight / total_weight;
let ratio_other = other_weight / total_weight;
- self.mean = self_mean.mul_add(ratio_self, other_mean * ratio_other);
+ let delta = other_mean - self_mean;
+ self.mean = if delta.is_finite() {
+ delta.mul_add(ratio_other, self_mean)
+ } else {
+ let ratio_self = self_weight / total_weight;
+ self_mean.mul_add(ratio_self, other_mean * ratio_other)
+ };
+
debug_assert!(
- !self.mean.is_nan(),
- "NaN values should never be present in centroids; self: {}, other: {}",
+ self.mean.is_finite(),
+ "Centroid's mean must be finite; self: {}, other: {}",
self_mean,
other_mean
);
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org