diff --git a/crates/engine/src/fill.rs b/crates/engine/src/fill.rs index 4fdd5b9b9e..2f1a98a371 100644 --- a/crates/engine/src/fill.rs +++ b/crates/engine/src/fill.rs @@ -528,20 +528,27 @@ impl Pattern { } } +/// 2^600 and 2^-600, both exact. +const TWO_POW_600: f64 = f64::from_bits((1023 + 600) << 52); +const TWO_POW_MINUS_600: f64 = f64::from_bits((1023 - 600) << 52); + /// The least-squares line through `nums` at positions 0, 1, 2…: its values at the first and last -/// positions, and its step. A lone number gives a step of zero. +/// positions, and its step. A lone number gives a step of zero. When a number is larger than 2^600, +/// the fit runs on the numbers multiplied by 2^-600 and its results are multiplied by 2^600, so the +/// sums stay finite for numbers up to the largest double. 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 / n).sum::(); + let mean_y = nums.iter().map(|y| y * down / n).sum::(); let mut num = 0.0; let mut den = 0.0; for (i, y) in nums.iter().enumerate() { - num += (i as f64 - mean_x) * (y - mean_y); + num += (i as f64 - mean_x) * (y * down - mean_y); den += (i as f64 - mean_x).powi(2); } let step = if den == 0.0 { 0.0 } else { num / den }; - (mean_y - step * mean_x, mean_y + step * (n - 1.0 - mean_x), step) + ((mean_y - step * mean_x) * up, (mean_y + step * (n - 1.0 - mean_x)) * up, step * up) } /// Whether `serial` falls on Monday to Friday; `None` outside the supported date range. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 3bcf559e49..7e99df9643 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1052,10 +1052,15 @@ fn linear_and_growth_trend_continue_the_fitted_line_or_curve() { fill(&mut s, "S1:S2", "S1:S6", "growthTrend").unwrap(); let Value::Number(s6) = v(&s, "S6") else { panic!("S6 is {:?}", v(&s, "S6")) }; assert!((s6 / 1e300 - 1.0).abs() < 1e-9, "S6 = {s6}"); - // Two numbers near the largest double repeat without their sum overflowing. - s.execute("range.setValues", json!({"range": "T1", "values": [[1e308], [1e308]]})).unwrap(); + // Numbers near the largest double continue their line without a sum overflowing: two equal + // numbers repeat, and 1e308 four times then 0 fit a step of -2e307 and continue at 2e307. + s.execute("range.setValues", json!({"range": "T1", "values": [[1e308, 1e308], [1e308, 1e308], [0, 1e308], [0, 1e308], [0, 0]]})).unwrap(); fill(&mut s, "T1:T2", "T1:T3", "linearTrend").unwrap(); - assert_eq!(v(&s, "T3"), Value::Number(1e308)); + fill(&mut s, "U1:U5", "U1:U6", "linearTrend").unwrap(); + for (cell, want) in [("T3", 1e308), ("U6", 2e307)] { + let Value::Number(got) = v(&s, cell) else { panic!("{cell} is {:?}", v(&s, cell)) }; + assert!((got / want - 1.0).abs() < 1e-9, "{cell} = {got}"); + } // A lone number repeats, and dates a month apart step by their days, not by months. s.execute("cell.set", json!({"cell": "M1", "input": "7"})).unwrap(); fill(&mut s, "M1", "M1:M2", "linearTrend").unwrap();