mirror of
https://github.com/storytold/gridcraft.git
synced 2026-12-01 18:26:05 -06:00
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.
This commit is contained in:
@@ -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::<f64>();
|
||||
let mean_y = nums.iter().map(|y| y * down).sum::<f64>() / n;
|
||||
let mut num = 0.0;
|
||||
let mut den = 0.0;
|
||||
for (i, y) in nums.iter().enumerate() {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user