Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down