Skip to content

Commit

Permalink
improved the rounding_digit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
himadripal committed Dec 21, 2024
1 parent 819f0d6 commit dd3874d
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions arrow-cast/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,19 +823,16 @@ fn parse_e_notation<T: DecimalType>(
)));
}

let rounding_digit;

if exp < 0 {
let result_str = result.to_i64().unwrap().to_string();
let wrapped_result = result.div_wrapping(base.pow_wrapping(-exp as _));
let rounding_digit_position = wrapped_result.to_i64().unwrap().to_string().len(); // position inside result_str
rounding_digit = result_str[rounding_digit_position - 1..rounding_digit_position]
.parse::<i32>()
.unwrap();
if rounding_digit >= 5 {
result = wrapped_result.add_wrapping(T::Native::usize_as(1));
let result_with_scale = result.div_wrapping(base.pow_wrapping(-exp as _));
let result_with_one_scale_above =
result.div_wrapping(base.pow_wrapping(-exp.sub_wrapping(1) as _));
let rounding_digit =
result_with_one_scale_above.sub_wrapping(result_with_scale.mul_wrapping(base));
if rounding_digit >= T::Native::usize_as(5) {
result = result_with_scale.add_wrapping(T::Native::usize_as(1));
} else {
result = wrapped_result;
result = result_with_scale;
}
} else {
result = result.mul_wrapping(base.pow_wrapping(exp as _));
Expand Down

0 comments on commit dd3874d

Please sign in to comment.