Skip to content

Commit b72c0b6

Browse files
committed
Customize print to provide "plain" representation
`print` writes the number without the type suffix. This also unifies the return values of `show`/`showtype` to `nothing`.
1 parent 39e7d9b commit b72c0b6

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/FixedPointNumbers.jl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module FixedPointNumbers
22

33
import Base: ==, <, <=, -, +, *, /, ~, isapprox,
4-
convert, promote_rule, show, bitstring, abs, decompose,
4+
convert, promote_rule, print, show, bitstring, abs, decompose,
55
isnan, isinf, isfinite, isinteger,
66
zero, oneunit, one, typemin, typemax, floatmin, floatmax, eps, reinterpret,
77
big, rationalize, float, trunc, round, floor, ceil, bswap, clamp,
@@ -468,35 +468,40 @@ end
468468

469469
function _alias_symbol(::Type{X}) where {X <: FixedPoint}
470470
if @generated
471-
sym = string(alias_symbol(X))
472-
return :(Symbol($sym))
471+
return QuoteNode(alias_symbol(X))
473472
else
474473
return alias_symbol(X)
475474
end
476475
end
477476

477+
function print(io::IO, x::FixedPoint{T,f}) where {T,f}
478+
compact = get(io, :compact, false)::Bool
479+
log10_2 = 0.3010299956639812
480+
digits = min(ceil(Int, f * log10_2), compact ? 6 : typemax(Int))
481+
val = round(convert(Float64, x), digits=digits)
482+
print(io, val)
483+
end
484+
478485
@inline function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
479486
if hasalias(X)
480487
write(io, _alias_symbol(X))
481488
else
482489
print(io, X)
483490
end
484-
io
491+
return nothing
485492
end
486493

487494
function show(io::IO, x::FixedPoint{T,f}) where {T,f}
488495
compact = get(io, :compact, false)::Bool
489-
log10_2 = 0.3010299956639812
490-
digits = min(ceil(Int, f * log10_2), compact ? 6 : typemax(Int))
491-
val = round(convert(Float64, x), digits=digits)
492496
if compact || get(io, :typeinfo, Any) === typeof(x)
493-
show(io, val)
497+
print(io, x)
494498
elseif hasalias(typeof(x))
495-
show(io, val)
499+
print(io, x)
496500
showtype(io, typeof(x))
497501
else
498-
print(io, typeof(x), '(', val, ')')
502+
print(io, typeof(x), '(', x, ')')
499503
end
504+
return nothing
500505
end
501506

502507
if VERSION < v"1.6.0-DEV.356" # JuliaLang/julia#36107

test/fixed.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,10 @@ end
727727
@test str == "-0.672Q0f7"
728728
@test eval(Meta.parse(str)) === q0f7
729729

730+
print(iob, q0f7)
731+
str = String(take!(iob))
732+
@test str == "-0.672" # w/o type suffix
733+
730734
q15f16 = reinterpret(Q15f16, signed(0xaaaaaaaa))
731735
show(iob, q15f16)
732736
str = String(take!(iob))

test/normed.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,10 @@ end
699699
@test str == "0.667N0f8"
700700
@test eval(Meta.parse(str)) === n0f8
701701

702+
print(iob, n0f8)
703+
str = String(take!(iob))
704+
@test str == "0.667" # w/o type suffix
705+
702706
n16f16 = reinterpret(N16f16, 0xaaaaaaaa)
703707
show(iob, n16f16)
704708
str = String(take!(iob))

0 commit comments

Comments
 (0)