Skip to content

Commit

Permalink
Reduce number of magnitude calculations.
Browse files Browse the repository at this point in the history
  • Loading branch information
owrior committed Oct 25, 2023
1 parent 4ba21a4 commit b84f8b2
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions crates/polars-ops/src/series/ops/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,12 @@ pub trait RoundSeries: SeriesSealed {

impl RoundSeries for Series {}

fn get_magnitude(value: f64, significant_figures: u32) -> f64 {
10.0.pow(significant_figures as f64 - 1.0 - ((value).log10().floor()))
}
fn round_sf(value: f64, significant_figures: u32) -> f64 {
if value == 0.0 {
return value;
}
(value * get_magnitude(value.abs(), significant_figures)).round()
/ get_magnitude(value.abs(), significant_figures)
let magnitiude = 10.0.pow(significant_figures as f64 - 1.0 - ((value.abs()).log10().floor()));
(value * magnitiude).round() / magnitiude
}

#[cfg(test)]
Expand Down

0 comments on commit b84f8b2

Please sign in to comment.