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();