Skip to content

Commit 1527800

Browse files
committed
Improve consistency in printing
1 parent 56760f3 commit 1527800

File tree

5 files changed

+53
-27
lines changed

5 files changed

+53
-27
lines changed

src/FixedPointNumbers.jl

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,27 +216,49 @@ function length(r::StepRange{<:FixedPoint})
216216
return div((stop - start) + step, step)
217217
end
218218

219+
hasalias(::Type) = false
220+
hasalias(::Type{X}) where {T<:NotBiggerThanInt64, f, X<:FixedPoint{T,f}} = nbitsfrac(X) isa Int
221+
219222
# Printing. These are used to generate type-symbols, so we need them
220-
# before we include any files.
221-
function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
222-
print(io, typechar(X))
223-
f = nbitsfrac(X)
224-
m = bitwidth(X)-f-signbits(X)
225-
print(io, m, 'f', f)
223+
# before we include "src/fixed.jl" / "src/normed.jl".
224+
@inline function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
225+
if hasalias(X)
226+
f = nbitsfrac(X)
227+
m = bitwidth(X) - f - signbits(X)
228+
write(io, typechar(X))
229+
m > 9 && write(io, Char(m ÷ 10 + 0x30))
230+
write(io, Char(m % 10 + 0x30), 'f')
231+
f > 9 && write(io, Char(f ÷ 10 + 0x30))
232+
write(io, Char(f % 10 + 0x30))
233+
else
234+
print(io, X)
235+
end
226236
io
227237
end
238+
228239
function show(io::IO, x::FixedPoint{T,f}) where {T,f}
240+
compact = get(io, :compact, false)
229241
log10_2 = 0.3010299956639812
230-
show(io, round(convert(Float64,x), digits=ceil(Int, f * log10_2)))
231-
get(io, :compact, false) || showtype(io, typeof(x))
242+
digits = min(ceil(Int, f * log10_2), compact ? 6 : typemax(Int))
243+
val = round(convert(Float64, x), digits=digits)
244+
if compact || get(io, :typeinfo, nothing) === typeof(x)
245+
show(io, val)
246+
elseif hasalias(typeof(x))
247+
show(io, val)
248+
showtype(io, typeof(x))
249+
else
250+
print(io, typeof(x), '(', val, ')')
251+
end
232252
end
233253

234-
function Base.showarg(io::IO, a::Array{T}, toplevel) where {T<:FixedPoint}
235-
toplevel || print(io, "::")
236-
print(io, "Array{")
237-
showtype(io, T)
238-
print(io, ",$(ndims(a))}")
239-
toplevel && print(io, " with eltype ", T)
254+
if VERSION < v"1.6.0-DEV.356"
255+
function Base.showarg(io::IO, a::Array{X}, toplevel) where {X<:FixedPoint}
256+
toplevel || print(io, "::")
257+
print(io, "Array{")
258+
showtype(io, X)
259+
print(io, ",$(ndims(a))}")
260+
toplevel && hasalias(X) && print(io, " with eltype ", X)
261+
end
240262
end
241263

242264
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
signbits(::Type{X}) where {X <: Fixed} = 1
2932

src/utilities.jl

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

1919
const ShorterThanInt = Int === Int32 ? ShortInts : Union{ShortInts, Int32, UInt32}
2020
const NotBiggerThanInt = Union{ShorterThanInt, Int, UInt}
21+
const NotBiggerThanInt64 = Union{ShortInts, Int32, UInt32, Int64, UInt64}
2122
const SShorterThanInt = typeintersect(ShorterThanInt, Signed)
2223
const UShorterThanInt = typeintersect(ShorterThanInt, Unsigned)
2324

test/fixed.jl

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

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

454454
show(IOContext(iob, :typeinfo=>Normed), q15f16)
455455
@test String(take!(iob)) == "-21845.33334Q15f16"
456456

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

460460
# TODO: remove this test
461461
show(iob, reinterpret(Fixed{Int8,8}, signed(0xaa)))
462-
@test_broken String(take!(iob)) == "Fixed{Int8,8}(-0.336)" # "Q-1f8" is invalid
462+
@test String(take!(iob)) == "Fixed{Int8,8}(-0.336)"
463463
end
464464

465465
@testset "summary" begin
466466
a = Q0f7[0.2, 0.4]
467467
aa = Fixed[0.2Q0f7 0.4Q0f15]
468468

469469
if VERSION >= v"1.6.0-DEV.356"
470-
@test_broken summary(a) == "2-element Vector{Q0f7}"
471-
@test_broken summary(view(a, 1:2)) == "2-element view(::Vector{Q0f7}, 1:2) with eltype Q0f7"
472-
@test_broken summary(aa) == "1×2 Matrix{Fixed}"
470+
@test summary(a) == "2-element Vector{Q0f7}"
471+
@test summary(view(a, 1:2)) == "2-element view(::Vector{Q0f7}, 1:2) with eltype Q0f7"
472+
@test summary(aa) == "1×2 Matrix{Fixed}"
473473
else
474474
@test summary(a) == "2-element Array{Q0f7,1} with eltype Fixed{Int8,7}"
475475
@test summary(view(a, 1:2)) == "2-element view(::Array{Q0f7,1}, 1:2) with eltype Fixed{Int8,7}"
476-
@test_broken summary(aa) == "1×2 Array{Fixed,2}"
476+
@test summary(aa) == "1×2 Array{Fixed,2}"
477477
end
478478
end

test/normed.jl

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

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

431431
show(IOContext(iob, :typeinfo=>Normed), n16f16)
432432
@test String(take!(iob)) == "43691.33333N16f16"
433433

434434
show(iob, Normed{UInt128,64}(1.2345e6))
435-
@test_broken String(take!(iob)) == "Normed{UInt128,64}(1.2345e6)" # "N64f64" is not defined
435+
@test String(take!(iob)) == "Normed{UInt128,64}(1.2345e6)"
436436
end
437437

438438
@testset "summary" begin
439439
a = N0f8[0.2, 0.4]
440440
aa = Normed[0.2N0f8 0.4N0f16]
441441

442442
if VERSION >= v"1.6.0-DEV.356"
443-
@test_broken summary(a) == "2-element Vector{N0f8}"
444-
@test_broken summary(view(a, 1:2)) == "2-element view(::Vector{N0f8}, 1:2) with eltype N0f8"
445-
@test_broken summary(aa) == "1×2 Matrix{Normed}"
443+
@test summary(a) == "2-element Vector{N0f8}"
444+
@test summary(view(a, 1:2)) == "2-element view(::Vector{N0f8}, 1:2) with eltype N0f8"
445+
@test summary(aa) == "1×2 Matrix{Normed}"
446446
else
447447
@test summary(a) == "2-element Array{N0f8,1} with eltype Normed{UInt8,8}"
448448
@test summary(view(a, 1:2)) == "2-element view(::Array{N0f8,1}, 1:2) with eltype Normed{UInt8,8}"
449-
@test_broken summary(aa) == "1×2 Array{Normed,2}"
449+
@test summary(aa) == "1×2 Array{Normed,2}"
450450
end
451451
end
452452

0 commit comments

Comments
 (0)