From 6529f11a3f7a8b5b4dcee2e9312ea50c45e9a50d Mon Sep 17 00:00:00 2001 From: gord chung <5091603+chungg@users.noreply.github.com> Date: Fri, 20 Sep 2024 08:36:08 -0400 Subject: [PATCH] fix trima smoothing function (#105) wrong operator that just so happened to produce expected results for test cases. --- src/smooth.rs | 2 +- tests/ma_test.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/smooth.rs b/src/smooth.rs index a67ef6b..e0929a6 100644 --- a/src/smooth.rs +++ b/src/smooth.rs @@ -430,7 +430,7 @@ pub fn lrf(data: &[T], window: usize) -> impl Iterator(data: &[T], window: usize) -> impl Iterator + '_ { let win1 = window.div_ceil(2); - let win2 = if window & 2 == 0 { win1 + 1 } else { win1 }; + let win2 = if window % 2 == 0 { win1 + 1 } else { win1 }; sma(&sma(data, win1).collect::>(), win2) .collect::>() .into_iter() diff --git a/tests/ma_test.rs b/tests/ma_test.rs index 845bf18..dc2418e 100644 --- a/tests/ma_test.rs +++ b/tests/ma_test.rs @@ -983,6 +983,34 @@ fn test_trima_even() { ], &result )); + let result = smooth::trima(&stats.close, 14).collect::>(); + assert_eq!(stats.close.len(), result.len()); + assert_eq!( + &[ + 49.236071927206865, + 48.14642906188965, + 46.93589333125524, + 45.86107185908727, + 44.97500031335014, + 44.23750019073486, + 43.55553579330444, + 42.89839288166591, + 42.35678570611136, + 41.95571429388863, + 41.68125002724784, + 41.63321440560477, + 41.791250228881836, + 42.07142884390695, + 42.441786016736714, + 42.94375031335014, + 43.4982145854405, + 44.100357396262034, + 44.75000020435878, + 45.37928588049752, + 46.01383944920131 + ], + &result[14 - 1..] + ); } #[test]