Skip to content

Commit

Permalink
Add rem2pi. Fix JuliaAttic#18.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkoolen committed Sep 14, 2017
1 parent fa19c5a commit f6ce042
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ hasdiffrule(f::Symbol, arity::Int) = in((f, arity), DEFINED_DIFFRULES)
@define_diffrule hypot(x, y) = :($x / hypot($x, $y)), :($y / hypot($x, $y))
@define_diffrule mod(x, y) = :(first(promote(ifelse(isinteger($x / $y), NaN, 1), NaN))), :(z = $x / $y; first(promote(ifelse(isinteger(z), NaN, -floor(z)), NaN)))
@define_diffrule rem(x, y) = :(first(promote(ifelse(isinteger($x / $y), NaN, 1), NaN))), :(z = $x / $y; first(promote(ifelse(isinteger(z), NaN, -trunc(z)), NaN)))
@define_diffrule rem2pi(x, r) = :(1), :NaN

####################
# SpecialFunctions #
Expand Down
14 changes: 14 additions & 0 deletions test/RulesTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ for f in vcat(RealInterface.BINARY_MATH, RealInterface.BINARY_ARITHMETIC, RealIn
end
end
end

# Treat rem2pi separately because of its non-numeric second argument:
derivs = DiffBase.diffrule(:rem2pi, :x, :y)
for xtype in [:Float64, :BigFloat, :Int64]
for mode in [:RoundUp, :RoundDown, :RoundToZero, :RoundNearest]
@eval begin
x = $xtype(rand(1 : 10))
y = $mode
dx, dy = $(derivs[1]), $(derivs[2])
@test isapprox(dx, finitediff(z -> rem2pi(z, y), float(x)), rtol=0.05)
@test isnan(dy)
end
end
end

0 comments on commit f6ce042

Please sign in to comment.