Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
handle Dual(Integer(1), n)^Integer
  • Loading branch information
jwscook committed Mar 21, 2021
commit cdf968280cffc8e910a93d79d3e8067c593e4dc6
1 change: 1 addition & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ function pow(z::Dual, n::AbstractFloat)
end
function pow(z::Dual{T}, n::Integer) where T
iszero(n) && return Dual(one(T), zero(T)) # avoid DomainError Int^(negative Int)
isone(z) && return Dual(one(T), epsilon(z) * n)
return Dual(value(z)^n, epsilon(z) * n * value(z)^(n - 1))
end
# these first two definitions are needed to fix ambiguity warnings
Expand Down
3 changes: 3 additions & 0 deletions test/automatic_differentiation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ powwrap(z, n, epspart=0) = Dual(z, epspart)^n
# these ones don't DomainError
@test powwrap(0, 0, 0) == Dual(1, 0) # special case is handled
@test powwrap(0, 0, 1) == Dual(1, 0) # special case is handled
@test powwrap(1, -1) == powwrap(1.0, -1) # special case is handled
@test powwrap(1, -2) == powwrap(1.0, -2) # special case is handled
@test powwrap(1, -123) == powwrap(1.0, -123) # special case is handled
@test powwrap(1, 0) == Dual(1, 1)
@test powwrap(123, 0) == Dual(1, 1)
for i ∈ -3:3
Expand Down