Skip to content

Commit

Permalink
Special case x^0 (#331)
Browse files Browse the repository at this point in the history
fixes #330
  • Loading branch information
tkf authored and jrevels committed Jul 19, 2018
1 parent 2d09793 commit 123cc19
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/dual.jl
Expand Up @@ -408,8 +408,12 @@ for f in (:(Base.:^), :(NaNMath.pow))
begin
v = value(x)
expv = ($f)(v, y)
deriv = y * ($f)(v, y - 1)
return Dual{Tx}(expv, deriv * partials(x))
if y == zero(y)
new_partials = zero(partials(x))
else
new_partials = partials(x) * y * ($f)(v, y - 1)
end
return Dual{Tx}(expv, new_partials)
end,
begin
v = value(y)
Expand Down
10 changes: 10 additions & 0 deletions test/DualTest.jl
Expand Up @@ -455,4 +455,14 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
end
end

@testset "Exponentiation of zero" begin
x0 = 0.0
x1 = Dual{:t1}(x0, 1.0)
x2 = Dual{:t2}(x1, 1.0)
x3 = Dual{:t3}(x2, 1.0)
@test x3^2 === x3 * x3
@test x2^1 === x2
@test x1^0 === Dual{:t1}(1.0, 0.0)
end

end # module

0 comments on commit 123cc19

Please sign in to comment.