@@ -46,6 +46,10 @@ reinterpret(::Type{X}, x::T) where {T <: Integer, X <: FixedPoint{T}} = X(x, 0)
4646nbitsfrac (:: Type{X} ) where {T, f, X <: FixedPoint{T,f} } = f
4747rawtype (:: 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)
217221end
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
227241end
242+
228243function 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
232256end
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
240266end
241267
242268include (" fixed.jl" )
0 commit comments