Skip to content

Commit 59f8341

Browse files
committed
Fix ambiguities
1 parent a63b5cc commit 59f8341

4 files changed

Lines changed: 10 additions & 1 deletion

File tree

src/fixed.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ convert{TF<:AbstractFloat,T,f}(::Type{TF}, x::Fixed{T,f}) =
5555
convert(TF,x.i>>f) + convert(TF,x.i&(1<<f - 1))/convert(TF,1<<f)
5656

5757
convert{T,f}(::Type{Bool}, x::Fixed{T,f}) = x.i!=0
58+
function convert{T,f}(::Type{Integer}, x::Fixed{T,f})
59+
isinteger(x) || throw(InexactError())
60+
convert(Integer, x.i>>f)
61+
end
5862
function convert{TI<:Integer, T,f}(::Type{TI}, x::Fixed{T,f})
5963
isinteger(x) || throw(InexactError())
6064
convert(TI, x.i>>f)

src/normed.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ zero(x::Normed) = zero(typeof(x))
3232
rawone(v) = reinterpret(one(v))
3333

3434
# Conversions
35-
convert{T<:Unsigned,f}(::Type{Normed{T,f}}, x::Normed{T,f}) = x
35+
convert{U<:Normed}(::Type{U}, x::U) = x
3636
convert{T1<:Unsigned,T2<:Unsigned,f}(::Type{Normed{T1,f}}, x::Normed{T2,f}) = Normed{T1,f}(convert(T1, x.i), 0)
3737
function convert{T<:Unsigned,T2<:Unsigned,f}(::Type{Normed{T,f}}, x::Normed{T2})
3838
U = Normed{T,f}
@@ -67,6 +67,7 @@ function convert{T<:AbstractFloat}(::Type{T}, x::Normed)
6767
convert(T, y) # needed for types like Float16 which promote arithmetic to Float32
6868
end
6969
convert(::Type{Bool}, x::Normed) = x == zero(x) ? false : true
70+
convert(::Type{Integer}, x::Normed) = convert(Integer, x*1.0)
7071
convert{T<:Integer}(::Type{T}, x::Normed) = convert(T, x*(1/one(T)))
7172
convert{Ti<:Integer}(::Type{Rational{Ti}}, x::Normed) = convert(Ti, reinterpret(x))//convert(Ti, rawone(x))
7273
convert(::Type{Rational}, x::Normed) = reinterpret(x)//rawone(x)

test/fixed.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ for T in (Float16, Float32, Float64, BigFloat)
103103
@test isa(y, T)
104104
end
105105

106+
@test convert(Int, Q1f6(1)) === 1
107+
@test convert(Integer, Q1f6(1)) === Int8(1)
108+
106109
# Floating-point conversions
107110
@test isa(float(one(Fixed{Int8,6})), Float32)
108111
@test isa(float(one(Fixed{Int32,18})), Float64)

test/normed.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ for T in (FixedPointNumbers.UF..., UF2...)
8989
@test convert(Bool, one(T)) == true
9090
@test convert(Bool, convert(T, 0.2)) == true
9191
@test convert(Int, one(T)) == 1
92+
@test convert(Integer, one(T)) == 1
9293
@test convert(Rational, one(T)) == 1
9394
end
9495
@test convert(Rational, convert(N0f8, 0.5)) == 0x80//0xff

0 commit comments

Comments
 (0)