Skip to content

Commit be32eff

Browse files
committed
Compact systematic display of values
1 parent b8506b5 commit be32eff

File tree

5 files changed

+76
-20
lines changed

5 files changed

+76
-20
lines changed

src/FixedPointNumbers.jl

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,58 @@ using Compat
1818
# f => Number of Bytes reserved for fractional part
1919
abstract FixedPoint{T <: Integer, f} <: Real
2020

21+
22+
# printing
23+
const subscripts = ('','','','','','','','','','')
24+
const superscripts = ('','¹','²','³','','','','','','')
25+
function showtype{X<:FixedPoint}(io::IO, ::Type{X})
26+
print(io, typechar(X))
27+
f = nbitsfrac(X)
28+
mdigs = digits(sizeof(X)*8-f-signbits(X))
29+
fdigs = digits(f)
30+
for i = length(mdigs):-1:1
31+
print(io, superscripts[mdigs[i]+1])
32+
end
33+
for i = length(fdigs):-1:1
34+
print(io, subscripts[fdigs[i]+1])
35+
end
36+
io
37+
end
38+
function show{T,f}(io::IO, x::FixedPoint{T,f})
39+
showcompact(io, x)
40+
showtype(io, typeof(x))
41+
end
42+
const _log2_10 = 3.321928094887362
43+
showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(convert(Float64,x), ceil(Int,f/_log2_10)))
44+
2145
export
2246
FixedPoint,
2347
Fixed,
2448
UFixed,
49+
# "special" typealiases
2550
Fixed16,
2651
UFixed8,
52+
U8,
2753
UFixed10,
2854
UFixed12,
2955
UFixed14,
3056
UFixed16,
31-
# literal constructor constants
57+
U16,
58+
# Q and U typealiases are exported in separate source files
59+
# literal constructor constants
3260
uf8,
3361
uf10,
3462
uf12,
3563
uf14,
3664
uf16,
37-
# Functions
65+
# Functions
3866
scaledual
3967

4068
reinterpret(x::FixedPoint) = x.i
4169

70+
# construction using the (approximate) intended value, i.e., 0.8U⁰₈
71+
*{X<:FixedPoint}(x::Real, ::Type{X}) = X(x)
72+
4273
# comparison
4374
=={T <: FixedPoint}(x::T, y::T) = x.i == y.i
4475
<{T <: FixedPoint}(x::T, y::T) = x.i < y.i
@@ -62,11 +93,13 @@ widen1(::Type{Int64}) = Int128
6293
widen1(::Type{UInt64}) = UInt128
6394
widen1(x::Integer) = x % widen1(typeof(x))
6495

96+
# This IOBuffer is used during module definition to generate typealias names
97+
_iotypealias = IOBuffer()
98+
6599
include("fixed.jl")
66100
include("ufixed.jl")
67101
include("deprecations.jl")
68102

69-
70103
# Promotions for reductions
71104
const Treduce = Float64
72105
r_promote{T}(::typeof(@functorize(+)), x::FixedPoint{T}) = Treduce(x)
@@ -98,15 +131,4 @@ scaledual{T<:FixedPoint}(Tdual::Type, x::Union{T,AbstractArray{T}}) =
98131
scaledual{Tdual<:Number, T<:FixedPoint}(b::Tdual, x::Union{T,AbstractArray{T}}) =
99132
convert(Tdual, b/one(T)), reinterpret(rawtype(T), x)
100133

101-
# printing
102-
function show{T,f}(io::IO, x::FixedPoint{T,f})
103-
shorttype = typeof(x)<:UFixed ? "UFixed" : "Fixed"
104-
print(io, shorttype, "{", T, ",", f, "}")
105-
print(io, "(")
106-
showcompact(io, x)
107-
print(io, ")")
108-
end
109-
const _log2_10 = 3.321928094887362
110-
showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(convert(Float64,x), ceil(Int,f/_log2_10)))
111-
112134
end # module

src/fixed.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,23 @@ immutable Fixed{T <: Signed,f} <: FixedPoint{T, f}
99
Fixed(x) = convert(Fixed{T,f}, x)
1010
end
1111

12-
typealias Fixed16 Fixed{Int32, 16}
13-
1412
rawtype{T,f}(::Type{Fixed{T,f}}) = T
1513
nbitsfrac{T,f}(::Type{Fixed{T,f}}) = f
14+
typechar{X<:Fixed}(::Type{X}) = 'Q'
15+
signbits{X<:Fixed}(::Type{X}) = 1
16+
17+
for T in (Int8, Int16, Int32, Int64)
18+
for f in 0:sizeof(T)*8-1
19+
sym = Symbol(takebuf_string(showtype(_iotypealias, Fixed{T,f})))
20+
@eval begin
21+
typealias $sym Fixed{$T,$f}
22+
export $sym
23+
end
24+
end
25+
end
26+
27+
# ASCII typealiases
28+
typealias Fixed16 Fixed{Int32,16}
1629

1730
# basic operators
1831
-{T,f}(x::Fixed{T,f}) = Fixed{T,f}(-x.i,0)

src/ufixed.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,34 @@ end
1010

1111
rawtype{T,f}(::Type{UFixed{T,f}}) = T
1212
nbitsfrac{T,f}(::Type{UFixed{T,f}}) = f
13+
typechar{X<:UFixed}(::Type{X}) = 'U'
14+
signbits{X<:UFixed}(::Type{X}) = 0
15+
16+
for T in (UInt8, UInt16, UInt32, UInt64)
17+
for f in 0:sizeof(T)*8
18+
sym = Symbol(takebuf_string(showtype(_iotypealias, UFixed{T,f})))
19+
@eval begin
20+
typealias $sym UFixed{$T,$f}
21+
export $sym
22+
end
23+
end
24+
end
1325

26+
# ASCII typealiases
27+
typealias U8 UFixed{UInt8,8}
1428
typealias UFixed8 UFixed{UInt8,8}
1529
typealias UFixed10 UFixed{UInt16,10}
1630
typealias UFixed12 UFixed{UInt16,12}
1731
typealias UFixed14 UFixed{UInt16,14}
1832
typealias UFixed16 UFixed{UInt16,16}
33+
typealias U16 UFixed{UInt16,16}
1934

2035
const UF = (UFixed8, UFixed10, UFixed12, UFixed14, UFixed16)
2136

2237
reinterpret{T<:Unsigned, f}(::Type{UFixed{T,f}}, x::T) = UFixed{T,f}(x, 0)
2338

24-
# The next lines mimic the floating-point literal syntax "3.2f0"
39+
## The next lines mimic the floating-point literal syntax "3.2f0"
40+
# construction using a UInt, i.e., 0xccuf8
2541
immutable UFixedConstructor{T,f} end
2642
*{T,f}(n::Integer, ::UFixedConstructor{T,f}) = UFixed{T,f}(n,0)
2743
const uf8 = UFixedConstructor{UInt8,8}()

test/fixed.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ for T in (Float16, Float32, Float64, BigFloat)
8484
end
8585

8686
# Show
87-
x = Fixed{Int32,3}(0.25)
87+
x = Fixed{Int32,5}(0.25)
8888
iob = IOBuffer()
8989
show(iob, x)
9090
str = takebuf_string(iob)
91-
@test startswith(str, "Fixed{Int32,3}(")
91+
@test str == "0.25Q²⁶₅"
9292
@test eval(parse(str)) == x

test/ufixed.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ using Compat
1313
@test reinterpret(UFixed12, 0x1fa2) == 0x1fa2uf12
1414
@test reinterpret(UFixed14, 0x1fa2) == 0x1fa2uf14
1515
@test reinterpret(UFixed16, 0x1fa2) == 0x1fa2uf16
16+
@test 0.635U⁰₈ == UFixed8(0.635)
17+
@test 0.635U⁶₁₀ == UFixed10(0.635)
18+
@test 0.635U⁴₁₂ == UFixed12(0.635)
19+
@test 0.635U²₁₄ == UFixed14(0.635)
20+
@test 0.635U⁰₁₆ == UFixed16(0.635)
1621

1722
@test UFixed8(1.0) == 0xffuf8
1823
@test UFixed8(0.5) == 0x80uf8
@@ -159,7 +164,7 @@ x = 0xaauf8
159164
iob = IOBuffer()
160165
show(iob, x)
161166
str = takebuf_string(iob)
162-
@test startswith(str, "UFixed{UInt8,8}(")
167+
@test str == "0.667U⁰₈"
163168
@test eval(parse(str)) == x
164169

165170
# scaledual

0 commit comments

Comments
 (0)