wislertt opened a new pull request, #12355: URL: https://github.com/apache/arrow-rs/pull/12355
# Which issue does this PR close? - Closes #11316 # Rationale for this change `ToPrimitive::to_f32` for `i256` used the trait default, `to_f64() as f32`, which rounds twice. Near a binary32 midpoint the first rounding can move the value across the midpoint, so the result differs from a correctly rounded `f32`. For example `2^53 + 2^29 + 1` gives `0x5a000000` instead of `0x5a000001`. # What changes are included in this PR? Override `ToPrimitive::to_f32` for `i256`: convert the magnitude with `u128 as f32`, which rounds once, return infinity when the magnitude is `2^128` or more, then apply the sign. `to_f64` is unchanged. # Are these changes tested? Yes. `test_i256_to_f32_midpoint_rounding` checks binary32 midpoints and their neighbours (both signs, exponents 24 to 254) plus the extremes against Rust's correctly rounded decimal parse. It fails without the fix and passes with it. # Are there any significant user-facing changes? No API change. `i256::to_f32` now returns the correctly rounded value where it previously could be off by one `f32` ulp. Out-of-range values still give `Some(±inf)`, as before. # AI usage disclosure Claude Code helped reproduce the bug, write the fix and the test, and run the checks. I reviewed the diff and can explain every line. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
