Skip to content

Commit

Permalink
check for iszero(partials(x)) in pow (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriselrod authored Aug 8, 2022
1 parent 61a7c5b commit 85f439e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ for f in (:(Base.:^), :(NaNMath.pow))
begin
v = value(x)
expv = ($f)(v, y)
if y == zero(y)
if y == zero(y) || iszero(partials(x))
new_partials = zero(partials(x))
else
new_partials = partials(x) * y * ($f)(v, y - 1)
Expand Down
6 changes: 6 additions & 0 deletions test/DualTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using DiffRules
import Calculus

struct TestTag end
struct OuterTestTag end

samerng() = MersenneTwister(1)

Expand All @@ -26,6 +27,8 @@ dual_isapprox(a::Dual{T,T1,T2}, b::Dual{T3,T4,T5}) where {T,T1,T2,T3,T4,T5} = er

ForwardDiff.:(::Type{TestTag()}, ::Int) = true
ForwardDiff.:(::Int, ::Type{TestTag()}) = false
ForwardDiff.:(::Type{TestTag}, ::Type{OuterTestTag}) = true
ForwardDiff.:(::Type{OuterTestTag}, ::Type{TestTag}) = false

for N in (0,3), M in (0,4), V in (Int, Float32)
println(" ...testing Dual{TestTag(),$V,$N} and Dual{TestTag(),Dual{TestTag(),$V,$M},$N}")
Expand Down Expand Up @@ -574,6 +577,9 @@ end
@test pow(x3, 2) === x3^2 === x3 * x3
@test pow(x2, 1) === x2^1 === x2
@test pow(x1, 0) === x1^0 === Dual{:t1}(1.0, 0.0)
y = Dual{typeof(TestTag())}(1.0, 0.0, 1.0);
x = Dual{typeof(OuterTestTag())}(0*y, 0*y);
@test iszero(ForwardDiff.partials(ForwardDiff.partials(x^y)[1]))
end

@testset "Type min/max" begin
Expand Down

0 comments on commit 85f439e

Please sign in to comment.