Skip to content

Commit 861b036

Browse files
authored
Merge pull request #105 from JuliaMath/anj/amb
Fix Ambiguities
2 parents 25affc6 + d91de9e commit 861b036

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
julia 0.6
2+
Compat 0.35

src/fixed.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ struct Fixed{T <: Signed,f} <: FixedPoint{T, f}
77
Fixed{T, f}(i::Integer, _) where {T,f} = new{T, f}(i % T)
88
Fixed{T, f}(x) where {T,f} = convert(Fixed{T,f}, x)
99
Fixed{T, f}(x::Fixed{T,f}) where {T,f} = x
10+
Fixed{T, f}(x::Char) where {T,f} = throw(ArgumentError("Fixed cannot be constructed from a Char"))
11+
Fixed{T, f}(x::Complex) where {T,f} = Fixed{T, f}(convert(real(typeof(x)), x))
12+
Fixed{T, f}(x::Base.TwicePrecision) where {T,f} = Fixed{T, f}(convert(Float64, x))
1013
end
1114

1215
reinterpret(::Type{Fixed{T,f}}, x::T) where {T <: Signed,f} = Fixed{T,f}(x, 0)

src/normed.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ struct Normed{T<:Unsigned,f} <: FixedPoint{T,f}
77
Normed{T, f}(i::Integer,_) where {T,f} = new{T, f}(i%T) # for setting by raw representation
88
Normed{T, f}(x) where {T,f} = convert(Normed{T,f}, x)
99
Normed{T, f}(x::Normed{T,f}) where {T,f} = x
10+
Normed{T, f}(x::Char) where {T,f} = throw(ArgumentError("Normed cannot be constructed from a Char"))
11+
Normed{T, f}(x::Complex) where {T,f} = Normed{T, f}(convert(real(typeof(x)), x))
12+
Normed{T, f}(x::Base.TwicePrecision) where {T,f} = Normed{T, f}(convert(Float64, x))
1013
end
1114

1215
typechar(::Type{X}) where {X <: Normed} = 'N'

test/fixed.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Base.Test
2-
using FixedPointNumbers
1+
using FixedPointNumbers, Compat.Test
32

43
function test_op(fun::F, ::Type{T}, fx, fy, fxf, fyf, tol) where {F,T}
54
# Make sure that the result is representable
@@ -130,3 +129,9 @@ end
130129

131130
# issue #79
132131
@test realmin(Q11f4) == Q11f4(0.06)
132+
133+
# Test disambiguation constructors
134+
@test_throws ArgumentError Fixed{Int32,16}('a')
135+
@test_throws InexactError Fixed{Int32,16}(complex(1.0, 1.0))
136+
@test Fixed{Int32,16}(complex(1.0, 0.0)) == 1
137+
@test Fixed{Int32,16}(Base.TwicePrecision(1.0, 0.0)) == 1

test/normed.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using FixedPointNumbers
2-
using Base.Test
1+
using FixedPointNumbers, Compat.Test
32

43
@test reinterpret(N0f8, 0xa2).i === 0xa2
54
@test reinterpret(N6f10, 0x1fa2).i === 0x1fa2
@@ -314,3 +313,10 @@ elseif VERSION >= v"0.7.0-DEV.1790"
314313
@test summary(a) == "2-element Array{N0f8,1} with eltype FixedPointNumbers.Normed{UInt8,8}"
315314
@test summary(view(a, 1:2)) == "2-element view(::Array{N0f8,1}, 1:2) with eltype FixedPointNumbers.Normed{UInt8,8}"
316315
end
316+
317+
# Test disambiguation constructors
318+
@test_throws ArgumentError Normed{UInt32,16}('a')
319+
@test_throws InexactError Normed{UInt32,16}(complex(1.0, 1.0))
320+
@test Normed{UInt32,16}(complex(1.0, 0.0)) == 1
321+
@test Normed{UInt32,16}(Base.TwicePrecision(1.0, 0.0)) == 1
322+

test/runtests.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using FixedPointNumbers, Base.Test
1+
using FixedPointNumbers, Compat.Test
22

3-
if VERSION < v"0.7.0-"
4-
@test isempty(detect_ambiguities(FixedPointNumbers, Base, Core))
5-
end
3+
@test isempty(detect_ambiguities(FixedPointNumbers, Base, Core))
64

75
for f in ["normed.jl", "fixed.jl"]
86
println("Testing $f")

0 commit comments

Comments
 (0)