Hello,
what is the right way in using FixedPointNumbers.jl to achieve custom math operation:
using FixedPointNumbers
a = convert(Fixed{Int8, 6}, 1.0)
b = convert(Fixed{Int8, 6}, 0.5)
c = convert(Fixed{Int16, 6}, 0.25)
#working
julia> a*b
0.5Q1f6
julia> a*c
ERROR: no promotion exists for FixedPointNumbers.Fixed{Int8,6} and FixedPointNum
bers.Fixed{Int16,6}
Stacktrace:
[1] promote_type(::Type{FixedPointNumbers.Fixed{Int8,6}}, ::Type{FixedPointNumb
ers.Fixed{Int16,6}}) at .\promotion.jl:161
[2] promote at .\promotion.jl:175 [inlined]
[3] *(::FixedPointNumbers.Fixed{Int8,6}, ::FixedPointNumbers.Fixed{Int16,6}) at
.\promotion.jl:250
I tried
import Base.promote_rule
promote_rule(::Type{Fixed{T1,f}}, ::Type{Fixed{T2,f}}) where {T1<:Signed,
T2<:Signed,f} = sizeof(T1)> sizeof(T2) ? Fixed{T1,f} : Fixed{T2,f}
in order to widen the result (just example, I’d be more careful with widening for different operations). But still the promote machinery doesn’t work.
promote(a,c)
ERROR: no promotion exists for FixedPointNumbers.Fixed{Int8,6} and FixedPointNum
bers.Fixed{Int16,6}
Stacktrace:
[1] promote_type(::Type{FixedPointNumbers.Fixed{Int8,6}}, ::Type{FixedPointNumb
ers.Fixed{Int16,6}}) at .\promotion.jl:161
[2] promote(::FixedPointNumbers.Fixed{Int8,6}, ::FixedPointNumbers.Fixed{Int16,
6}) at .\promotion.jl:175
What is the right way to define the promotion rules?
Thanks,
Petr