Skip to content

Commit

Permalink
improve round precision
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jan 16, 2022
1 parent aff81cd commit 1a5a294
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions polars/polars-core/src/series/ops/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ impl Series {
pub fn round(&self, decimals: u32) -> Result<Self> {
use num::traits::Pow;
if let Ok(ca) = self.f32() {
let multiplier = 10.0.pow(decimals as f32) as f32;
// Note we do the computation on f64 floats to not loose precision
// when the computation is done, we cast to f32
let multiplier = 10.0.pow(decimals as f64);
let s = ca
.apply(|val| (val * multiplier).round() / multiplier)
.apply(|val| ((val as f64 * multiplier).round() / multiplier) as f32)
.into_series();
return Ok(s);
}
if let Ok(ca) = self.f64() {
let multiplier = 10.0.pow(decimals as f32) as f64;
let multiplier = 10.0.pow(decimals as f64);
let s = ca
.apply(|val| (val * multiplier).round() / multiplier)
.into_series();
Expand Down

0 comments on commit 1a5a294

Please sign in to comment.