# Help with promote on FixedPointNumbers

**URL:** <https://discourse.julialang.org/t/help-with-promote-on-fixedpointnumbers/10047>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 29, 2018, 9:26am UTC](https://discourse.julialang.org/t/help-with-promote-on-fixedpointnumbers/10047 "2018-03-29T09:26:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)\
**Post date:** [March 29, 2018, 9:26am UTC](https://discourse.julialang.org/t/help-with-promote-on-fixedpointnumbers/10047/1 "2018-03-29T09:26:31Z")

</div>

Hello,  
what is the right way in using FixedPointNumbers.jl to achieve custom math operation:

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

```julia
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.

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 29, 2018, 11:07am UTC](https://discourse.julialang.org/t/help-with-promote-on-fixedpointnumbers/10047/2 "2018-03-29T11:07:09Z")

</div>

You could define

```julia
Base.promote_type(::Type{Fixed{S, f}}, ::Type{Fixed{T, f}}) where {f, S, T} =
    Fixed{promote_type(S, T), f}

```

as the error message suggests, but then there appears to be no `convert` method, so it does not help you much (unless, of course, you are willing to define one too).

I would recommend that you open an issue for the package.

---

<div class="post-metadata">

**Author:** ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)\
**Post date:** [March 30, 2018, 8:45am UTC](https://discourse.julialang.org/t/help-with-promote-on-fixedpointnumbers/10047/3 "2018-03-30T08:45:47Z")

</div>

After some further investigation, these are my findings:

- **Beware worl-age problem!**
- `Base.promote_rule` definition is the first thing to try (and what the manual suggests).
- Nonexisting `promote_type` error is just a fallback if promote\_rule is not defined  
and my first code would do the right thing if I wasn’t bitten by `world age problem`. Before the method definition I already executed a\*c, thus forced Julia to compile the function without the `promote_rule`. And then the old one was used.

One has to be careful whenever `import Base.any_function` appears in his code and the function is not top level but used internally by any other function. I might make a PR to mention this in the relevant places of the manual.
