Wield behavior of multiple dispatch

1115
Tue, 29 Nov 2022 10:54:15 +0000
General Usage

In Julia 1.8.3. Given the following two type signatures.

(x::AbstractFloat, y::Number)
(x::Union{Float64, ComplexF64}, y::Union{Float64, ComplexF64})

I would expect there being an ambiguity error if the input type is (::Float64, ::Float64).

But

julia> FloatAndComplex64 = Union{Float64, ComplexF64}
Union{Float64, ComplexF64}

julia> begin
               function roughly_equal(x::AbstractFloat, y::Number)
                       @info "(::AbstractFloat, ::Number)"
                       -10 * eps(x) < x - y < 10 * eps(x)
               end
               function roughly_equal(x::Union{Float64, ComplexF64}, y::Union{Float64, ComplexF64})
                       @info "(::Union{Float64, ComplexF64}, ::Union{Float64, ComplexF64})"
                       abs(x - y) < 10 * eps(y)
               end
       end
roughly_equal (generic function with 4 methods)

julia> roughly_equal(3.0, 3.0)
[ Info: (::Union{Float64, ComplexF64}, ::Union{Float64, ComplexF64})
true

7 posts - 4 participants