diff --git a/src/FixedPointNumbers.jl b/src/FixedPointNumbers.jl index 67b38160..32981b5e 100644 --- a/src/FixedPointNumbers.jl +++ b/src/FixedPointNumbers.jl @@ -1,7 +1,7 @@ module FixedPointNumbers import Base: ==, <, <=, -, +, *, /, ~, isapprox, - convert, promote_rule, show, bitstring, abs, decompose, + convert, promote_rule, print, show, bitstring, abs, decompose, isnan, isinf, isfinite, isinteger, zero, oneunit, one, typemin, typemax, floatmin, floatmax, eps, reinterpret, big, rationalize, float, trunc, round, floor, ceil, bswap, clamp, @@ -468,35 +468,40 @@ end function _alias_symbol(::Type{X}) where {X <: FixedPoint} if @generated - sym = string(alias_symbol(X)) - return :(Symbol($sym)) + return QuoteNode(alias_symbol(X)) else return alias_symbol(X) end end +function print(io::IO, x::FixedPoint{T,f}) where {T,f} + compact = get(io, :compact, false)::Bool + log10_2 = 0.3010299956639812 + digits = min(ceil(Int, f * log10_2), compact ? 6 : typemax(Int)) + val = round(convert(Float64, x), digits=digits) + print(io, val) +end + @inline function showtype(io::IO, ::Type{X}) where {X <: FixedPoint} if hasalias(X) write(io, _alias_symbol(X)) else print(io, X) end - io + return nothing end function show(io::IO, x::FixedPoint{T,f}) where {T,f} compact = get(io, :compact, false)::Bool - log10_2 = 0.3010299956639812 - digits = min(ceil(Int, f * log10_2), compact ? 6 : typemax(Int)) - val = round(convert(Float64, x), digits=digits) if compact || get(io, :typeinfo, Any) === typeof(x) - show(io, val) + print(io, x) elseif hasalias(typeof(x)) - show(io, val) + print(io, x) showtype(io, typeof(x)) else - print(io, typeof(x), '(', val, ')') + print(io, typeof(x), '(', x, ')') end + return nothing end if VERSION < v"1.6.0-DEV.356" # JuliaLang/julia#36107 diff --git a/test/fixed.jl b/test/fixed.jl index c66a9495..ff7a832b 100644 --- a/test/fixed.jl +++ b/test/fixed.jl @@ -727,6 +727,10 @@ end @test str == "-0.672Q0f7" @test eval(Meta.parse(str)) === q0f7 + print(iob, q0f7) + str = String(take!(iob)) + @test str == "-0.672" # w/o type suffix + q15f16 = reinterpret(Q15f16, signed(0xaaaaaaaa)) show(iob, q15f16) str = String(take!(iob)) diff --git a/test/normed.jl b/test/normed.jl index c5328299..fd48df95 100644 --- a/test/normed.jl +++ b/test/normed.jl @@ -699,6 +699,10 @@ end @test str == "0.667N0f8" @test eval(Meta.parse(str)) === n0f8 + print(iob, n0f8) + str = String(take!(iob)) + @test str == "0.667" # w/o type suffix + n16f16 = reinterpret(N16f16, 0xaaaaaaaa) show(iob, n16f16) str = String(take!(iob))