Possible bug in conversions of Float16 NaN values to Float32/Float64

The code for convert(::Type{Float32}, val::Float16) in Base at float.jl:155 seems to be incorrect.

I had expected that conversions of NaN values from Float16 to larger types would preserve the NaN payload,
so that a round-trip would give the exact same bit pattern, however, for 2046 of the possible NaN16 values,
the 10 bits of payload get replaced by 0.

Conversions from Float32 to Float64 and back again will always give the same bit pattern, so I believe this is a bug.
To fix this, line 185 needs: ret = 0x7fc00000 | (sign<<31) replaced by ret = 0x7fc00000 | (sign<<31) | (sig<<(23-10))

Issue opened: https://github.com/JuliaLang/julia/issues/23244.

1 Like

Thanks! Can this be placed on the backport list for v0.6.x?
(it would be nice to not have to implement a work-around until v1.0 comes out :slight_smile: )

Done.