# Displaying complex version of numeric type

**URL:** https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016
**Category:** General Usage
**Created:** [May 19, 2018, 5:56pm UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016 "2018-05-19T17:56:33Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [May 19, 2018, 5:56pm UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/1 "2018-05-19T17:56:33Z")

</div>

I’m creating a numeric type (fixed point) and ran into the following problem when trying to print:

here’s the definition of the type

```julia
type Fixed{m, f} <: Real
    x :: Int64
end

```

```julia
c = complex(a,b)

println(c)

ERROR: LoadError: no promotion exists for Fixed{1,15} and Int64
Stacktrace:
 [1] promote_type(::Type{Fixed{1,15}}, ::Type{Int64}) at ./promotion.jl:161
 [2] promote at ./promotion.jl:175 [inlined]
...

```

i tried defining a promote\_type,

```julia
function promote_type(x::Fixed{m,f}, ::Type{Int64}) where {m,f}
    x.x
end

```

and that gives me exactly the same error.  
Then I realized, why is it trying to promote the type ?  
if i do

```julia
println(a)

```

where a is Fixed{1,15}(0), i get the default representation,

```julia
Fixed{1,15}(0)

```

So it would seem that println(complex(a,b)) should simply use that representation for each of the real and imaginary parts. yes, this means that I haven’t defined a show method.

However defining a show method doesn’t seem to help.

```julia
function show(io::IO, x::Fixed{m,f}) where {m,f}
    print(io, x.x)
end

ERROR: LoadError: no promotion exists for Fixed{1,15} and Int64
Stacktrace:
 [1] promote_type(::Type{Fixed{1,15}}, ::Type{Int64}) at ./promotion.jl:161

```

I’ve been perusing the documentation and looking through FixedPointNumbers but can’t quite figure out what to try next.

Thanks,

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [May 19, 2018, 7:05pm UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/2 "2018-05-19T19:05:03Z")

</div>

You defined your type as

```julia
type Fixed{m, f} <: Real
    x :: Int64
end

```

Therefore `x` must be an `Int64`, It seems you’re trying to assign a complex number to `x`.  
Try the following type declaration instead.

```julia
type Fixed{m, f} <: Real
    x
end

```

By the way in Julia version 0.6 the keyword to use is `struct` instead of `type`.

---

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [May 19, 2018, 7:37pm UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/3 "2018-05-19T19:37:28Z")

</div>

You didn’t show the part of the stack trace which indicates that the trouble is in `signbit`. Since your type doesn’t seem to have one, try

```julia
import Base.signbit
signbit(x::Fixed{m,f}) where m where f = false

```

In general, if you see something like this in a stacktrace:

```julia
 [4] signbit(::Fixed{1,15}) at ./number.jl:72
 [5] show(::IOContext{Base.Terminals.TTYTerminal}, ::Complex{Fixed{1,15}}) at ./complex.jl:154

```

you can proceed to

```julia
julia> less( show,(IOContext{Base.Terminals.TTYTerminal}, Complex{Fixed{1,15}}))

```

where we see why `signbit` is called.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [May 19, 2018, 7:59pm UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/4 "2018-05-19T19:59:55Z")

</div>

@jandehaan  
yes, i should use struct, the value is not intended to be mutable.  
however i don’t understand your statement about “trying to assign a complex number to x”. The assignment worked fine, i.e.

```julia
c = complex(a,b)

```

succeeded. it was the print of ‘c’ that failed.  
@Ralph_Smith that did it. thank you. Is there anyway i could have figured that out from the documentation ? It seems like a very obscure point.

Also I could not get the ‘less’ statement you provided to work.

```julia
julia> include("test_fixed.jl")
Fixed{1,15}(0)
Fixed{1,15}(0)

julia> less(show,(IOContext{Base.Terminals.TTYTerminal}, Complex{Fixed{1,15}}))
ERROR: no method found for the specified argument types
Stacktrace:
 [1] which(::Any, ::Any) at ./reflection.jl:823
 [2] less(::Function, ::Any) at ./interactiveutil.jl:121

julia> 

```

I probably should have stated that I’m still using 0.6.0 …

---

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [May 19, 2018, 8:53pm UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/5 "2018-05-19T20:53:10Z")

</div>

> [@purplishrock](#):
>
> Is there anyway i could have figured that out from the documentation ? It seems like a very obscure point.

I think that’s one of the costs of implementing a low-level type. You could look at the source for the `FixedPointNumbers` package to see a superset of the (relevant part of the) interface for subtypes of `Real`. I fear that trial and error is the only way to find the minimum needed for your purposes, and the “error” part involves this sort of detective work.

> [@purplishrock](#):
>
> Also I could not get the ‘less’ statement you provided to work.

Is your `Fixed` type in the `Main` scope of the REPL? Or maybe the full spec for the `IO` type was different in 0.6.0? Whatever signature you got in the stacktrace should work, possibly with extra module qualifications (the method must have been found if it was traced).

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [May 19, 2018, 9:16pm UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/6 "2018-05-19T21:16:03Z")

</div>

@Ralph_Smith is correct. I misunderstood your example because the assignments of `a` and `b` were missing. In any case, look at [https://github.com/JuliaLang/julia/blob/master/base/complex.jl](https://github.com/JuliaLang/julia/blob/master/base/complex.jl) starting on line 181. It shows (no pun intended) that the `show` method for complex numbers depends on functions `signbit` and `isnan`. You’ll have to implement methods for those functions that take your type as input.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [May 20, 2018, 12:19am UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/7 "2018-05-20T00:19:17Z")

</div>

Great! Thanks very much for the help.

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [May 20, 2018, 4:53am UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/8 "2018-05-20T04:53:24Z")

</div>

> [@Ralph\_Smith](#):
>
> I think that’s one of the costs of implementing a low-level type. You could look at the source for the `FixedPointNumbers` package to see a superset of the (relevant part of the) interface for subtypes of `Real` . I fear that trial and error is the only way to find the minimum needed for your purposes, and the “error” part involves this sort of detective work.

I can’t help to bring back the _documentation of interface for abstract types_ topic. Having to dive in source code as your trial and error strategy proposal suggests is not really satisfying. I do accept that my concept/interface/traits concerns ([Question about abstract type interfaces - #3 by LaurentPlagne](https://discourse.julialang.org/t/question-about-abstract-type-interfaces/10605/3)) are related with OO way of thinking, but I also note that this question arises regularly.

---

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [May 20, 2018, 4:24pm UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/9 "2018-05-20T16:24:31Z")

</div>

I agree that well-defined and documented interfaces, with infrastructure that enforces them, would be better than workarounds such as I described. But all this takes work, and people keep finding reasons to give higher priority to other matters.

With regard to traits etc., I’ll point to [this thread on multiple traits](https://discourse.julialang.org/t/why-does-julia-not-support-multiple-traits/5278) as evidence that some of the developers (a) recognize these issues to be important and (b) understand that a good solution is likely to be difficult.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [May 20, 2018, 6:25pm UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/10 "2018-05-20T18:25:54Z")

</div>

and now more complications…  
my definition of zero:

```julia
zero(::Type{FixedPoint{m,f}}) where {m,f} = FixedPoint{m,f}(0)

```

and so

```julia
zf3 = zero(FixedPoint{1,31})
println(zf3)

0.0Q1f31

```

works but,

```julia
zf4 = zero(Complex{FixedPoint{1,31}})
println(zf4)
ERROR: LoadError: MethodError: Cannot `convert` an object of type Int64 to an object of type Fixed.FixedPoint{1,31}
This may have arisen from a call to the constructor Fixed.FixedPoint{1,31}(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] zero(::Type{Complex{Fixed.FixedPoint{1,31}}}) at ./number.jl:131
 [2] include_from_node1(::String) at ./loading.jl:569
 [3] include(::String) at ./sysimg.jl:14
 [4] process_options(::Base.JLOptions) at ./client.jl:305
 [5] _start() at ./client.jl:371

```

doesn’t. That’s so confusing. I can’t find anything in complex.jl that tells me how it’s generating a zero value, but you would hope it simply calls ‘zero’ for each of the real and imaginary parts, so it should just work. I looked in FixedPointNumbers too, and cannot find a zero method, and yet it just works also.

even the error line doesn’t make sense. Int64 is in the very definition of FixedPoint, why should a convert function to go from Int64 to FixedPoint even be required ??

FixedPoint{1,31}(0) is the very definition of a fixed point value using the constructor.

---

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [May 20, 2018, 7:38pm UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/11 "2018-05-20T19:38:34Z")

</div>

`convert` from Integer is definitely part of the `Real` interface; it can substitute for a simple constructor (but not vice-versa, as you were hoping here), and it’s used for several things like this. Also see the manual section on constructors:

> If you want to define a constructor for a lossless conversion from one type to another, you should probably define a convert method instead.

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [May 20, 2018, 8:36pm UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/12 "2018-05-20T20:36:02Z")

</div>

> [@purplishrock](#):
>
> zero(Complex{FixedPoint{1,31}})

The `@which` macro may be helpful here. It tells you which specific method will be invoked.

```julia
julia> @which zero(Complex{FixedPoint{1,31}})
zero{T<:Number}(::Type{T}) at number.jl:157

```

and `number.jl:157` reads

```julia
zero(::Type{T}) where {T<:Number} = convert(T,0)

```

Therefore you’ll need something like

```julia
convert(::Type{FixedPoint{m,f}}, x::Int) where {m,f} = FixedPoint{m,f}(x)

```

By the way, you’ll find that when you define your own Number type, you’ll have to define many methods to match. Consider using Julia package `AbstractNumbers`.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [May 21, 2018, 4:17am UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/13 "2018-05-21T04:17:10Z")

</div>

really, thanks very much to everyone for the help.

one other thing i should mention since it’s a bit of trap.

if you define your convert function, you can’t forget to do

‘’’  
import Base: convert  
‘’’

or you’ll get the exact same message. it took me an embarrasingly long time to figure that out …

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [May 21, 2018, 9:41am UTC](https://discourse.julialang.org/t/displaying-complex-version-of-numeric-type/11016/14 "2018-05-21T09:41:33Z")

</div>

Thank you very much for this answer and the link.  
As a Julia newbie, as was afraid that my questions about interface were totally irrelevant 😀
