wislertt opened a new issue, #12475:
URL: https://github.com/apache/arrow-rs/issues/12475

   ### Describe the bug
   
   `cast` from `Float64` to `Float16` goes through `num_cast`, and the `half` 
crate's `NumCast` for `f16` converts with `to_f32().map(f16::from_f32)`. That 
rounds to `f32` first and then to `f16`. A value just above an `f16` midpoint 
can land exactly on the midpoint in `f32`, and round-half-even then picks the 
lower neighbour.
   
   ### To Reproduce
   
   ```rust
   use arrow_array::{Float64Array, cast::AsArray, types::Float16Type};
   use arrow_cast::cast;
   use arrow_schema::DataType;
   
   let x = 1.0 + 2f64.powi(-11) + 2f64.powi(-40);
   let out = cast(&Float64Array::from(vec![x]), &DataType::Float16).unwrap();
   println!("{}", out.as_primitive::<Float16Type>().value(0)); // 1
   ```
   
   ### Expected behavior
   
   The nearest `f16`, `1.0009766` (`1 + 2^-10`). `x` is above `1 + 2^-11`, the 
midpoint between `1.0` and `1 + 2^-10`, so it should round up.
   
   ### Additional context
   
   Switching to `f16::from_f64` is not enough on its own: on x86 with F16C, 
`half` also implements it as `f64 as f32` followed by the hardware `f32` to 
`f16` conversion, so it rounds twice there too.
   
   Integer casts to `Float16` also go through `f32`, but they are not affected: 
every integer in `f16`'s range is exact in `f32`.
   
   ### Willingness to contribute
   
   I plan to contribute a fix for this bug independently
   


-- 
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]

Reply via email to