Skip to content

Commit

Permalink
round
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Aug 7, 2023
1 parent 3c2a03f commit d18dec1
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ta_lib/core/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ impl Series<f64> {
self.fmap(|val| val.map(|v| v.abs()))
}

pub fn round(&self, places: u32) -> Self {
let multiplier = 10f64.powi(places as i32);
self.fmap(|val| val.map(|v| (v * multiplier).round() / multiplier))
}

pub fn cumsum(&self) -> Self {
let mut sum = 0.0;

Expand Down Expand Up @@ -233,6 +238,16 @@ mod tests {
assert_eq!(result, expected);
}

#[test]
fn test_round() {
let source = Series::from([-1.2211, 2.2456, -3.5677, 4.0, 5.3334]);
let expected = Series::from([-1.0, 2.0, -4.0, 4.0, 5.0]);

let result = source.round(0);

assert_eq!(result, expected);
}

#[test]
fn test_cumsum() {
let source = vec![1.0, 2.0, 3.0, 4.0, 5.0];
Expand Down

0 comments on commit d18dec1

Please sign in to comment.