Skip to content

Commit 80e602a

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

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/FixedPointNumbers.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ export
3434
uf12,
3535
uf14,
3636
uf16,
37-
# Functions
37+
U₈⁸,
38+
U₁₀¹⁶,
39+
U₁₂¹⁶,
40+
U₁₄¹⁶,
41+
U₁₆¹⁶,
42+
# Functions
3843
scaledual
3944

4045
reinterpret(x::FixedPoint) = x.i
@@ -99,12 +104,14 @@ scaledual{Tdual<:Number, T<:FixedPoint}(b::Tdual, x::Union{T,AbstractArray{T}})
99104
convert(Tdual, b/one(T)), reinterpret(rawtype(T), x)
100105

101106
# printing
107+
const subscripts = ('','','','','','','','','','')
108+
const superscripts = ('','¹','²','³','','','','','','')
102109
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, "(")
106110
showcompact(io, x)
107-
print(io, ")")
111+
shorttype = typeof(x)<:UFixed ? "U" : "F"
112+
fstr = join(map(n->subscripts[n+1], reverse(digits(f))), "")
113+
Tstr = join(map(n->superscripts[n+1], reverse(digits(sizeof(T)*8))), "")
114+
print(io, shorttype, fstr, Tstr)
108115
end
109116
const _log2_10 = 3.321928094887362
110117
showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(convert(Float64,x), ceil(Int,f/_log2_10)))

src/ufixed.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const UF = (UFixed8, UFixed10, UFixed12, UFixed14, UFixed16)
2121

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

24-
# The next lines mimic the floating-point literal syntax "3.2f0"
24+
## The next lines mimic the floating-point literal syntax "3.2f0"
25+
# construction using a UInt, i.e., 0xccuf8
2526
immutable UFixedConstructor{T,f} end
2627
*{T,f}(n::Integer, ::UFixedConstructor{T,f}) = UFixed{T,f}(n,0)
2728
const uf8 = UFixedConstructor{UInt8,8}()
@@ -30,6 +31,15 @@ const uf12 = UFixedConstructor{UInt16,12}()
3031
const uf14 = UFixedConstructor{UInt16,14}()
3132
const uf16 = UFixedConstructor{UInt16,16}()
3233

34+
# construction using the actual value, i.e., 0.8U₈⁸
35+
immutable UValueConstructor{T,f} end
36+
*{T,f}(x::Real, ::UValueConstructor{T,f}) = UFixed{T,f}(x)
37+
const U₈⁸ = UValueConstructor{UInt8,8}()
38+
const U₁₀¹⁶ = UValueConstructor{UInt16,10}()
39+
const U₁₂¹⁶ = UValueConstructor{UInt16,12}()
40+
const U₁₄¹⁶ = UValueConstructor{UInt16,14}()
41+
const U₁₆¹⁶ = UValueConstructor{UInt16,16}()
42+
3343
zero{T,f}(::Type{UFixed{T,f}}) = UFixed{T,f}(zero(T),0)
3444
@generated function one{T<:UFixed}(::Type{T})
3545
f = 2^nbitsfrac(T)-1

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)