From 9ee51d2f9fcc4a2963a6691218cebef3d93ac916 Mon Sep 17 00:00:00 2001 From: jishnub Date: Sat, 13 Jun 2020 23:14:07 +0530 Subject: [PATCH 1/2] Add methods to resolve ambiguity with Base Added methods to // to resolve ambiguity involving Complex numbers and Arrays --- src/HalfIntegers.jl | 5 +++++ test/runtests.jl | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/HalfIntegers.jl b/src/HalfIntegers.jl index 7191e12..ad03dc8 100644 --- a/src/HalfIntegers.jl +++ b/src/HalfIntegers.jl @@ -90,6 +90,11 @@ Base.://(x::HalfInteger, y::HalfInteger) = twice(x)//twice(y) Base.://(x::HalfInteger, y) = twice(x)//twice(y) Base.://(x, y::HalfInteger) = twice(x)//twice(y) +# Ambiguity resolution with Base +Base.://(x::HalfInteger, y::Complex) = twice(x)//twice(y) +Base.://(x::Complex, y::HalfInteger) = twice(x)//twice(y) +Base.://(x::AbstractArray, y::HalfInteger) = twice(x)//twice(y) + Base.:^(x::Real, y::HalfInteger) = x^float(y) Base.:^(::Irrational{:ℯ}, x::HalfInteger) = exp(x) diff --git a/test/runtests.jl b/test/runtests.jl index 4b68e7f..1676b4e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,6 +7,9 @@ halfuinttypes = (:HalfUInt, :HalfUInt8, :HalfUInt16, :HalfUInt32, :HalfUInt64, : ==ₜ(x, y) = x === y ==ₜ(x::T, y::T) where T<:Union{BigInt,BigFloat,BigHalfInt,Rational{BigInt},Complex{BigInt},Complex{BigFloat},Complex{BigHalfInt},Complex{Rational{BigInt}}} = x == y +==ₜ(x::AbstractArray, y::AbstractArray) = (x == y) && (typeof(x) == typeof(y)) + +@test isempty(Test.detect_ambiguities(HalfIntegers, Base)) @testset "Constructors" begin for T in (halfinttypes..., halfuinttypes..., :BigHalfInt) @@ -671,6 +674,13 @@ end @test 2 // BigHalfInt(2) ==ₜ Rational{BigInt}(1//1) @test BigInt(2) // BigHalfInt(2) ==ₜ Rational{BigInt}(1//1) @test (4//3) // BigHalfInt(3/2) ==ₜ Rational{BigInt}(8//9) + + # Extra tests for ambiguity resolution with Base + for T in (halfinttypes..., halfuinttypes..., BigHalfInt) + @eval @test [1]//$T(2) ==ₜ [1//$T(2)] + @eval @test Complex(2,0)//$T(2) ==ₜ Complex(2//$T(2),0//1) + @eval @test $T(2)//Complex(2,0) ==ₜ Complex($T(2)//2,0//1) + end end @testset "div/rem/mod" begin From 6a07724f92c94f0ab4914e4a607ef40192f8f78d Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Sun, 14 Jun 2020 15:43:22 +0530 Subject: [PATCH 2/2] Elementwise rational division with AbstractArrays Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> --- src/HalfIntegers.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HalfIntegers.jl b/src/HalfIntegers.jl index ad03dc8..16e69ba 100644 --- a/src/HalfIntegers.jl +++ b/src/HalfIntegers.jl @@ -93,7 +93,7 @@ Base.://(x, y::HalfInteger) = twice(x)//twice(y) # Ambiguity resolution with Base Base.://(x::HalfInteger, y::Complex) = twice(x)//twice(y) Base.://(x::Complex, y::HalfInteger) = twice(x)//twice(y) -Base.://(x::AbstractArray, y::HalfInteger) = twice(x)//twice(y) +Base.://(x::AbstractArray, y::HalfInteger) = twice.(x) .// twice(y) Base.:^(x::Real, y::HalfInteger) = x^float(y) Base.:^(::Irrational{:ℯ}, x::HalfInteger) = exp(x)