This is an automated email from the ASF dual-hosted git repository.

tisonkun pushed a commit to branch codex/tdigest-p0-fixes
in repository https://gitbox.apache.org/repos/asf/datasketches-rust.git

commit f7dab30f2a6d085e7e8c4b8a8f80e278217aa64d
Author: tison <[email protected]>
AuthorDate: Wed Jul 29 14:49:38 2026 +0800

    fix(tdigest): keep extreme finite quantiles finite
    
    Signed-off-by: tison <[email protected]>
---
 datasketches/src/tdigest/sketch.rs        |  5 +++--
 datasketches/tests/tdigest_test/sketch.rs | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/datasketches/src/tdigest/sketch.rs 
b/datasketches/src/tdigest/sketch.rs
index 765ac13..7b39c3a 100644
--- a/datasketches/src/tdigest/sketch.rs
+++ b/datasketches/src/tdigest/sketch.rs
@@ -1347,6 +1347,7 @@ mod scale_function {
     }
 }
 
-const fn weighted_average(x1: f64, w1: f64, x2: f64, w2: f64) -> f64 {
-    (x1 * w1 + x2 * w2) / (w1 + w2)
+fn weighted_average(x1: f64, w1: f64, x2: f64, w2: f64) -> f64 {
+    let total_weight = w1 + w2;
+    x1 * (w1 / total_weight) + x2 * (w2 / total_weight)
 }
diff --git a/datasketches/tests/tdigest_test/sketch.rs 
b/datasketches/tests/tdigest_test/sketch.rs
index b61d779..13ca646 100644
--- a/datasketches/tests/tdigest_test/sketch.rs
+++ b/datasketches/tests/tdigest_test/sketch.rs
@@ -229,6 +229,25 @@ fn test_invalid_inputs() {
     assert!(td.is_empty());
 }
 
+#[test]
+fn test_extreme_values_produce_finite_quantiles() {
+    let mut tdigest = TDigestMut::default();
+    for i in 0..10_000 {
+        tdigest.update(if i % 2 == 0 { f64::MAX } else { -f64::MAX });
+    }
+
+    assert_eq!(tdigest.total_weight(), 10_000);
+    assert_eq!(tdigest.min_value(), Some(-f64::MAX));
+    assert_eq!(tdigest.max_value(), Some(f64::MAX));
+    for rank in [0.25, 0.5, 0.75] {
+        let quantile = tdigest.quantile(rank).unwrap();
+        assert!(
+            quantile.is_finite(),
+            "quantile at rank {rank} must be finite, got {quantile}"
+        );
+    }
+}
+
 #[test]
 fn test_estimate_repeat_values() {
     let mut tdigest = TDigestMut::default();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to