From 62282d86e63994b176ddb4e08e7ba7b134bcacc1 Mon Sep 17 00:00:00 2001 From: owrior Date: Tue, 7 Nov 2023 14:08:04 +0000 Subject: [PATCH] Match type. --- crates/polars-ops/src/series/ops/round.rs | 46 ++++++----------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/crates/polars-ops/src/series/ops/round.rs b/crates/polars-ops/src/series/ops/round.rs index cc8a4ac459b7..1d3749d0debd 100644 --- a/crates/polars-ops/src/series/ops/round.rs +++ b/crates/polars-ops/src/series/ops/round.rs @@ -1,5 +1,6 @@ use num_traits::pow::Pow; use polars_core::prelude::*; +use polars_core::with_match_physical_numeric_polars_type; use crate::series::ops::SeriesSealed; @@ -45,33 +46,18 @@ pub trait RoundSeries: SeriesSealed { ) }; - if let Ok(ca) = s.f64() { - let s = ca - .apply_values(|val| round_sig_figs(val, digits)) - .into_series(); - return Ok(s); - } - // Note we do the computation on f64 floats to not lose precision - // when the computation is done we cast back - if let Ok(ca) = s.f32() { - let s = ca - .apply_values(|val| round_sig_figs(val as f64, digits) as f32) - .into_series(); - return Ok(s); - } - if let Ok(ca) = s.i32() { - let s = ca - .apply_values(|val| round_sig_figs(val as f64, digits) as i32) - .into_series(); + with_match_physical_numeric_polars_type!(s.dtype(), |$T| { + let s = s.cast(&DataType::Float64)?; + let ca = s.f64()?; + let s = ca.apply_values(|value| { + if value == 0.0 { + return 0.0; + } + let magnitude = 10.0_f64.powi(digits - 1 - ((value.abs()).log10().floor() as i32)); + (value * magnitude).round() / magnitude + }).into_series(); return Ok(s); - } - if let Ok(ca) = s.i64() { - let s = ca - .apply_values(|val| round_sig_figs(val as f64, digits) as i64) - .into_series(); - return Ok(s); - } - polars_bail!(opq = round_sig_figs, s.dtype()); + }); } /// Floor underlying floating point array to the lowest integers smaller or equal to the float value. @@ -107,14 +93,6 @@ pub trait RoundSeries: SeriesSealed { impl RoundSeries for Series {} -fn round_sig_figs(value: f64, digits: i32) -> f64 { - if value == 0.0 { - return value; - } - let magnitude = 10.0_f64.powi(digits - 1 - ((value.abs()).log10().floor() as i32)); - (value * magnitude).round() / magnitude -} - #[cfg(test)] mod test { use super::*;