Skip to content

Commit

Permalink
Fix method ambiguity for Base.TwicePrecision (#715)
Browse files Browse the repository at this point in the history
* Fix method ambiguity for `Base.TwicePrecision`

* Mark tests as not broken
  • Loading branch information
devmotion authored Oct 30, 2024
1 parent ec74fbc commit 826d919
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ end
@inline Dual{T,V,N}(x::Number) where {T,V,N} = convert(Dual{T,V,N}, x)
@inline Dual{T,V}(x) where {T,V} = convert(Dual{T,V}, x)

# Fix method ambiguity issue by adapting the definition in Base to `Dual`s
Dual{T,V,N}(x::Base.TwicePrecision) where {T,V,N} =
(Dual{T,V,N}(x.hi) + Dual{T,V,N}(x.lo))::Dual{T,V,N}

##############################
# Utility/Accessor Functions #
##############################
Expand Down
4 changes: 2 additions & 2 deletions test/AllocationsTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ convert_test_574() = convert(ForwardDiff.Dual{Nothing,ForwardDiff.Dual{Nothing,F
index = 1
alloc = @allocated ForwardDiff.seed!(duals, x, index, seeds)
alloc = @allocated ForwardDiff.seed!(duals, x, index, seeds)
if VERSION < v"1.9"
if VERSION < v"1.9" || VERSION >= v"1.11"
@test alloc == 0
else
@test_broken alloc == 0
Expand All @@ -33,7 +33,7 @@ convert_test_574() = convert(ForwardDiff.Dual{Nothing,ForwardDiff.Dual{Nothing,F
index = 1
alloc = @allocated ForwardDiff.seed!(duals, x, index, seed)
alloc = @allocated ForwardDiff.seed!(duals, x, index, seed)
if VERSION < v"1.9"
if VERSION < v"1.9" || VERSION >= v"1.11"
@test alloc == 0
else
@test_broken alloc == 0
Expand Down
4 changes: 4 additions & 0 deletions test/DualTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -667,4 +667,8 @@ end
@test ForwardDiff.derivative(float, 1)::Float64 === 1.0
end

@testset "TwicePrecision" begin
@test ForwardDiff.derivative(x -> sum(1 .+ x .* (0:0.1:1)), 1) == 5.5
end

end # module

0 comments on commit 826d919

Please sign in to comment.