From 210b8c9cd68973bdfaa8f3423ef577da5ee684d9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 30 Oct 2026 21:12:21 -0700 Subject: [PATCH] Fill handle: a linear fit of very small numbers keeps them The fitted line sums the numbers before dividing by their count, so the smallest doubles no longer round to zero one by one; three cells of 5e-324 continue at 5e-324 instead of 0. --- crates/engine/src/fill.rs | 2 +- crates/engine/src/tests.rs | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/fill.rs b/crates/engine/src/fill.rs index ab2d901bb..2b967a3b6 100644 --- a/crates/engine/src/fill.rs +++ b/crates/engine/src/fill.rs @@ -546,7 +546,7 @@ fn least_squares(nums: &[f64]) -> (f64, f64, f64) { let (down, up) = if nums.iter().any(|y| y.abs() > TWO_POW_600) { (TWO_POW_MINUS_600, TWO_POW_600) } else { (1.0, 1.0) }; let n = nums.len() as f64; let mean_x = (n - 1.0) / 2.0; - let mean_y = nums.iter().map(|y| y * down / n).sum::(); + let mean_y = nums.iter().map(|y| y * down).sum::() / n; let mut num = 0.0; let mut den = 0.0; for (i, y) in nums.iter().enumerate() { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 160f48693..e6514ac7a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1070,6 +1070,11 @@ fn linear_and_growth_trend_continue_the_fitted_line_or_curve() { fill(&mut s, "V1", "V1:V3", "growthTrend").unwrap(); fill(&mut s, "W1", "W1:W3", "linearTrend").unwrap(); assert_eq!(nums(&s, &["V2", "V3", "W2", "W3"]), n(&[1e308, 1e308, 0.12345678901234567, 0.12345678901234567])); + // The smallest double three times repeats rather than averaging to zero. + let tiny = f64::from_bits(1); + s.execute("range.setValues", json!({"range": "X1", "values": [[tiny], [tiny], [tiny]]})).unwrap(); + fill(&mut s, "X1:X3", "X1:X4", "linearTrend").unwrap(); + assert_eq!(v(&s, "X4"), Value::Number(tiny)); s.execute("cell.set", json!({"cell": "N1", "input": "1/15/2026"})).unwrap(); s.execute("cell.set", json!({"cell": "N2", "input": "2/15/2026"})).unwrap(); fill(&mut s, "N1:N2", "N1:N3", "linearTrend").unwrap();