Skip to content

Commit d41a0a1

Browse files
committed
Centralize conversion to floating point through floattype
1 parent e3dee04 commit d41a0a1

5 files changed

Lines changed: 37 additions & 15 deletions

File tree

src/FixedPointNumbers.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Base: ==, <, <=, -, +, *, /, ~,
88
convert, promote_rule, show, showcompact, isinteger, abs, decompose,
99
isnan, isinf, isfinite,
1010
zero, one, typemin, typemax, realmin, realmax, eps, sizeof, reinterpret,
11-
trunc, round, floor, ceil, bswap,
11+
float, trunc, round, floor, ceil, bswap,
1212
div, fld, rem, mod, mod1, rem1, fld1, min, max, minmax,
1313
start, next, done, r_promote, reducedim_init
1414

@@ -55,6 +55,7 @@ reinterpret(x::FixedPoint) = x.i
5555
# predicates
5656
isinteger{T,f}(x::FixedPoint{T,f}) = (x.i&(1<<f-1)) == 0
5757

58+
# traits
5859
typemax{T<: FixedPoint}(::Type{T}) = T(typemax(rawtype(T)), 0)
5960
typemin{T<: FixedPoint}(::Type{T}) = T(typemin(rawtype(T)), 0)
6061
realmin{T<: FixedPoint}(::Type{T}) = typemin(T)
@@ -70,10 +71,25 @@ widen1(::Type{Int64}) = Int128
7071
widen1(::Type{UInt64}) = UInt128
7172
widen1(x::Integer) = x % widen1(typeof(x))
7273

74+
if VERSION <= v"0.5.0-dev+755"
75+
@generated function floattype{T,f}(::Type{FixedPoint{T,f}})
76+
f>22 ? :(Float64) : :(Float32)
77+
end
78+
else
79+
@pure function floattype{T,f}(::Type{FixedPoint{T,f}})
80+
f>22 ? Float64 : Float32
81+
end
82+
end
83+
floattype(x::FixedPoint) = floattype(supertype(typeof(x)))
84+
85+
7386
include("fixed.jl")
7487
include("ufixed.jl")
7588
include("deprecations.jl")
7689

90+
eps{T<:FixedPoint}(::Type{T}) = T(one(rawtype(T)),0)
91+
eps{T<:FixedPoint}(::T) = eps(T)
92+
sizeof{T<:FixedPoint}(::Type{T}) = sizeof(rawtype(T))
7793

7894
# Promotions for reductions
7995
const Treduce = Float64
@@ -115,7 +131,7 @@ function show{T,f}(io::IO, x::FixedPoint{T,f})
115131
print(io, ")")
116132
end
117133
const _log2_10 = 3.321928094887362
118-
showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(convert(Float64,x), ceil(Int,f/_log2_10)))
134+
showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(float(x), ceil(Int,f/_log2_10)))
119135

120136
@noinline function throw_converterror{T<:FixedPoint}(::Type{T}, x)
121137
n = 2^(8*sizeof(T))

src/fixed.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ typealias Fixed16 Fixed{Int32, 16}
1313

1414
rawtype{T,f}(::Type{Fixed{T,f}}) = T
1515
nbitsfrac{T,f}(::Type{Fixed{T,f}}) = f
16+
floattype{T<:Fixed}(::Type{T}) = floattype(supertype(T))
1617

1718
# basic operators
1819
-{T,f}(x::Fixed{T,f}) = Fixed{T,f}(-x.i,0)
@@ -37,6 +38,9 @@ convert{T,f}(::Type{Fixed{T,f}}, x::Rational) = Fixed{T,f}(x.num)/Fixed{T,f}(x.d
3738
rem{T,f}(x::Integer, ::Type{Fixed{T,f}}) = Fixed{T,f}(rem(x,T)<<f,0)
3839
rem{T,f}(x::Real, ::Type{Fixed{T,f}}) = Fixed{T,f}(rem(Integer(trunc(x)),T)<<f + rem(Integer(round(rem(x,1)*(1<<f))),T),0)
3940

41+
# convert{T,f}(::Type{AbstractFloat}, x::Fixed{T,f}) = convert(floattype(x), x)
42+
float(x::Fixed) = convert(floattype(x), x)
43+
4044
convert{T,f}(::Type{BigFloat}, x::Fixed{T,f}) =
4145
convert(BigFloat,x.i>>f) + convert(BigFloat,x.i&(1<<f - 1))/convert(BigFloat,1<<f)
4246
convert{TF<:AbstractFloat,T,f}(::Type{TF}, x::Fixed{T,f}) =

src/ufixed.jl

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ end
1111
rawtype{T,f}(::Type{UFixed{T,f}}) = T
1212
rawtype(x::Number) = rawtype(typeof(x))
1313
nbitsfrac{T,f}(::Type{UFixed{T,f}}) = f
14+
floattype{T<:UFixed}(::Type{T}) = floattype(supertype(T))
1415

1516
typealias UFixed8 UFixed{UInt8,8}
1617
typealias UFixed10 UFixed{UInt16,10}
@@ -65,6 +66,9 @@ rem{T<:UFixed}(x::T, ::Type{T}) = x
6566
rem{T<:UFixed}(x::UFixed, ::Type{T}) = reinterpret(T, _unsafe_trunc(rawtype(T), round((rawone(T)/rawone(x))*reinterpret(x))))
6667
rem{T<:UFixed}(x::Real, ::Type{T}) = reinterpret(T, _unsafe_trunc(rawtype(T), round(rawone(T)*x)))
6768

69+
# convert(::Type{AbstractFloat}, x::UFixed) = convert(floattype(x), x)
70+
float(x::UFixed) = convert(floattype(x), x)
71+
6872
convert(::Type{BigFloat}, x::UFixed) = reinterpret(x)*(1/BigFloat(rawone(x)))
6973
function convert{T<:AbstractFloat}(::Type{T}, x::UFixed)
7074
y = reinterpret(x)*(one(rawtype(x))/convert(T, rawone(x)))
@@ -81,17 +85,6 @@ eps{T<:UFixed}(::T) = eps(T)
8185
sizeof{T<:UFixed}(::Type{T}) = sizeof(rawtype(T))
8286
abs(x::UFixed) = x
8387

84-
# Arithmetic
85-
if VERSION <= v"0.5.0-dev+755"
86-
@generated function floattype{T,f}(::Type{UFixed{T,f}})
87-
f>22 ? :(Float64) : :(Float32)
88-
end
89-
else
90-
@pure function floattype{T,f}(::Type{UFixed{T,f}})
91-
f>22 ? Float64 : Float32
92-
end
93-
end
94-
9588
(-){T<:UFixed}(x::T) = T(-reinterpret(x), 0)
9689
(~){T<:UFixed}(x::T) = T(~reinterpret(x), 0)
9790

test/fixed.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ for T in (Float16, Float32, Float64, BigFloat)
9797
@test isa(y, T)
9898
end
9999

100+
# Floating-point conversions
101+
@test isa(float(one(Fixed{Int8,6})), Float32)
102+
@test isa(float(one(Fixed{Int32,18})), Float32)
103+
@test isa(float(one(Fixed{Int32,25})), Float64)
104+
100105
# Show
101106
x = Fixed{Int32,3}(0.25)
102107
iob = IOBuffer()

test/ufixed.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,15 @@ x = UFixed8(0b01010001, 0)
111111
@test ~x == UFixed8(0b10101110, 0)
112112
@test -x == 0xafuf8
113113

114+
@test isa(float(one(UFixed{UInt8,7})), Float32)
115+
@test isa(float(one(UFixed{UInt32,18})), Float32)
116+
@test isa(float(one(UFixed{UInt32,25})), Float64)
117+
114118
for T in (FixedPointNumbers.UF..., UF2...)
115119
x = T(0x10,0)
116120
y = T(0x25,0)
117-
fx = convert(FixedPointNumbers.floattype(T), x)
118-
fy = convert(FixedPointNumbers.floattype(T), y)
121+
fx = float(x)
122+
fy = float(y)
119123
@test y > x
120124
@test y != x
121125
@test typeof(x+y) == T

0 commit comments

Comments
 (0)