Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rem2pi #19

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
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