Skip to content

Commit 57f878f

Browse files
committed
Improve consistency in printing
1 parent 7c90fb6 commit 57f878f

File tree

5 files changed

+57
-27
lines changed

5 files changed

+57
-27
lines changed

src/FixedPointNumbers.jl

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ reinterpret(::Type{X}, x::T) where {T <: Integer, X <: FixedPoint{T}} = X(x, 0)
4646
nbitsfrac(::Type{X}) where {T, f, X <: FixedPoint{T,f}} = f
4747
rawtype(::Type{X}) where {T, X <: FixedPoint{T}} = T
4848

49+
# traits based on static parameters
50+
signbits(::Type{X}) where {T, X <: FixedPoint{T}} = T <: Unsigned ? 0 : 1
51+
nbitsint(::Type{X}) where {X <: FixedPoint} = bitwidth(X) - nbitsfrac(X) - signbits(X)
52+
4953
# construction using the (approximate) intended value, i.e., N0f8
5054
*(x::Real, ::Type{X}) where {X <: FixedPoint} = _convert(X, x)
5155

@@ -216,27 +220,49 @@ function length(r::StepRange{<:FixedPoint})
216220
return div((stop - start) + step, step)
217221
end
218222

223+
hasalias(::Type) = false
224+
hasalias(::Type{X}) where {T<:NotBiggerThanInt64, f, X<:FixedPoint{T,f}} = nbitsfrac(X) isa Int
225+
219226
# 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)
227+
# before we include "src/fixed.jl" / "src/normed.jl".
228+
@inline function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
229+
if hasalias(X)
230+
f = nbitsfrac(X)
231+
m = nbitsint(X)
232+
write(io, typechar(X))
233+
m > 9 && write(io, Char(m ÷ 10 + 0x30))
234+
write(io, Char(m % 10 + 0x30), 'f')
235+
f > 9 && write(io, Char(f ÷ 10 + 0x30))
236+
write(io, Char(f % 10 + 0x30))
237+
else
238+
print(io, X)
239+
end
226240
io
227241
end
242+
228243
function show(io::IO, x::FixedPoint{T,f}) where {T,f}
244+
compact = get(io, :compact, false)
229245
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))
246+
digits = min(ceil(Int, f * log10_2), compact ? 6 : typemax(Int))
247+
val = round(convert(Float64, x), digits=digits)
248+
if compact || get(io, :typeinfo, nothing) === typeof(x)
249+
show(io, val)
250+
elseif hasalias(typeof(x))
251+
show(io, val)
252+
showtype(io, typeof(x))
253+
else
254+
print(io, typeof(x), '(', val, ')')
255+
end
232256
end
233257

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)
258+
if VERSION < v"1.6.0-DEV.356"
259+
function Base.showarg(io::IO, a::Array{X}, toplevel) where {X<:FixedPoint}
260+
toplevel || print(io, "::")
261+
print(io, "Array{")
262+
showtype(io, X)
263+
print(io, ",$(ndims(a))}")
264+
toplevel && hasalias(X) && print(io, " with eltype ", X)
265+
end
240266
end
241267

242268
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)