JuliaMath / JuliaMath/FixedPointNumbers.jl
support reinterpret from UIntX to Fixed{IntX, N}
- Dominant language
- Julia
- Stars
- 95
- Forks
- 38
- Avg merge
- 13m
- Merged PRs (30d)
- 2
Description
Currently if you have a `UInt16` that you want to reinterpret as a signed fixed-point number the `reinterpret` method [here](https://github.com/JuliaMath/FixedPointNumbers.jl/blob/c238d4205ee5abea6d596e71a3e1967e4b1c7080/src/fixed.jl#L11) doesn't apply, so you get an error:
```julia
julia> reinterpret(Fixed{Int16, 15}, 0x8123)
ERROR: bitcast: target type not a leaf primitive type
Stacktrace:
[1] reinterpret(::Type{FixedPointNumbers.Fixed{Int16,15}}, ::UInt16) at ./essentials.jl:155
[2] eval(::Module, ::Any) at ./boot.jl:235
```
Is it worth having another `reinterpret` method to handle this, or is it safer to have the user double-reinterpret? (e.g. `reinterpret(Fixed{Int16, 15}, reinterpret(Int16, 0x8123))`)
One proposed method would be:
```julia
function Base.reinterpret(::Type{Fixed{T,f}}, x::Unsigned) where {T <: Signed,f}
reinterpret(Fixed{T, f}, reinterpret(T, x))
end
```
Or we could even drop the requirement the `x` subtypes `Unsigned` if we want to reinterpret more broadly.
Contributor guide
Assessment
This issue has not been assessed yet.