From ad09d36c6059710d6ec85384706a88e5b3d2289b Mon Sep 17 00:00:00 2001 From: Tiago Branquinho Date: Mon, 17 Mar 2025 16:33:28 +0000 Subject: [PATCH 1/4] Fix #37756: Correct formatting of hexadecimal imaginary parts 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. --- base/complex.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/complex.jl b/base/complex.jl index 9163d91174624..bc8f757d01d69 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -206,7 +206,7 @@ function show(io::IO, z::Complex) print(io, compact ? "+" : " + ") show(io, i) end - if !(isa(i,Integer) && !isa(i,Bool) || isa(i,AbstractFloat) && isfinite(i)) + if !(isa(i,Integer) && !isa(i,Bool) || isa(i,AbstractFloat) && isfinite(i)) || occursin("0x",repr(i)) print(io, "*") end print(io, "im") From 04a82155cfd556dda76bde1b4392aff8b3851490 Mon Sep 17 00:00:00 2001 From: Tiago Branquinho Date: Mon, 17 Mar 2025 19:15:33 +0000 Subject: [PATCH 2/4] Refactor base/complex.jl: Improve readability with type check Replaced explicit conditionals with a type check to make the code easier to understand and maintain. This ensures that the logic is clearer while preserving the original behavior. Co-authored-by: Lilith Orion Hafner --- base/complex.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/complex.jl b/base/complex.jl index bc8f757d01d69..2520f4f856868 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -206,7 +206,7 @@ function show(io::IO, z::Complex) print(io, compact ? "+" : " + ") show(io, i) end - if !(isa(i,Integer) && !isa(i,Bool) || isa(i,AbstractFloat) && isfinite(i)) || occursin("0x",repr(i)) + if !(isa(i,Signed) || isa(i,AbstractFloat) && isfinite(i)) print(io, "*") end print(io, "im") From 3e2810f4b0e8ac89b3c5b77a3ac41ad2a2b7b482 Mon Sep 17 00:00:00 2001 From: Tiago Branquinho Date: Mon, 17 Mar 2025 19:22:42 +0000 Subject: [PATCH 3/4] test: add tests for `show` method with various number formats 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 #37756. --- test/complex.jl | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/test/complex.jl b/test/complex.jl index 63304652ee7d8..fc311af418fb1 100644 --- a/test/complex.jl +++ b/test/complex.jl @@ -1,6 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using LinearAlgebra +using Test @test reim(2 + 3im) == (2, 3) @@ -24,10 +25,21 @@ for T in (Int64, Float64) @test complex(Complex{T}) == Complex{T} end -#show -@test sprint(show, complex(1, 0), context=:compact => true) == "1+0im" -@test sprint(show, complex(true, true)) == "Complex(true,true)" -@test sprint(show, Complex{Int8}(0, typemin(Int8))) == "0 - 128im" +@testset "show" begin + @test sprint(show, complex(1, 0), context=:compact => true) == "1+0im" + @test sprint(show, complex(true, true)) == "Complex(true,true)" + @test sprint(show, Complex{Int8}(0, typemin(Int8))) == "0 - 128im" + @test sprint(show, complex(typemin(Int16), typemax(Int16))) == "-32768 + 32767im" + @test sprint(show, complex(0x26, 0x26), context=:compact => true) == "0x26+0x26*im" + @test sprint(show, complex(0o77, 0o77), context=:compact => true) == "0x3f+0x3f*im" + @test sprint(show, complex(0b10, 0b11)) == "0x02 + 0x03*im" + @test sprint(show, complex(-0x1A, 0x2F), context=:compact => true) == "0xe6+0x2f*im" + @test sprint(show, complex(typemax(UInt16), typemin(UInt16))) =="0xffff + 0x0000*im" + @test sprint(show, complex(-Inf, Inf)) == "-Inf + Inf*im" + @test sprint(show, complex(-Inf, NaN)) == "-Inf + NaN*im" + @test sprint(show, complex(0, -Inf)) == "0.0 - Inf*im" +end + @testset "unary operator on complex boolean" begin @test +Complex(true, true) === Complex(1, 1) From 48e97df0e2bb66534a6a4c57941fe30df724a77e Mon Sep 17 00:00:00 2001 From: Tiago Branquinho Date: Mon, 17 Mar 2025 22:23:26 +0000 Subject: [PATCH 4/4] test: remove redundant `using Test` in test/complex.jl Co-authored-by: Lilith Orion Hafner --- test/complex.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/complex.jl b/test/complex.jl index fc311af418fb1..e24e54febc815 100644 --- a/test/complex.jl +++ b/test/complex.jl @@ -1,7 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using LinearAlgebra -using Test @test reim(2 + 3im) == (2, 3)