Casting Float64 to Float16 rounds twice through f32

Đang mở
#12,475 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@wislertt đang làm issue này rồi.

Từ ngày 26/11/2026.

  • #12476 của @wislertt — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
20/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
rust
Lĩnh vực
backend, data

Hướng nghiên cứu

Start at arrow_cast::cast and trace the Float64 to Float16 path through num_cast and the half crate conversion described in the issue. The reproduction gives a precise failing value and expected result; use it to check the conversion path and add a regression test alongside the existing cast tests. Done means the value rounds to 1.0009766 directly from f64; issue #12476 already has an open pull request.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Rust
Star
3.7k
Fork
1.5k
Merge trung bình
2 ngày 16 giờ
Pull request đã merge (30 ngày)
183

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của apache/arrow-rs

Tất cả issue của apache/arrow-rs

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.