Skip to content

Conversation

@tisonkun
Copy link
Member

Signed-off-by: tison <wander4096@gmail.com>
Comment on lines +1254 to +1257
self.weight = self
.weight
.checked_add(other.weight.get())
.expect("weight overflow");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of silently saturating_add, panic if overflow.

This should never happen with real inputs, and let it crash should be better than silently holding inaccurate values.

Comment on lines +1261 to +1267
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)
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If other_mean - self_mean doesn't overflow/underflow, save one mul + one div to reduce precision loss.

This can overflow/underflow, so we fallback to conservative weighted average computed in that case. In both cases, use mul_add to best effort reduce precision loss.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the internal Centroid::add logic in the T-Digest implementation to more robustly combine centroids, particularly around numerical edge cases during mean updates.

Changes:

  • Switch centroid weight accumulation from saturating addition to checked addition with an explicit overflow expectation.
  • Update centroid mean computation to use a numerically stable formulation (self_mean + (other_mean - self_mean) * ratio_other) with a fallback when the subtraction overflows to non-finite.
  • Tighten centroid mean invariant checking from “not NaN” to “finite”.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tisonkun tisonkun merged commit 2fc50ae into main Feb 11, 2026
15 checks passed
@tisonkun tisonkun deleted the floating-cal branch February 11, 2026 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant