You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
multiply_fixed_point returns wrong values or panics when the required scale is more than 76 below the product scale #11605
multiply_fixed_point, multiply_fixed_point_checked and multiply_fixed_point_dyn round the product with the divisor 10^(product_scale - required_scale). When that exponent is 77 or more, the divisor does not fit in i256 and wraps, and the results are wrong: a zero product gives 1. When the exponent is above 127, computing it overflows i8, which panics in debug builds. I did not check what a release build returns.
To Reproduce
use arrow_arith::arithmetic::{multiply_fixed_point, multiply_fixed_point_checked};use arrow_array::Decimal128Array;fndec(v:Vec<i128>,p:u8,s:i8) -> Decimal128Array{Decimal128Array::from(v).with_precision_and_scale(p, s).unwrap()}fnmain(){// Product scale 0, required scale -77let a = dec(vec![0,5,5],1,0);let b = dec(vec![0,5, -5],1,0);let r = multiply_fixed_point(&a,&b, -77).unwrap();println!("{:?}", r.values());// [1, 1, -1]let r = multiply_fixed_point_checked(&a,&b, -77).unwrap();println!("{:?}", r.values());// [1, 1, -1]// Product scale 38, required scale -90let z = dec(vec![0],1,0);let c = dec(vec![0],38,38);let _ = multiply_fixed_point(&z,&c, -90);// debug build: panics with "attempt to subtract with overflow"}
Expected behavior
[0, 0, 0] for both calls in the first case, because no product of two i128 values reaches 10^77 / 2, so every product rounds to 0 at that scale. Alternatively an error, as the docs specify for a required scale above the product scale. They do not restrict how far below it the required scale can be, and Decimal128(p, -77) passes validate_decimal_precision_and_scale.
Additional context
get_fixed_point_info computes product_scale - required_scale in i8 and the divisor with i256::pow_wrapping. 10^77 wraps to a negative i256, so half in divide_and_round is negative and every non-negative remainder rounds up. Should exponents above 76 return zeros, or an error?
Found with property-based tests that build arrays in every physical layout the Arrow format allows, on main at 72887ed9. The code is unchanged on main at dd240116.
I'll return zeros for both multiply_fixed_point and multiply_fixed_point_checked when the required scale is more than 76 below the product scale. A product of two i128 values is below 10^77 / 2 so it rounds to zero there anyway. It also keeps the docs free of a new error case.
Activity
VanshajPoonia commentedon Nov 25, 2026
take
VanshajPoonia commentedon Nov 25, 2026
I'll return zeros for both multiply_fixed_point and multiply_fixed_point_checked when the required scale is more than 76 below the product scale. A product of two i128 values is below 10^77 / 2 so it rounds to zero there anyway. It also keeps the docs free of a new error case.