Skip to content

Commit f8eaeb4

Browse files
Fix #37756: Correct formatting of hexadecimal imaginary parts (#57803)
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. --------- Co-authored-by: Lilith Orion Hafner <[email protected]>
1 parent bf01638 commit f8eaeb4

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

base/complex.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function show(io::IO, z::Complex)
206206
print(io, compact ? "+" : " + ")
207207
show(io, i)
208208
end
209-
if !(isa(i,Integer) && !isa(i,Bool) || isa(i,AbstractFloat) && isfinite(i))
209+
if !(isa(i,Signed) || isa(i,AbstractFloat) && isfinite(i))
210210
print(io, "*")
211211
end
212212
print(io, "im")

test/complex.jl

+15-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,21 @@ for T in (Int64, Float64)
2424
@test complex(Complex{T}) == Complex{T}
2525
end
2626

27-
#show
28-
@test sprint(show, complex(1, 0), context=:compact => true) == "1+0im"
29-
@test sprint(show, complex(true, true)) == "Complex(true,true)"
30-
@test sprint(show, Complex{Int8}(0, typemin(Int8))) == "0 - 128im"
27+
@testset "show" begin
28+
@test sprint(show, complex(1, 0), context=:compact => true) == "1+0im"
29+
@test sprint(show, complex(true, true)) == "Complex(true,true)"
30+
@test sprint(show, Complex{Int8}(0, typemin(Int8))) == "0 - 128im"
31+
@test sprint(show, complex(typemin(Int16), typemax(Int16))) == "-32768 + 32767im"
32+
@test sprint(show, complex(0x26, 0x26), context=:compact => true) == "0x26+0x26*im"
33+
@test sprint(show, complex(0o77, 0o77), context=:compact => true) == "0x3f+0x3f*im"
34+
@test sprint(show, complex(0b10, 0b11)) == "0x02 + 0x03*im"
35+
@test sprint(show, complex(-0x1A, 0x2F), context=:compact => true) == "0xe6+0x2f*im"
36+
@test sprint(show, complex(typemax(UInt16), typemin(UInt16))) =="0xffff + 0x0000*im"
37+
@test sprint(show, complex(-Inf, Inf)) == "-Inf + Inf*im"
38+
@test sprint(show, complex(-Inf, NaN)) == "-Inf + NaN*im"
39+
@test sprint(show, complex(0, -Inf)) == "0.0 - Inf*im"
40+
end
41+
3142

3243
@testset "unary operator on complex boolean" begin
3344
@test +Complex(true, true) === Complex(1, 1)

0 commit comments

Comments
 (0)