Skip to content

Commit

Permalink
Use powi.
Browse files Browse the repository at this point in the history
  • Loading branch information
owrior authored and orlp committed Nov 9, 2023
1 parent cc7bc27 commit f4b6066
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions crates/polars-ops/src/series/ops/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub trait RoundSeries: SeriesSealed {
polars_bail!(opq = round, s.dtype());
}

fn round_sig_figs(&self, digits: u32) -> PolarsResult<Series> {
fn round_sig_figs(&self, digits: i32) -> PolarsResult<Series> {
let s = self.as_series();
if digits < 1 {
polars_bail!(
Expand Down Expand Up @@ -107,11 +107,11 @@ pub trait RoundSeries: SeriesSealed {

impl RoundSeries for Series {}

fn round_sig_figs(value: f64, digits: u32) -> f64 {
fn round_sig_figs(value: f64, digits: i32) -> f64 {
if value == 0.0 {
return value;
}
let magnitude = 10.0.pow(digits as f64 - 1.0 - ((value.abs()).log10().floor()));
let magnitude = 10.0_f64.powi(digits - 1 - ((value.abs()).log10().floor() as i32));
(value * magnitude).round() / magnitude
}

Expand Down
2 changes: 1 addition & 1 deletion crates/polars-plan/src/dsl/function_expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub enum FunctionExpr {
},
#[cfg(feature = "round_series")]
RoundSF {
digits: u32,
digits: i32,
},
#[cfg(feature = "round_series")]
Floor,
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-plan/src/dsl/function_expr/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub(super) fn round(s: &Series, decimals: u32) -> PolarsResult<Series> {
s.round(decimals)
}

pub(super) fn round_sig_figs(s: &Series, digits: u32) -> PolarsResult<Series> {
pub(super) fn round_sig_figs(s: &Series, digits: i32) -> PolarsResult<Series> {
s.round_sig_figs(digits)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/polars-plan/src/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ impl Expr {

/// Round underlying floating point array to given significant figures.
#[cfg(feature = "round_series")]
pub fn round_sig_figs(self, digits: u32) -> Self {
pub fn round_sig_figs(self, digits: i32) -> Self {
self.map_private(FunctionExpr::RoundSF { digits })
}

Expand Down
2 changes: 1 addition & 1 deletion py-polars/src/expr/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl PyExpr {
self.inner.clone().round(decimals).into()
}

fn round_sig_figs(&self, digits: u32) -> Self {
fn round_sig_figs(&self, digits: i32) -> Self {
self.clone().inner.round_sig_figs(digits).into()
}

Expand Down

0 comments on commit f4b6066

Please sign in to comment.