From b84f8b2e49d63920b0c19818b28d4c681c359be7 Mon Sep 17 00:00:00 2001 From: owrior Date: Wed, 25 Oct 2023 10:54:25 +0100 Subject: [PATCH] Reduce number of magnitude calculations. --- crates/polars-ops/src/series/ops/round.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/polars-ops/src/series/ops/round.rs b/crates/polars-ops/src/series/ops/round.rs index 48b7a5f9bce81..ca71817a29d49 100644 --- a/crates/polars-ops/src/series/ops/round.rs +++ b/crates/polars-ops/src/series/ops/round.rs @@ -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)]