Skip to content

Commit b58c7f0

Browse files
authored
Merge pull request #43 from JeffBezanson/teh/float16
Make conversions work for Float16 (and speed tests)
2 parents f1bafe3 + 204723a commit b58c7f0

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

src/ufixed.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ ufixed16(x) = convert(UFixed16, x)
6767

6868

6969
convert(::Type{BigFloat}, x::UFixed) = reinterpret(x)*(1/BigFloat(rawone(x)))
70-
convert{T<:AbstractFloat}(::Type{T}, x::UFixed) = reinterpret(x)*(1/convert(T, rawone(x)))
70+
function convert{T<:AbstractFloat}(::Type{T}, x::UFixed)
71+
y = reinterpret(x)*(1/convert(T, rawone(x)))
72+
convert(T, y) # needed for types like Float16 which promote arithmetic to Float32
73+
end
7174
convert(::Type{Bool}, x::UFixed) = x == zero(x) ? false : true
7275
convert{T<:Integer}(::Type{T}, x::UFixed) = convert(T, x*(1/one(T)))
7376
convert{Ti<:Integer}(::Type{Rational{Ti}}, x::UFixed) = convert(Ti, reinterpret(x))//convert(Ti, rawone(x))

test/fixed.jl

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
using Base.Test
22
using FixedPointNumbers
33

4+
function test_op{F,T}(fun::F, ::Type{T}, fx, fy, fxf, fyf, tol)
5+
# Make sure that the result is representable
6+
(typemin(T) <= fun(fxf, fyf) <= typemax(T)) || return nothing
7+
@assert abs(fun(fx, fy) - convert(T, fun(fxf, fyf))) <= tol
8+
@assert abs(convert(Float64, fun(fx, fy)) - fun(fxf, fyf)) <= tol
9+
end
10+
411
function test_fixed{T}(::Type{T}, f)
512
values = [-10:0.01:10; -180:.01:-160; 160:.01:180]
613
tol = 2.0^-f
@@ -29,21 +36,16 @@ function test_fixed{T}(::Type{T}, f)
2936
fy = convert(T,y)
3037
fyf = convert(Float64, fy)
3138

32-
@test fx==fy || x!=y
33-
@test fx<fy || x>=y
34-
@test fx<=fy || x>y
39+
@assert fx==fy || x!=y
40+
@assert fx<fy || x>=y
41+
@assert fx<=fy || x>y
3542

36-
for fun in [+, -, *, /]
37-
# Make sure that the result is representable
38-
if !(typemin(T) <= fun(fxf, fyf) <= typemax(T))
39-
continue
40-
elseif (fun == /) && fy != 0
41-
@test abs(fun(fx, fy) - convert(T, fun(fxf, fyf))) <= tol
42-
@test abs(convert(Float64, fun(fx, fy)) - fun(fxf, fyf)) <= tol
43-
end
44-
end
43+
test_op(+, T, fx, fy, fxf, fyf, tol)
44+
test_op(-, T, fx, fy, fxf, fyf, tol)
45+
test_op(*, T, fx, fy, fxf, fyf, tol)
46+
fy != 0 && test_op(/, T, fx, fy, fxf, fyf, tol)
4547

46-
@test isequal(fx,fy) == isequal(hash(fx),hash(fy))
48+
@assert isequal(fx,fy) == isequal(hash(fx),hash(fy))
4749
end
4850
end
4951
end
@@ -66,3 +68,9 @@ a = F6[1.2, 1.4]
6668
acmp = Float64(a[1])*Float64(a[2])
6769
@test prod(a) == acmp
6870
@test prod(a, 1) == [acmp]
71+
72+
x = Fixed{Int8,8}(0.3)
73+
for T in (Float16, Float32, Float64, BigFloat)
74+
y = convert(T, x)
75+
@test isa(y, T)
76+
end

test/ufixed.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,9 @@ a = UFixed14[3.2, 2.4]
160160
acmp = Float64(a[1])*Float64(a[2])
161161
@test prod(a) == acmp
162162
@test prod(a, 1) == [acmp]
163+
164+
x = UFixed8(0.3)
165+
for T in (Float16, Float32, Float64, BigFloat)
166+
y = convert(T, x)
167+
@test isa(y, T)
168+
end

0 commit comments

Comments
 (0)