From 417a0b2848c5016ea3af222a1f52b3b83706e9cf Mon Sep 17 00:00:00 2001 From: Twan Koolen Date: Thu, 14 Sep 2017 13:12:20 -0400 Subject: [PATCH] Add rem2pi. Fix #18. --- src/rules.jl | 1 + test/RulesTests.jl | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/rules.jl b/src/rules.jl index 5267097..548bc19 100644 --- a/src/rules.jl +++ b/src/rules.jl @@ -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) = :(first(promote(ifelse(isinteger($x), NaN, 1), NaN))), :NaN #################### # SpecialFunctions # diff --git a/test/RulesTests.jl b/test/RulesTests.jl index f874976..fb03d17 100644 --- a/test/RulesTests.jl +++ b/test/RulesTests.jl @@ -38,3 +38,15 @@ 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 mode in [:RoundUp, :RoundDown, :RoundToZero, :RoundNearest] + @eval begin + x = 5π * (rand() - 0.5) + y = $mode + dx, dy = $(derivs[1]), $(derivs[2]) + @test isapprox(dx, finitediff(z -> rem2pi(z, y), x), rtol=0.05) + @test isnan(dy) + end +end