Skip to content

Commit 55ee461

Browse files
authored
Improve consistency in printing (#189)
In Julia v1.6, displaying aliases is going to be supported, so this no longer customizes the `summary`. This also simplifies the display of array elements based on the `:typeinfo` property. This fixes the broken display of aliases.
1 parent cab78d7 commit 55ee461

File tree

5 files changed

+54
-27
lines changed

5 files changed

+54
-27
lines changed

src/FixedPointNumbers.jl

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ rawtype(::Type{X}) where {T, X <: FixedPoint{T}} = T
4949

5050
# traits based on static parameters
5151
signbits(::Type{X}) where {T, X <: FixedPoint{T}} = T <: Unsigned ? 0 : 1
52+
nbitsint(::Type{X}) where {X <: FixedPoint} = bitwidth(X) - nbitsfrac(X) - signbits(X)
5253

5354
# construction using the (approximate) intended value, i.e., N0f8
5455
*(x::Real, ::Type{X}) where {X <: FixedPoint} = _convert(X, x)
@@ -260,27 +261,49 @@ function length(r::StepRange{<:FixedPoint})
260261
return div((stop - start) + step, step)
261262
end
262263

264+
hasalias(::Type) = false
265+
hasalias(::Type{X}) where {T<:NotBiggerThanInt64, f, X<:FixedPoint{T,f}} = f isa Int
266+
263267
# Printing. These are used to generate type-symbols, so we need them
264-
# before we include any files.
265-
function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
266-
print(io, typechar(X))
267-
f = nbitsfrac(X)
268-
m = bitwidth(X)-f-signbits(X)
269-
print(io, m, 'f', f)
268+
# before we include "src/fixed.jl" / "src/normed.jl".
269+
@inline function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
270+
if hasalias(X)
271+
f = nbitsfrac(X)
272+
m = nbitsint(X)
273+
write(io, typechar(X))
274+
m > 9 && write(io, Char(m ÷ 10 + 0x30))
275+
write(io, Char(m % 10 + 0x30), 'f')
276+
f > 9 && write(io, Char(f ÷ 10 + 0x30))
277+
write(io, Char(f % 10 + 0x30))
278+
else
279+
print(io, X)
280+
end
270281
io
271282
end
283+
272284
function show(io::IO, x::FixedPoint{T,f}) where {T,f}
285+
compact = get(io, :compact, false)::Bool
273286
log10_2 = 0.3010299956639812
274-
show(io, round(convert(Float64,x), digits=ceil(Int, f * log10_2)))
275-
get(io, :compact, false) || showtype(io, typeof(x))
287+
digits = min(ceil(Int, f * log10_2), compact ? 6 : typemax(Int))
288+
val = round(convert(Float64, x), digits=digits)
289+
if compact || get(io, :typeinfo, Any) === typeof(x)
290+
show(io, val)
291+
elseif hasalias(typeof(x))
292+
show(io, val)
293+
showtype(io, typeof(x))
294+
else
295+
print(io, typeof(x), '(', val, ')')
296+
end
276297
end
277298

278-
function Base.showarg(io::IO, a::Array{T}, toplevel) where {T<:FixedPoint}
279-
toplevel || print(io, "::")
280-
print(io, "Array{")
281-
showtype(io, T)
282-
print(io, ",$(ndims(a))}")
283-
toplevel && print(io, " with eltype ", T)
299+
if VERSION < v"1.6.0-DEV.356"
300+
function Base.showarg(io::IO, a::Array{X}, toplevel) where {X<:FixedPoint}
301+
toplevel || print(io, "::")
302+
print(io, "Array{")
303+
showtype(io, X)
304+
print(io, ",$(ndims(a))}")
305+
toplevel && hasalias(X) && print(io, " with eltype ", X)
306+
end
284307
end
285308

286309
include("fixed.jl")

src/fixed.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ struct Fixed{T <: Signed, f} <: FixedPoint{T, f}
2424
end
2525
end
2626

27+
# TODO: remove this
28+
hasalias(::Type{F}) where {F <: Union{Fixed{Int8,8},Fixed{Int16,16},Fixed{Int32,32},Fixed{Int64,64}}} = false
29+
2730
typechar(::Type{X}) where {X <: Fixed} = 'Q'
2831

2932
for T in (Int8, Int16, Int32, Int64)

src/utilities.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const LongInts = Union{Int64, UInt64, Int128, UInt128, BigInt}
2020

2121
const ShorterThanInt = Int === Int32 ? ShortInts : Union{ShortInts, Int32, UInt32}
2222
const NotBiggerThanInt = Union{ShorterThanInt, Int, UInt}
23+
const NotBiggerThanInt64 = Union{ShortInts, Int32, UInt32, Int64, UInt64}
2324
const SShorterThanInt = typeintersect(ShorterThanInt, Signed)
2425
const UShorterThanInt = typeintersect(ShorterThanInt, Unsigned)
2526

test/fixed.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -486,30 +486,30 @@ end
486486
@test String(take!(iob)) == "-21845.3"
487487

488488
show(IOContext(iob, :typeinfo=>Q15f16), q15f16)
489-
@test String(take!(iob)) == "-21845.33334Q15f16" # TODO: Consider removing suffix (issue #188)
489+
@test String(take!(iob)) == "-21845.33334"
490490

491491
show(IOContext(iob, :typeinfo=>Normed), q15f16)
492492
@test String(take!(iob)) == "-21845.33334Q15f16"
493493

494494
show(iob, Fixed{Int128,64}(-1.2345e6))
495-
@test_broken String(take!(iob)) == "Fixed{Int128,64}(-1.2345e6)" # "Q63f64" is not defined
495+
@test String(take!(iob)) == "Fixed{Int128,64}(-1.2345e6)"
496496

497497
# TODO: remove this test
498498
show(iob, reinterpret(Fixed{Int8,8}, signed(0xaa)))
499-
@test_broken String(take!(iob)) == "Fixed{Int8,8}(-0.336)" # "Q-1f8" is invalid
499+
@test String(take!(iob)) == "Fixed{Int8,8}(-0.336)"
500500
end
501501

502502
@testset "summary" begin
503503
a = Q0f7[0.2, 0.4]
504504
aa = Fixed[0.2Q0f7 0.4Q0f15]
505505

506506
if VERSION >= v"1.6.0-DEV.356"
507-
@test_broken summary(a) == "2-element Vector{Q0f7}"
508-
@test_broken summary(view(a, 1:2)) == "2-element view(::Vector{Q0f7}, 1:2) with eltype Q0f7"
509-
@test_broken summary(aa) == "1×2 Matrix{Fixed}"
507+
@test summary(a) == "2-element Vector{Q0f7}"
508+
@test summary(view(a, 1:2)) == "2-element view(::Vector{Q0f7}, 1:2) with eltype Q0f7"
509+
@test summary(aa) == "1×2 Matrix{Fixed}"
510510
else
511511
@test summary(a) == "2-element Array{Q0f7,1} with eltype Fixed{Int8,7}"
512512
@test summary(view(a, 1:2)) == "2-element view(::Array{Q0f7,1}, 1:2) with eltype Fixed{Int8,7}"
513-
@test_broken summary(aa) == "1×2 Array{Fixed,2}"
513+
@test summary(aa) == "1×2 Array{Fixed,2}"
514514
end
515515
end

test/normed.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,27 +473,27 @@ end
473473
@test String(take!(iob)) == "43691.3"
474474

475475
show(IOContext(iob, :typeinfo=>N16f16), n16f16)
476-
@test String(take!(iob)) == "43691.33333N16f16" # TODO: Consider removing suffix (issue #188)
476+
@test String(take!(iob)) == "43691.33333"
477477

478478
show(IOContext(iob, :typeinfo=>Normed), n16f16)
479479
@test String(take!(iob)) == "43691.33333N16f16"
480480

481481
show(iob, Normed{UInt128,64}(1.2345e6))
482-
@test_broken String(take!(iob)) == "Normed{UInt128,64}(1.2345e6)" # "N64f64" is not defined
482+
@test String(take!(iob)) == "Normed{UInt128,64}(1.2345e6)"
483483
end
484484

485485
@testset "summary" begin
486486
a = N0f8[0.2, 0.4]
487487
aa = Normed[0.2N0f8 0.4N0f16]
488488

489489
if VERSION >= v"1.6.0-DEV.356"
490-
@test_broken summary(a) == "2-element Vector{N0f8}"
491-
@test_broken summary(view(a, 1:2)) == "2-element view(::Vector{N0f8}, 1:2) with eltype N0f8"
492-
@test_broken summary(aa) == "1×2 Matrix{Normed}"
490+
@test summary(a) == "2-element Vector{N0f8}"
491+
@test summary(view(a, 1:2)) == "2-element view(::Vector{N0f8}, 1:2) with eltype N0f8"
492+
@test summary(aa) == "1×2 Matrix{Normed}"
493493
else
494494
@test summary(a) == "2-element Array{N0f8,1} with eltype Normed{UInt8,8}"
495495
@test summary(view(a, 1:2)) == "2-element view(::Array{N0f8,1}, 1:2) with eltype Normed{UInt8,8}"
496-
@test_broken summary(aa) == "1×2 Array{Normed,2}"
496+
@test summary(aa) == "1×2 Array{Normed,2}"
497497
end
498498
end
499499

0 commit comments

Comments
 (0)