Casting Float64 to Float16 rounds twice through f32

Abierto
#12,475 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@wislertt ya está trabajando en esto.

Desde el 26/11/2026.

  • #12476 de @wislertt — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
20/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
rust
Área
backend, data

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
Rust
Estrellas
3.7k
Forks
1.5k
Merge medio
2 d 16 h
PR fusionados (30 d)
183

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de apache/arrow-rs

Todos los issues de apache/arrow-rs

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.