wislertt opened a new pull request, #12476: URL: https://github.com/apache/arrow-rs/pull/12476
# Which issue does this PR close? - Closes #12475 # Rationale for this change Casting `Float64` to `Float16` went through `num_cast`, which in the `half` crate converts via `f32`. Rounding twice can give the wrong `f16` for values just above a midpoint: `1.0 + 2^-11 + 2^-40` became `1.0` instead of `1.0009766`. # What changes are included in this PR? A new `f64_to_f16` helper, used by the `(Float64, Float16)` cast through `unary`. It rounds to `f32` toward zero and sets the last bit when that is inexact (round to odd), then calls `f16::from_f32`. An `f32` has 13 more significand bits than an `f16`, so the round-to-odd value never sits on an `f16` midpoint unless the input did, and the final rounding gives the correctly rounded result. `f16::from_f64` is not used because on x86 with F16C it also goes through `f32`. Casts to a float cannot fail, so `safe`, NaN, infinity, overflow and nulls behave as before. # Are these changes tested? Yes. `test_cast_f64_to_f16_rounds_once` casts a value just above the midpoint after `1.0` with both signs, plus the exact midpoint, which still rounds to even. It fails without the fix and passes with it. # Are there any significant user-facing changes? No API change. Some `Float64` values now cast to the correctly rounded `Float16`, one `f16` ulp different from before. # AI usage disclosure Claude Code helped find 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]
