Skip to content

Commit be7969f

Browse files
committed
Improve consistency in printing
1 parent 2f2a34a commit be7969f

File tree

5 files changed

+125
-31
lines changed

5 files changed

+125
-31
lines changed

src/FixedPointNumbers.jl

Lines changed: 35 additions & 13 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
220223
# 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)
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: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,6 @@ end
358358
end
359359
end
360360

361-
@testset "Show" begin
362-
x = Fixed{Int32,5}(0.25)
363-
iob = IOBuffer()
364-
show(iob, x)
365-
str = String(take!(iob))
366-
@test str == "0.25Q26f5"
367-
@test eval(Meta.parse(str)) == x
368-
end
369-
370361
@testset "rand" begin
371362
for F in (Fixed{Int8,7}, Fixed{Int16,8}, Fixed{Int16,10}, Fixed{Int32,16})
372363
@test isa(rand(F), F)
@@ -433,3 +424,51 @@ end
433424
@test promote_type(Float32,Q0f7,Int) == Float32
434425
@test promote_type(Q0f7,Q1f6,Q2f5,Q3f4,Q4f3,Q5f2) == Fixed{Int128,7}
435426
end
427+
428+
@testset "show" begin
429+
iob = IOBuffer()
430+
q0f7 = reinterpret(Q0f7, signed(0xaa))
431+
show(iob, q0f7)
432+
str = String(take!(iob))
433+
@test str == "-0.672Q0f7"
434+
@test eval(Meta.parse(str)) === q0f7
435+
436+
q15f16 = reinterpret(Q15f16, signed(0xaaaaaaaa))
437+
show(iob, q15f16)
438+
str = String(take!(iob))
439+
@test str == "-21845.33334Q15f16"
440+
@test eval(Meta.parse(str)) === q15f16
441+
442+
show(IOContext(iob, :compact=>true), q15f16)
443+
@test String(take!(iob)) == "-21845.3"
444+
445+
show(IOContext(iob, :compact=>true, :typeinfo=>Q15f16), q15f16)
446+
@test String(take!(iob)) == "-21845.3"
447+
448+
show(IOContext(iob, :compact=>true, :typeinfo=>Fixed), q15f16)
449+
@test String(take!(iob)) == "-21845.3"
450+
451+
show(IOContext(iob, :typeinfo=>Q15f16), q15f16)
452+
@test String(take!(iob)) == "-21845.33334"
453+
454+
show(IOContext(iob, :typeinfo=>Normed), q15f16)
455+
@test String(take!(iob)) == "-21845.33334Q15f16"
456+
457+
show(iob, Fixed{Int128,64}(-1.2345e6))
458+
@test String(take!(iob)) == "Fixed{Int128,64}(-1.2345e6)"
459+
460+
# TODO: remove this test
461+
show(iob, reinterpret(Fixed{Int8,8}, signed(0xaa)))
462+
@test String(take!(iob)) == "Fixed{Int8,8}(-0.336)"
463+
end
464+
465+
@testset "summary" begin
466+
a = Q0f7[0.2, 0.4]
467+
if VERSION >= v"1.6.0-DEV.356"
468+
@test summary(a) == "2-element Vector{Q0f7}"
469+
@test summary(view(a, 1:2)) == "2-element view(::Vector{Q0f7}, 1:2) with eltype Q0f7"
470+
else
471+
@test summary(a) == "2-element Array{Q0f7,1} with eltype Fixed{Int8,7}"
472+
@test summary(view(a, 1:2)) == "2-element view(::Array{Q0f7,1}, 1:2) with eltype Fixed{Int8,7}"
473+
end
474+
end

test/normed.jl

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,47 @@ end
403403
end
404404

405405
@testset "show" begin
406-
x = reinterpret(N0f8, 0xaa)
407406
iob = IOBuffer()
408-
show(iob, x)
407+
n0f8 = reinterpret(N0f8, 0xaa)
408+
show(iob, n0f8)
409409
str = String(take!(iob))
410410
@test str == "0.667N0f8"
411-
@test eval(Meta.parse(str)) == x
411+
@test eval(Meta.parse(str)) === n0f8
412+
413+
n16f16 = reinterpret(N16f16, 0xaaaaaaaa)
414+
show(iob, n16f16)
415+
str = String(take!(iob))
416+
@test str == "43691.33333N16f16"
417+
@test eval(Meta.parse(str)) === n16f16
418+
419+
show(IOContext(iob, :compact=>true), n16f16)
420+
@test String(take!(iob)) == "43691.3"
421+
422+
show(IOContext(iob, :compact=>true, :typeinfo=>N16f16), n16f16)
423+
@test String(take!(iob)) == "43691.3"
424+
425+
show(IOContext(iob, :compact=>true, :typeinfo=>Normed), n16f16)
426+
@test String(take!(iob)) == "43691.3"
427+
428+
show(IOContext(iob, :typeinfo=>N16f16), n16f16)
429+
@test String(take!(iob)) == "43691.33333"
430+
431+
show(IOContext(iob, :typeinfo=>Normed), n16f16)
432+
@test String(take!(iob)) == "43691.33333N16f16"
433+
434+
show(iob, Normed{UInt128,64}(1.2345e6))
435+
@test String(take!(iob)) == "Normed{UInt128,64}(1.2345e6)"
436+
end
437+
438+
@testset "summary" begin
439+
a = N0f8[0.2, 0.4]
440+
if VERSION >= v"1.6.0-DEV.356"
441+
@test summary(a) == "2-element Vector{N0f8}"
442+
@test summary(view(a, 1:2)) == "2-element view(::Vector{N0f8}, 1:2) with eltype N0f8"
443+
else
444+
@test summary(a) == "2-element Array{N0f8,1} with eltype Normed{UInt8,8}"
445+
@test summary(view(a, 1:2)) == "2-element view(::Array{N0f8,1}, 1:2) with eltype Normed{UInt8,8}"
446+
end
412447
end
413448

414449
@testset "scaledual" begin
@@ -478,12 +513,6 @@ end
478513
@test Float16(1.0) % N0f16 === N0f16(1.0)
479514
end
480515

481-
@testset "summary" begin
482-
a = N0f8[0.2, 0.4]
483-
@test summary(a) == "2-element Array{N0f8,1} with eltype Normed{UInt8,8}"
484-
@test summary(view(a, 1:2)) == "2-element view(::Array{N0f8,1}, 1:2) with eltype Normed{UInt8,8}"
485-
end
486-
487516
@testset "disambiguation constructors" begin
488517
@test_throws ArgumentError Normed{UInt32,16}('a')
489518
@test_throws InexactError Normed{UInt32,16}(complex(1.0, 1.0))

0 commit comments

Comments
 (0)