Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bad print format for Complex{Unsigned} #37756

Closed
vtjnash opened this issue Sep 25, 2020 · 7 comments
Closed

bad print format for Complex{Unsigned} #37756

vtjnash opened this issue Sep 25, 2020 · 7 comments
Labels
bug Indicates an unexpected problem or unintended behavior display and printing Aesthetics and correctness of printed representations of objects. good first issue Indicates a good issue for first-time contributors to Julia help wanted Indicates that a maintainer wants help on an issue or pull request

Comments

@vtjnash
Copy link
Member

vtjnash commented Sep 25, 2020

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

julia> 0x1e*im
0x00 + 0x1eim

julia> 0x1eim
ERROR: syntax: invalid numeric constant "0x1ei"
@vtjnash vtjnash added the display and printing Aesthetics and correctness of printed representations of objects. label Sep 25, 2020
@JeffreySarnoff
Copy link
Contributor

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
Copy link
Member

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
Copy link
Contributor

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

@JeffBezanson JeffBezanson added the bug Indicates an unexpected problem or unintended behavior label Sep 28, 2020
@User-764Q
Copy link

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

@simeonschaub simeonschaub added good first issue Indicates a good issue for first-time contributors to Julia help wanted Indicates that a maintainer wants help on an issue or pull request labels Oct 16, 2021
@P3rcy-8685
Copy link

P3rcy-8685 commented Aug 20, 2022

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
Copy link

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

@KristofferC
Copy link
Member

Please assign this issue to me.

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

tsbranquinho added a commit to tsbranquinho/julia that referenced this issue Mar 17, 2025
…ry parts in 'show'

The 'show' method for 'Complex' was not properly handling cases where the imaginary part was represented in hexadecimal ('0x'). This could lead to missing multiplication symbols '*' in the output, making the formatting inconsistent.

To fix this, a check using 'occursin("0x", repr(i))' was added to detect these cases and ensure proper formatting.

This improves the consistency of complex number representations and prevents ambiguities in the printed output.
tsbranquinho added a commit to tsbranquinho/julia that referenced this issue Mar 17, 2025
The 'show' method for 'Complex' was not properly handling cases where
the imaginary part was represented in hexadecimal ('0x'). This could
lead to missing multiplication symbols '*' in the output, making the
formatting inconsistent.

To fix this, a check using 'occursin("0x", repr(i))' was added to
detect these cases and ensure proper formatting.

This improves the consistency of complex number representations and
prevents ambiguities in the printed output.
tsbranquinho added a commit to tsbranquinho/julia that referenced this issue Mar 17, 2025
These additions improve test coverage and ensure robust handling of
different numeric representations in the `show` function.

This extends the test suite in reference to issue JuliaLang#37756.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior display and printing Aesthetics and correctness of printed representations of objects. good first issue Indicates a good issue for first-time contributors to Julia help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

No branches or pull requests

8 participants