JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.6k stars 5.48k forks source link

bad print format for Complex{Unsigned} #37756

Open vtjnash opened 4 years ago

vtjnash commented 4 years ago

Complex number printing assumes that simple concatenation will produce something meaningful:

julia> 0x1e*im
0x00 + 0x1eim

julia> 0x1eim
ERROR: syntax: invalid numeric constant "0x1ei"
JeffreySarnoff commented 4 years ago

plausible logic

restr = sprint(show, complexnum.re)
imstr = sprint(show, complexnum.im)
imstr = '9' < imstr[end] ? (imstr * "*im") : (imstr * "im")
imstr = signbit(complexnum.im) ? " - "*imstr[2:end] : " + " * imstr
str = restr * imstr
simeonschaub commented 4 years ago

Perhaps we also just might want to do show_unquoted(:($(z.re) + $(z.im) * im)), since that already handles all this logic. We would be putting arbitrary number types into the AST, but I don't think that should be a problem if it's just for printing, since show_unquoted should always fall back to show.

JeffreySarnoff commented 4 years ago

I was unaware of show_unquoted. With Complex{<:Unsigned} it interposes spaces around the '*', though.

julia> z = complex(0x3a, 0x2e)
0x3a + 0x2eim

julia> Base.show_unquoted(stdout,:($(z.re) + $(z.im) * im))
0x3a + 0x2e * im

julia> z = complex(58, 46)
58 + 46im

julia> Base.show_unquoted(stdout,:($(z.re) + $(z.im) * im))
58 + 46im
User-764Q commented 3 years ago

Bump, I tested this today on Julia 1.6.2 and it was still a problem.

P3rcy-8685 commented 2 years ago

I would like to take up this issue, but I wanted to clarify what exactly the problem is. We would like complex numbers to be printed in the following manner, right? z=complex(1,2) 1 + 2*im Or is something else needed?

divyaS09 commented 1 year ago

I would like to contribute to resolving this issue. Please assign this issue to me.

KristofferC commented 1 year ago

Please assign this issue to me.

This is generally not done. But don't let that stop you from working on it :)