From dd3874d4d243b84bd2064cec7d192f65fc4192e2 Mon Sep 17 00:00:00 2001 From: himadripal Date: Sat, 21 Dec 2024 14:34:50 -0800 Subject: [PATCH] improved the rounding_digit logic --- arrow-cast/src/parse.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/arrow-cast/src/parse.rs b/arrow-cast/src/parse.rs index c70b3e58fd2..d23ae856b76 100644 --- a/arrow-cast/src/parse.rs +++ b/arrow-cast/src/parse.rs @@ -823,19 +823,16 @@ fn parse_e_notation( ))); } - 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::() - .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 _));