Skip to content

Commit 773039a

Browse files
authored
Merge pull request #502 from RainerHeintzmann/master
new rule: copysign
2 parents 262be3c + a81085b commit 773039a

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ChainRules"
22
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
3-
version = "1.7.0"
3+
version = "1.8.0"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/rulesets/Base/fastmath_able.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ let
184184
@scalar_rule max(x, y) @setup(gt = x > y) (gt, !gt)
185185
@scalar_rule min(x, y) @setup(gt = x > y) (!gt, gt)
186186

187+
@scalar_rule copysign(y, x) (ifelse(signbit(x)!=signbit(y), -one(y), +one(y)), NoTangent())
188+
187189
# Unary functions
188190
@scalar_rule +x true
189191
@scalar_rule -x -1

test/rulesets/Base/fastmath_able.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ const FASTABLE_AST = quote
192192
end
193193
end
194194

195+
@testset "copysign" begin
196+
# don't go too close to zero as the numerics may jump over it yielding wrong results
197+
@testset "at $y" for y in (-1.1, 0.1, 100.0)
198+
@testset "at $x" for x in (-1.1, -0.1, 33.0)
199+
test_frule(copysign, y, x)
200+
test_rrule(copysign, y, x)
201+
end
202+
end
203+
end
204+
195205
@testset "sign" begin
196206
@testset "real" begin
197207
@testset "at $x" for x in (-1.1, -1.1, 0.5, 100.0)

0 commit comments

Comments
 (0)