Skip to content

Commit

Permalink
Extend conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierHnt committed Oct 10, 2024
1 parent 429ad16 commit eb865b2
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
6 changes: 5 additions & 1 deletion ext/IntervalArithmeticForwardDiffExt.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module IntervalArithmeticForwardDiffExt

using IntervalArithmetic, ForwardDiff
using ForwardDiff: Dual, , value, partials
using ForwardDiff: Dual, Partials, , value, partials

#

Expand Down Expand Up @@ -70,4 +70,8 @@ function Base.:(^)(x::ExactReal, y::Dual{<:Ty}) where {Ty}
end
end

# resolve ambiguity

Base.convert(::Type{Dual{T,V,N}}, x::ExactReal) where {T,V,N} = Dual{T}(V(x), zero(Partials{N,V}))

end
4 changes: 2 additions & 2 deletions src/intervals/construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ isguaranteed(x::Complex{<:Interval}) = isguaranteed(real(x)) & isguaranteed(imag
isguaranteed(::Number) = false

Interval{T}(x::Interval) where {T<:NumTypes} = convert(Interval{T}, x) # needed to resolve method ambiguity
# Interval{T}(x) where {T<:NumTypes} = convert(Interval{T}, x)
# Interval{T}(x::Interval{T}) where {T<:NumTypes} = convert(Interval{T}, x) # needed to resolve method ambiguity
Interval{T}(x::Real) where {T<:NumTypes} = convert(Interval{T}, x)
Interval(x::Real) = convert(Interval{promote_numtype(typeof(x), typeof(x))}, x)

#

Expand Down
3 changes: 0 additions & 3 deletions src/intervals/exact_literals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ Base.promote_rule(::Type{ExactReal{T}}, ::Type{Interval{S}}) where {T<:Real,S<:N

# to Real

# allows Interval{<:NumTypes}(::ExactReal)
(::Type{T})(x::ExactReal) where {T<:Real} = convert(T, x)

Base.convert(::Type{T}, x::ExactReal) where {T<:Real} = convert(T, x.value)

Base.promote_rule(::Type{T}, ::Type{ExactReal{S}}) where {T<:Real,S<:Real} =
Expand Down
6 changes: 3 additions & 3 deletions test/interval_tests/construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ end
@test_throws MethodError BareInterval(1, 2)
@test_throws MethodError BareInterval{Float64}(1, 2)

@test_throws MethodError Interval(1)
@test_throws MethodError Interval{Float64}(1)
@test !isguaranteed(Interval(1))
@test !isguaranteed(Interval{Float64}(1))
@test_throws MethodError Interval(1, 2)
@test_throws MethodError Interval{Float64}(1, 2)

Expand Down Expand Up @@ -163,7 +163,7 @@ end
i = interval(IS.Interval(0.1, 2))
@test isequal_interval(i, interval(0.1, 2.)) && !isguaranteed(i)
@test interval(Float64, IS.Interval(0.1, 2)) === i

i = interval(IS.iv"[0.1, Inf)")
@test isequal_interval(i, interval(0.1, Inf)) && !isguaranteed(i)
@test interval(IS.iv"[0.1, Inf]") === nai(Float64)
Expand Down

0 comments on commit eb865b2

Please sign in to comment.