From 4b3209f42b4a2e9ed09333748cb57ed3c5d1e6ea Mon Sep 17 00:00:00 2001 From: Joel Dahne Date: Sun, 21 Apr 2024 11:45:45 +0200 Subject: [PATCH] Fix some edge cases in string and show for Arb and Acb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The matrix `Arb[1 1]` is now displayed as 1×2 Matrix{Arb}: 1.0 1.0 instead of as 1×2 Matrix{Arb}: 1.00{…72 digits…} 1.00{…72 digits…} --- src/show.jl | 19 ++++++++++++++++--- test/show.jl | 11 +++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/show.jl b/src/show.jl index f2e977b..5a2d6d0 100644 --- a/src/show.jl +++ b/src/show.jl @@ -6,14 +6,14 @@ function _remove_trailing_zeros(str::AbstractString) # Numbers on the form xxx.yyy0ezzz mantissa, exponent = split(str, 'e', limit = 2) mantissa = rstrip(mantissa, '0') - if endswith(mantissa, '.') + if endswith(mantissa, '.') || endswith(mantissa, '}') mantissa *= '0' end str = mantissa * 'e' * exponent else # Numbers on the form xxx.yyy0 str = rstrip(str, '0') - if endswith(str, '.') + if endswith(str, '.') || endswith(str, '}') str *= '0' end end @@ -129,7 +129,20 @@ end function Base.show(io::IO, x::Union{ArbOrRef,AcbOrRef}) if Base.get(io, :compact, false) && rel_accuracy_bits(x) > 48 - print(io, string(x, condense = 2, unicode = true)) + str = string(x, condense = 2, unicode = true) + if isexact(x) + # For exact values the shortest output is in some cases + # not given by condensing the decimal digits, but by + # removing trailing zeros. We try both options and take + # the shortest. + + str_alt = string(x) + + if length(str_alt) < length(str) + str = str_alt + end + end + print(io, str) else print(io, string(x)) end diff --git a/test/show.jl b/test/show.jl index 6a8f960..9c9b80b 100644 --- a/test/show.jl +++ b/test/show.jl @@ -30,6 +30,7 @@ "1.000000000000000000" @test string(Arb(1), digits = 2, remove_trailing_zeros = false) == "1.0" @test string(Arb(1), digits = 12, remove_trailing_zeros = false) == "1.00000000000" + @test string(Arb(1), condense = 2) == "1.00{...72 digits...}0" @test string(Arb(10)^100) == "1.0e+100" @test string(Arb(10)^100, remove_trailing_zeros = false) == "1.0000000000000000000000000000000000000000000000000000000000000000000000000000e+100" @@ -90,10 +91,20 @@ @testset "show" begin @test repr(Mag(1), context = IOContext(stdout, :compact => true)) == "1.0" @test repr(Arf(1), context = IOContext(stdout, :compact => true)) == "1.0" + @test repr(Arb(1), context = IOContext(stdout, :compact => true)) == "1.0" + @test repr(Acb(1, 1), context = IOContext(stdout, :compact => true)) == + "1.0 + 1.0im" + @test repr(Arb(π), context = IOContext(stdout, :compact => true)) == "[3.14{…72 digits…}62 ± 1.93e-77]" @test repr(Acb(π, ℯ), context = IOContext(stdout, :compact => true)) == "[3.14{…72 digits…}62 ± 1.93e-77] + [2.71{…72 digits…}35 ± 5.46e-77]im" + @test repr(Arb(Float64(π)), context = IOContext(stdout, :compact => true)) == + "3.14{…72 digits…}0" + @test repr( + Acb(Float64(π), Float64(ℯ)), + context = IOContext(stdout, :compact => true), + ) == "3.14{…72 digits…}0 + 2.71{…72 digits…}0im" prec = 32